/**
 * input.js
 * 
 * Widgets de formulaires, basé sur mootools.
 * 
 * Auteur : David Coudrier
 */

var Input={};

/**
 * Saisie de quantité.
 */
Input.Quantity = new Class({
	
	Implements: Options,
	
	options: {
		element: {
			'class': 'quantite'
		},
		incClass: 'inc',
		decClass: 'dec',
		step: 1,
		min: 1,
		max: 99
	},
	
	initialize: function(input, options) {
		//alert('okiii : '+input.get('id'));
		
		this.setOptions(options);
		var cont = new Element('span', this.options.element);
		
		var inc = new Element('div', {
			'class': this.options.incClass,
			'events': {
				'click': this.increment.bind(this)
			}
		});
		
		var dec = new Element('div', {
			'class': this.options.decClass,
			'events': {
				'click': this.decrement.bind(this)
			}
		});
		
		input.addEvent('keypress', function(event) {
			var key = event.key;
			if (key >= 0 && key <= 9 || ['enter','up','down','left','right','space','backspace','delete'].contains(key)) {
				return;
			}
			event.stop();
		});
		
		cont.replaces(input);
		cont.adopt([input, inc, dec]);

		this.input = input;
	},
	

	increment: function()
	{
		var value = parseInt(this.input.get('value'));
		if (isNaN(value)) value = 0;
		value += this.options.step;
		if (value >= this.options.max) {
			value = this.options.max;
		}
		this.input.set('value', value);
	},

	decrement: function()
	{
		var value = parseInt(this.input.get('value'));
		if (isNaN(value)) value = 0;
		value -= this.options.step;
		if (value <= this.options.min) {
			value = this.options.min;
		}
		this.input.set('value', value);
	}
	
});