Bw.Widgets.SpinButton = 
{
	superclassName: "Bw.Widgets.Editable",
	selfclassName: "Bw.Widgets.SpinButton",
			
	create: function (min, max, step, value, pad)
	{
		var obj = document.createElement ('div');
		
		obj.setAttribute ("min", min);
		obj.setAttribute ("max", max);
		obj.setAttribute ("step", step);
		obj.setAttribute ("value", value);
		obj.setAttribute ("pad", pad);
		
		Bw.Core.bootstrap (obj, Bw.Widgets.SpinButton);
		
		return obj;
	},
	
	initialize: function ()
	{
		Bw.Widgets.Widget.initialize.call (this);
				
		this.draw ();
		
		this.min = this.getAttribute ("min") || 0;
		this.min = parseFloat(this.min);
		
		this.max = this.getAttribute ("max") || 100;
		this.max = parseFloat(this.max);

		this.step = this.getAttribute ("step") || 1;
		this.step = parseFloat(this.step);
				
		this.pad = this.getAttribute ("pad") || 0;
		this.pad = parseInt (this.pad);
			 
		var v = this.getAttribute ("value") || 0;
		this.setValue (v);
		
		this.onupdate = this.getAttribute ("onupdate");
		this.onfocus = this.getAttribute ("onfocus");

		var self = this;
		this.field.onupdate = function () { self.changed(); }; 
		this.field.onfocus = function () { self.focused(); }; 
			
		var p = function (e) { self.mousePressed (Bw.getEvent(e)); return false;};
		var r = function () { self.mouseReleased (); };
		
		this.up.firstChild.onmousedown = p;
		this.up.firstChild.onmouseup = r;
		this.down.firstChild.onmousedown = p;
		this.down.firstChild.onmouseup = r;
	},
	
	changed: function ()
	{
		if (this.onupdate) Bw.Util.call (this, this.onupdate);
	},
	
	focused: function ()
	{
		if (this.onfocus) Bw.Util.call (this, this.onfocus);
	},
	
	clear: function ()
	{
		this.field.clear();
	},
	
	mousePressed: function (e)
	{
		if (this.disabled) return false;
		
		this.mouseReleased();
		
		this.capturer = e.target;
		
		this.mul = 1;
		this.iter = 0;
		
		this.isUp = (this.capturer == this.up || this.capturer.parentNode == this.up);
		this.capturer.setCapture();
		
		var self = this;
		var f = function () { self.spin (); };
		f();
		this.loop = setInterval (f , 200);
	},

	mouseReleased: function ()
	{
		if (this.loop)
		{
			clearInterval (this.loop);
			this.loop = null;
			this.capturer.releaseCapture();
		}
	},
	
	spin: function ()
	{
		var s = this.step * this.mul;
		var o = (this.isUp) ? s : -s;
		
		var vo = this.value;
		var v = vo + o;
		
		if (this.canWrap && v >= this.max && !this.wrapped)
		{
			v = this.min;
			this.wrapped = true;
		}
		else if (this.canWrap && v <= this.min && !this.wrapped)
		{
			v = this.max;
			this.wrapped = true;
		}
		else
		{
			this.wrapped = false;
		}
		
		this.setValue (v);
		
		if (this.value != vo && this.onupdate) Bw.Util.call (this, this.onupdate); 
		
		if (((this.iter++) % 5) == 0) {
			this.mul++;
		}
	},
	
	setMin: function (v)
	{
		this.min = parseFloat (v);
		this.value = Math.max (v, this.value); 
	},
	
	setMax: function (v)
	{
		this.max = parseFloat (v);
		this.value = Math.min (v, this.value); 
	},
	
	setStep: function (v)
	{
		this.step = parseFloat (v);
	},
	
	setValue: function (v)
	{
		v = parseFloat (v);
		v = Math.min (this.max, v); 
		v = Math.max (this.min, v);
		
		if (this.step < 1)
		{
			var d = Math.round (1 / this.step);
			v = Math.round (v * d);
			v /= d;
		}
		
		this.value = v;
		this.field.setValue (this.padToZero (v));
	},
	
	padToZero: function (v)
	{
		var s = '' + v;
		while (s.length < this.pad)
		{
			s = '0' + s;
		}
		return s;
	},
	
	getValue: function ()
	{
		return parseFloat (this.field.getValue());
	},
	
	draw: function ()
	{
		with (this.style)
		{
			cursor = 'default';
		}
		
		var f = Bw.Widgets.Textfield.create();
		f.field.readOnly = true;
		
		with (f.style)
		{
			marginRight = "10px";
		}
		
		this.appendChild (f);
		
		this.field = f;
		
		var b = document.createElement ("div");
		with (b.style)
		{
			styleFloat = cssFloat = "right";
			width = "7px";
			var h = 19;
			if (document.all) h++;
			marginTop = (-h) + "px";
		}
		this.appendChild (b);
		
		this.up = this.createButton ("plus");
		b.appendChild (this.up);
		
		this.down = this.createButton ("minus");
		b.appendChild (this.down);
		
		b = document.createElement ("div");
		b.style.clear = "both";
		this.appendChild (b);
	},
	
	createButton: function (i)
	{
		var b = document.createElement ("div");
		with (b.style)
		{
			backgroundColor = "#eeeeee";	
			fontSize = "1px";
			lineHeight = "1px";
		}
		
		var bi = Bw.Widgets.StockIcon.create (i);
		b.appendChild (bi);
		
		return b;
	},
	
	hide: function ()
	{
		this.mouseReleased();
		Bw.Widgets.Widget.hide.call (this);
	},
	
	disable: function()
	{
		this.disabled = true;
		this.field.disable();
	},
	
	enable: function ()
	{
	    this.disabled = false;
		this.field.enable();
	}
};

