/********************************************************************

	FOREX.COM - SPINNER CONTROL
	
	=========================================
	Author: Chris Erwin (chris@teehanlax.com)
	Agency: teehan+lax
	Date: July 15, 2008
	=========================================	
	
	
	Public Methods 
	-------------------------------------------------------------
	initialize (constructor) 
		- Arguments:
			- fieldID (string): the id of the HTML text field element we are adding a spinner control to
			- changeAmount (integer): the amount the field value should increment & decrement by
			- decimalPlaces (integer): the number of decimal places the value should have when changed
		- Returns: null
		
	
	Internal 'Private' Methods
	-------------------------------------------------------------
	incrementValue
		- Description: increments the value in the text field by the amount specified when instanstiating
		- Arguments: none
		- Returns: null
		
	decrementValue
		- Description: decrements the value in the text field by the amount specified when instanstiating
		- Arguments: none
		- Returns: null
		
		
	Sample HTML File
	-------------------------------------------------------------
	/_widget_samples/spinner_control.html
	
	
	Dependencies
	-------------------------------------------------------------
	- MooTools 1.2
	
*********************************************************************/

var SpinnerControl = new Class({
    initialize: function(fieldID, changeAmount, decimalPlaces){
		this.fieldID = fieldID;
		this.changeAmount = changeAmount;
		this.decimalPlaces = decimalPlaces;
		this.disabled = false;
		
		
		this.field = $(this.fieldID);
		
		var 	spinnerControl = new Element('div');
				spinnerControl.set('class','spinner_buttons');
		var 	upIMG = new Element('img');
				upIMG.set('alt','+');
				upIMG.set('class','spin_up');
				upIMG.set('src','/resources/images/buttons/btn_spinner_control_up_off.gif');
				spinnerControl.grab(upIMG);
		var 	downIMG = new Element('img');
				downIMG.set('alt','-');
				downIMG.set('class','spin_down');
				downIMG.set('src','/resources/images/buttons/btn_spinner_control_down_off.gif');
				spinnerControl.grab(downIMG);
		
		spinnerControl.inject(this.field, 'after');
		
		downIMG.addEvent('click', this.decrementValue.bind(this));
		upIMG.addEvent('click', this.incrementValue.bind(this));
    },
	
	incrementValue : function() {
		if (! this.disabled) {
			var newValue = this.field.value.toFloat() + this.changeAmount.toFloat()
			if(this.decimalPlaces != null) {
				newValue = newValue.toFixed(this.decimalPlaces);
			}
			this.field.value = newValue;
			this.field.fireEvent('valueChange');
		}
	},
	
	decrementValue : function() {
		if (! this.disabled) {
			var newValue = this.field.value.toFloat() - this.changeAmount.toFloat()
			if(this.decimalPlaces != null) {
				newValue = newValue.toFixed(this.decimalPlaces);
			}
			this.field.value = newValue;
			this.field.fireEvent('valueChange');
		}
	}
});
