/* 
 *
 * Created by Alexandr Trubnikov
 * alexandr@trubnikov.info
 * 
 */
Proto.UI.spinner=Class.create({
initialize: function() 
	{	
		//if(navigator.userAgent.indexOf('MSIE 6')!=-1||navigator.userAgent.indexOf('MSIE 7')!=-1){ return;}
		this.options = Object.extend({
		className:'Proto_spinner',
		container:document.body,
		element:false,
		interval:1,
		maxValue:false,
		minValue:false,
		padded:false,
		onMin:function(){},
		onMax:function(){}
		}, arguments[0] || { });
		
		this.onMax=function(){
		if(this.onMaxDelay){ window.clearTimeout(this.onMaxDelay);}
		this.onMaxDelay=this.options.onMax.bind(this).delay(0.5);
		};
		this.onMin=function(){
				if(this.onMinDelay){ window.clearTimeout(this.onMinDelay);}
		this.onMinDelay=this.options.onMin.bind(this).delay(0.5);
		};
	if(!this.options.element){ throw("Element no found for spinner"); }
	if(this.options.element.tagName!="INPUT"||this.options.element.getAttribute("type")!="text")
	{ throw("Element is not input text"); }
	this.element=this.options.element;
	if(isNaN(this.element.value)||this.element.value==""){this.element.value=this.options.minValue;}
	this.create();
},
interval:function(){if(typeof this.options.interval == "function")
{
return(this.options.interval());
}
return(this.options.interval);
},
min:function(){
if(typeof this.options.minValue == "function")
{
return(this.options.minValue());
}
if(typeof this.options.minValue == "number")
{
return(this.options.minValue);
}
return(-Infinity);

},
max:function(){
if(typeof this.options.maxValue == "function")
{
return(this.options.maxValue());
}
if(typeof this.options.maxValue == "number")
{
return(this.options.maxValue);
}
return(Infinity);

},
create:function()
{
	this.container = new Element("div",{'class':this.options.className});
	this.element.wrap(this.container);
	this.buttons = new Element("div",{'class':this.options.className+"-buttons"});
	this.container.insert(this.buttons);
	this.butUp = new Element("div",{'class':this.options.className+"-buttonUp"});
	this.buttons.insert(this.butUp);
	this.butDown = new Element("div",{'class':this.options.className+"-buttonDown"});
	this.buttons.insert(this.butDown);
	this.updateWidth();
	this.timer=false;
	this.plus=true;
	this.event=false;
	this.delay=false;


	
	this.butUp.observe(((!Prototype.Browser.MobileSafari)?"mousedown":"touchstart"),function(e)
			{
			this.plus=true;
			this.delay=this.egg.bind(this).delay(0.5);
			this.event=e;Event.stop(e);

			}.bind(this));
	_(document.body).observe(((!Prototype.Browser.MobileSafari)?"mouseup":"touchend"),function()
			{

			window.clearTimeout(this.delay);
			if(typeof this.timer.stop=="function"){this.timer.stop();};
			this.timer=false;

			}.bind(this));

	this.butDown.observe(((!Prototype.Browser.MobileSafari)?"mousedown":"touchstart"),function(e)
			{
			this.plus=false;this.event=e;Event.stop(e);
			this.delay=this.egg.bind(this).delay(0.5);

			}.bind(this));
	
	if(!Prototype.Browser.MobileSafari){
	this.butUp.observe("click",function()
			{
			this.up(1);
			}.bind(this));
	this.butDown.observe("click",function(){
			this.up();
			}.bind(this));
	
	this.element.observe(((!Prototype.Browser.Gecko)?"mousewheel":"DOMMouseScroll"),function(e){
			var raw = e.detail ? e.detail : e.wheelDelta;

			if(raw <0){
			this.up(1);
			}else{
			this.up();
			}
			Event.stop(e);


			}.bind(this));
			}
	this.element.onchange=function(){if(isNaN(this.value)){this.value=0;}}
	this.element.onkeyup=function(){if(isNaN(this.value)){this.value=0;}}
},
egg:function()
{
	new PeriodicalExecuter(function(pe) {
			this.up(this.plus);
			this.timer=pe; 
			try{  Event.stop(this.event) }catch(x){}
			}.bind(this), 0.05);
},
up:function(p)
{

	if(p)
	{
		var val=Number(this.element.value)+Number(this.interval());
		if(this.max&&Number(this.element.value)>=this.max()){val=this.max();this.onMax();}
		this.element.value=((this.options.padded)?Number(val).toPaddedString(this.options.padded):val);
	}
	else{
		var val=Number(this.element.value)-Number(this.interval());
		if(this.max()&&Number(this.element.value)<=this.min()){val=this.min();this.onMin();} this.element.value=((this.options.padded)?Number(val).toPaddedString(this.options.padded):val);}
},
updateWidth:function()
{
	this.element.style.width=(this.container.getWidth()-this.buttons.getWidth()-4)+"px";
	
	
	
}

})
