/////////////////////////////////////////////////////////////////////////////
//
// Gladiator Components
//
// http://www.gladiatorweb.com
//
// (c) 2006 by Edward H. Trager .  All Rights Reserved
//
// NAME: gladiatorSpin.js
//
// DESCRIPTION: Implements a spin control
//
// AUTHOR: Edward H. Trager
//
// LAST UPDATE: 2006.05.22
//
// NOTE: SEE "LICENSE" FILE FOR LICENSING TERMS
//
//////////////////////////////////////////////////////////////////////////////

//
// DEPENDENCY CHECKING:
//
if(typeof GLADIATOR_CORE_INCLUDED == 'undefined') alert('NOTE BENE: "gladiatorSpin.js" REQUIRES "gladiatorCore.js"!');
if(typeof GLADIATOR_FORM_COMPONENTS_INCLUDED == 'undefined') alert('NOTE BENE: "gladiatorSpin.js" REQUIRES "gladiatorFormComponents.js"!');
if(typeof GLADIATOR_ALERT_INCLUDED == 'undefined') alert('NOTE BENE: "gladiatorSpin.js" REQUIRES "gladiatorAlert.js"!');

var GLADIATOR_SPIN_INCLUDED=true;


//////////////////////////////////////////
//
// gSpin: Spin widget
//
//////////////////////////////////////////
function gSpin(label,id,value,increment,minError,minWarn,maxWarn,maxError,parentNode){
	
	var _parentComponent = new gFormComponent(label,id,parentNode);
	
	var _thisComponent = _parentComponent.getNode();
	
	var _alertWindow =  new gAlert("Error","err_"+id,false,false,false,false,"<h2>Error</h2><p>The value is out of range.</p>",false);
	var _value;
	if(isNull(value)) _value=50;
	else      _value=value;
	
	var _increment;
	if(increment) _increment=increment;
	else          _increment=1;
	
	var _minError;
	if(minError) _minError=minError;
	else         _minError=0;
	
	var _minWarn;
	if(!isNull(minWarn)) _minWarn=minWarn;
	else        _minWarn=10;
	
	
	var _maxWarn;
	if(maxWarn) _maxWarn=maxWarn;
	else        _maxWarn=90;
	
	var _maxError;
	if(maxError) _maxError=maxError;
	else         _maxError=100;
	
	var _arrowContainer = document.createElementNS(NS.xhtml,"div");
	_arrowContainer.setAttribute("class","aC");
	_thisComponent.appendChild(_arrowContainer);
	
	var _upArrow = document.createElementNS(NS.xhtml,"div");
	_upArrow.setAttribute("class","uA");
	var _dnArrow = document.createElementNS(NS.xhtml,"div");
	_dnArrow.setAttribute("class","dA");
	_arrowContainer.appendChild(_upArrow);
	_arrowContainer.appendChild(_dnArrow);
	
	var _numberBox = document.createElementNS(NS.xhtml,"input");
	_numberBox.setAttribute("class","sI");
	_numberBox.id = id;
	_numberBox.name=id;
	//_numberBox.setAttribute("value",".");
	_numberBox.value=".";
	
	_thisComponent.appendChild(_numberBox);
	
	var _allowIncrement = false;
	
	//
	// incrementValue
	//
	function incrementValue(){
		
		if(_allowIncrement){
			_value+=_increment;
			//_numberBox.setAttribute("value",_value);
			_numberBox.value = _value;
			checkInputBoxStatus();
			window.setTimeout(incrementValue,280);
		}
	}
	
	//
	// decrementValue
	//
	function decrementValue(){
		
		if(_allowIncrement){
			_value-=_increment;
			//_numberBox.setAttribute("value",_value);
			_numberBox.value = _value;
			checkInputBoxStatus();
			window.setTimeout(decrementValue,280);
		}
	}
	
	//
	// checkInputBoxStatus
	//
	function checkInputBoxStatus(){
		
		var currentClass = _numberBox.getAttribute("class");
		if( _value>=_maxError || _value<=_minError ){
			if(currentClass!="sIError") _numberBox.setAttribute("class","sIError");
			_alertWindow.show(true);
			
		}else if( _value>=_maxWarn  || _value<=_minWarn  ){
			if(currentClass!="sIWarn" ) _numberBox.setAttribute("class","sIWarn" );
		}else{
			if(currentClass!="sI"     ) _numberBox.setAttribute("class","sI");
		}
		
	}
	
	//
	// startIncrement()
	//
	function startIncrement(){
		
		_allowIncrement = true;
		document.addEventListener("mouseup",stopIncrement,false);
		incrementValue();
		
	}
	
	//
	// startDecrement()
	//
	function startDecrement(){
		
		_allowIncrement = true;
		document.addEventListener("mouseup"  ,stopIncrement,false);
		decrementValue();
		
	}
	
	//
	// stopIncrement()
	//
	function stopIncrement(){
		
		_allowIncrement = false;
		document.removeEventListener("mouseup",stopIncrement,false);
		
	}
	
	//
	// function readBoxValue()
	//
	function readBoxValue(){
		_value = document.getElementById(_numberBox.id).value;
		//_value = _numberBox.getAttribute("value");
		//alert(_numberBox.getAttribute("value"));
		//alert(_value);
		checkInputBoxStatus();
		
	}
	
	_upArrow.addEventListener("mousedown",startIncrement,false);
	_dnArrow.addEventListener("mousedown",startDecrement,false);
	_numberBox.addEventListener("change",readBoxValue,false);
	
	/////////////////////////////
	//
	// Public Functions:
	//
	/////////////////////////////
	this.getNode = function(){
		
		return _thisComponent;
		
	}
	
	this.resetValue = function(){
		
		_numberBox.value = ".";
		_value = value;
	}
	
}

