var jSpinner={
	Version: '0.11',
	nightly: '20090122',
	create:function(q){
		// inizializza opzioni
		var o;
		if(arguments.length>1){
			if(is.Object(arguments[1])){
				o=arguments[1];
			}
		}
		// inizializza opzioni con valori di default
		if(!is.Defined(o)){
			o={step:1};
		}else{
			// step può non essere settato
			o.step=(!is.Defined(o.step)||!is.Num(o.step))?1:Number(o.step);
			// min e max possono non essere settati
			if(!is.Defined(o.rangeStart))o.rangeStart="";
			if(!is.Defined(o.rangeEnd))o.rangeEnd="";
		}
		// inizializza elemento
		
		var q=$(q).removeClassName("jSpinner");
		var spinnerContainer=new Element("div",{className:"jSpinner"});
		var divText       = new Element("DIV",{
			className:q.className,
			disabled:q.disabled,
			title:q.title,
			style:q.readAttribute("style")
		}).addClassName("text").update(q.value);
		spinnerContainer.appendChild(divText);
		
		q.insert({after:spinnerContainer});
		spinnerContainer.appendChild(q.remove());
		q.hide();

		var controls = new Element("div").addClassName("controls");
		
		var plus = new Element("A",{
			href:"javascript:;"
		}).insert("<span>+</span>").addClassName("plus").observe("mouseup",function(){
			var q = this.up("DIV.jSpinner").select("input").first() ;
			var v = o.step ;
			var newValue = jSpinner.getValue(q) + v ;
			if( newValue <= o.max && newValue >= o.min ) jSpinner.add(q,v);
		}).observe("click",function(){
			this.blur();
		});
		
		var minus = new Element("A",{
			href:"javascript:;"
		}).insert("<span>&minus;</span>").addClassName("minus").observe("mouseup",function(){
			var q = this.up("DIV.jSpinner").select("input").first() ;
			var v = -o.step ;
			var newValue = jSpinner.getValue(q) + v ;
			if( newValue <= o.max && newValue >= o.min ) jSpinner.add(q,v);
		}).observe("click",function(){
			this.blur();
		});
		
		controls.appendChild(plus);
		controls.appendChild(minus);
		spinnerContainer.appendChild(controls);
		
		// funzionalità rotellina
		divText.onWheel(function(event){
			var q = Event.element(event).up().select("input").first() ;
			var v = o.step * Event.wheel(event) * -1 ;
  			var newValue = jSpinner.getValue(q) + v;
			if( newValue <= o.max && newValue >= o.min ) jSpinner.add(q,v) ;
		});
		
		// funzionalità click destro e sinistro
		divText.observe("mouseup",function(event){
			var q = Event.element(event).up().select("input").first() ;
			var v = o.step * ( (event.ctrlKey||!event.isLeftClick()) ? -1 : 1 ) ;
			var newValue = jSpinner.getValue(q) + v ;
			if( newValue <= o.max && newValue >= o.min ) jSpinner.add(q,v);
		});
		
		// elimina selezioni accidentali
		Event.observe(spinnerContainer,"click",function(event){
			var q=$(Event.findElement(event,'DIV').up());
			if (window.getSelection)window.getSelection().removeAllRanges();
  			else if (document.getSelection)document.getSelection().removeAllRanges();
	  		else if (document.selection)document.selection.empty();
		});
		
		// elimina context menu (in Opera non si può)
		spinnerContainer.oncontextmenu=function(event){return false;}
	},
	getValue:function(q){
		return $(q).value*1 ;
	},
	setValue:function(q,v){
	  q=$(q);
	  q.value = v ;
	  q.up().select(".text").first().update(v) ;
	},
	add:function(q,v){
		q=$(q),v*=1;
  		var newValue = jSpinner.getValue(q) + v;
		jSpinner.setValue(q,newValue);
	}
}