Bw.Widgets.Popup =
{
	superclassName: "Bw.Widgets.Editable",
	selfclassName: "Bw.Widgets.Popup",
	
	create: function (src, record, rowValue, label, value)//, group)
	{
		var obj = document.createElement ("div");
		
		if (record) obj.setAttribute ("record", record);
		if (rowValue) obj.setAttribute ("rowValue", rowValue);
		if (label) obj.setAttribute ("label", label);
		if (src) obj.setAttribute ("src", src);
		if (value) obj.setAttribute ("value", value);
		//if (group) obj.setAttribute ("group", group);
		
		Bw.Core.bootstrap (obj, "Bw.Widgets.Popup");
		
		return obj;
	},

	initialize: function ()
	{
		var self = this;
		
		Bw.Widgets.Widget.initialize.call (this);
		
		this.pop = document.createElement ("select");
		this.pop.style.width = "100%";
		this.appendChild (this.pop);
		
		this.rowValue = this.getAttribute ("rowValue");
		this.group = this.getAttribute ("group");
		this.record = this.getAttribute ("record");
		this.label = this.getAttribute ("label");
		
		this.onupdate = this.getAttribute ("onupdate");
		this.pop.onchange = function () { self.updated (); }; 
		
		this.onfocus = this.getAttribute ("onfocus");
		this.pop.onfocus = function () { self.focused(); }; 
		
		var s = this.getAttribute ("src");
		if (s != null)
		{
			this.appendXML (s, null);
		}
		
		return false;
	},

	appendXML: function (src, cb)
	{
		var m = src;
		
		if (typeof m == "string")
		{
			var q = Bw.IO.Query.create ();
			q.setNoCache();
			
			var self = this;
			q.get (src, function () { self._asyncAppend (q.getMessage(), cb); } );
		}
		else
		{
			this._asyncAppend (m, cb);
		}
	},
	
	_asyncAppend: function (src, cb)
	{
		var sel = this.getAttribute ("value");
		if (this.group)
		{
			var recs = src.getNodes (this.group);
			var count = recs.length;
			
			for (var r = 0; r < count; r++) 
			{
				var rec = recs[r];
				var l = src.getNode (this.label, rec);
				l = Bw.Xml.Helpers.getNodeValue (l);

				var o = document.createElement ("optgroup");
				o.setAttribute ("label", l);
				
				this.pop.appendChild (o);
				
				this.readLabels (src, sel, rec, o);
			}
		}
		else
		{
			this.readLabels (src, sel, null);
		}
		
		if (cb) cb();
	},

	clear: function ()
	{
		var c;
		while ((c = this.pop.firstChild) != null)
		{
			this.pop.removeChild (c);
		}
	},

	readLabels: function (src, sel, srcGroup, group)
	{
		var recs = src.getNodes (this.record, srcGroup);
		var count = recs.length;
		
		for (var r = 0; r < count; r++) 
		{
			var rec = recs[r];
			
			var v = src.getNode (this.rowValue, rec);
			var l = src.getNode (this.label, rec);
			
			v = Bw.Xml.Helpers.getNodeValue (v);
			l = Bw.Xml.Helpers.getNodeValue (l);
			
			var o = document.createElement ("option");
			o.setAttribute ("value", v);
			if (v == sel)
			{
				o.setAttribute ("selected" , true);
			}
						
			var t = document.createTextNode (l);
	
			o.appendChild (t);
			(group || this.pop).appendChild (o);
		}
	},

	updated: function ()
	{
		if (this.onupdate) Bw.Util.call (this, this.onupdate);
	},
	
	focused: function ()
	{
		if (this.onfocus) Bw.Util.call (this, this.onfocus);
	},
	
	getValue: function ()
	{
		if( this.pop.selectedIndex < 0 )
		{
			return "";	
		}
		
		return this.pop.options[this.pop.selectedIndex].value;
	},

	setValue: function (v)
	{
		var self = this;
		setTimeout ( function () { self._setValue (v) }, 10);
	},
	
	_setValue: function (v)
	{
		var ops = this.pop.options;
		var l = ops.length;
		for (var i = 0; i < l; i++)
		{
			var o = ops[i];
			if (o.value == v)
			{
				this.pop.selectedIndex = i;
				return;
			}
		}
	},

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


