var MooSpinner = new Class({
	Implements: [Options, Events],
	initialize: function(element, options){
		this.setOptions({
			"minimum": 0,
			"maximum": 9,
			"increment": null,
			"decrement": null,
			"onChange":Class.empty
		},options);
		
		this.element = $(element);
		if(!this.element || this.element.get("tag") != "input") return;
		
		this.retrieve();
		
		if(this.options.increment){
			$(this.options.increment).addEvents({
				"mousedown": this.increment.bind(this),
				"click": function(e){ new Event(e).stop(); }
			});
		}
		
		if(this.options.decrement){
			$(this.options.decrement).addEvents({
				"mousedown": this.decrement.bind(this),
				"click": function(e){ new Event(e).stop(); }
			});
		}
	},
	
	retrieve: function(){
		this.value = isNaN(this.element.get("value")) || this.element.get("value") == "" ? this.options.minimum : this.element.get("value").toInt();
	},
	
	validate: function(v){
		if(v >= this.options.maximum) this.value = this.options.maximum;
		if(v <= this.options.minimum) this.value = this.options.minimum;
	},
	
	increment: function(e){
		if(e) new Event(e).stop();
		this.retrieve();
		this.validate(++this.value);
		this.update();
	},
	
	decrement: function(e){
		if(e) new Event(e).stop();
		this.retrieve();
		this.validate(--this.value);
		this.update();
	},
	
	update: function(){
		this.element.value = this.value;
		this.fireEvent("onChange", this, [this.value, this.element]);
	}
});