Bw.Widgets.Intuitive =
{
	superclassName: "Bw.Widgets.Editable",
	selfclassName: "Bw.Widgets.Intuitive",
	
	delay: 300,
	
	initialize: function ()
	{
		Bw.Widgets.Widget.initialize.call (this);
		
		this.disabled = false;
		this.list = null;
		this.field = null;
		this.lastEmit = '';
		
		this.src = this.getAttribute ("src");
		this.record = this.getAttribute ("record");
		this.rowValue = this.getAttribute ("rowValue");
		
		this.columns = this.initColumns ();
		
		var f = Bw.Widgets.Textfield.create ("");
		f.autocomplete = "off";
		with (f.style)
		{
			MozUserSelect = "text";
			width = this.style.width;
		}
		this.appendChild (f);
		
		this.field = f;
		
		var self = this;
		f.onkeydown = function (e) { return self.keyDown (Bw.getEvent(e)) };
		f.onkeyup = function (e){ return self.keyUp (Bw.getEvent(e)) };
		f.onblur = function () { setTimeout (function () { self.lostFocus() }, 100) };
		f.onfocus = function () { self.gotFocus() };
		
		setInterval (function () { self.emit() }, this.delay); 
		
		return false;
	},

	initColumns: function ()
	{
		var cols = [];
		
		var c = this.firstChild;
		while (c != null)
		{
			if (c.className && c.className == 'Bw.Widgets.List.Column')
			{
				cols.push (c);
			}
			
			this.removeChild (c);
			c = this.firstChild;
		}
				
		return cols;
	},

	showList: function ()
	{
		var f = this.field;
		var l = this.list;		
		if (l == null) 
		{
			var w = f.offsetWidth;
			if (navigator.product == "Gecko") w -= 2;
			
			l = Bw.Widgets.List.create (this.record, this.rowValue, this.columns, null, false, false, 100, w);
			with (l.style)
			{
				position = "absolute";
				zIndex = 1;
				top = (Bw.Util.getElementTopPosition (f) + f.offsetHeight - 1) + "px";
				left = (Bw.Util.getElementLeftPosition (f)) + "px";
			}
			
			this.appendChild (l);
			this.list = l;
			
			var self = this;
			l.onaction = function () { self.setValue (self.list.getValue()); self.list.hide() };
			l.onfocus = function () { self.gotFocus() };
			l.onblur = function () { setTimeout (function(){ self.lostFocus() }, 100) };
		}
		
		l.show();
		
		return l;
	},

	keyDown: function (e)
	{
		var k = e.keyCode;
		if (k == 40 || k==38 || k==33 || k==34 || k==36 || k==35)
		{
			this.list.keyPressed (e);
		}
	},

	keyUp: function (e)
	{
		var k = e.keyCode;
		
		if (k==40 || k==38 || k==33 || k==34 || k==36 || k==35) return;
		
		var l = this.list;
		var f = this.field;
		
		if (l != null && k == 27 || ((k == 8 || k == 46) && f.getValue() == ''))
		{
			l.hide();
			return;
		}
		else if (l != null && k == 13 && l.visible())
		{
			Bw.Util.call (l, l.onaction);
			return;
		}
		else if (l == null || !l.visible())
		{
			l = this.showList();
		}
	},
	
	emit: function ()
	{
		var l = this.list;
		var v = this.field.getValue();		
		if (l && l.visible() && v != '' && v != this.lastEmit)
		{
			l.clear();
			l.appendXML (this.src + v, null, true);
			l.layout(false);
			
			this.lastEmit = v;
		}
	},
	
	lostFocus: function ()
	{
		var a = document.activeElement;
		if (a && (a == this.field || (Bw.Util.findParentElement (a, null, "BwWidgetsIntuitive") != null)))
		{
			return;
		}
		
		var now = new Date().getTime();
		var dif = now - this.focusTime;
		if (dif > 200 && this.list)
		{
			this.list.hide();
		}
	},

	gotFocus: function ()
	{
		this.focusTime = new Date().getTime();
	},

	getValue: function ()
	{
		return this.field.getValue();
	},

	setValue: function (txt)
	{
		this.field.setValue (txt);
	},

	disable: function ()
	{
		this.disabled = true;
		this.field.disable();
	},
	
	enable: function ()
	{
		this.disabled = false;
		this.field.enable();
	}
};

