Bw.Widgets.LanguageSelector =
{
	superclassName: "Bw.Widgets.Editable",
	selfclassName: "Bw.Widgets.LanguageSelector",
	
	create: function (src, record, rowValue, label, value, image)
	{
		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 (image) obj.setAttribute ("image", image);
		
		Bw.Core.bootstrap (obj, "Bw.Widgets.LanguageSelector");
		
		return obj;
	},

	initialize: function ()
	{
		var self = this;
		
		Bw.Widgets.Widget.initialize.call (this);
		
		this.rowValue = this.getAttribute ("rowValue");
		this.group = this.getAttribute ("group");
		this.record = this.getAttribute ("record");
		this.label = this.getAttribute ("label");
		this.image = this.getAttribute ("image");
		this.onaction = this.getAttribute ("onaction");
		this.value = this.getAttribute ("value");

		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");
		this.readLanguages (src, sel, null);
		
	},

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

	readLanguages: function (src, sel, srcGroup, group)
	{
		var recs = src.getNodes (this.record, srcGroup);
		var count = recs.length;

		var self = this;
		
		if( count < 2 )
		{
			return;	
		}
		
		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);
			var i = src.getNode (this.image, rec);
			
			v = Bw.Xml.Helpers.getNodeValue (v);
			l = Bw.Xml.Helpers.getNodeValue (l);
			i = Bw.Xml.Helpers.getNodeValue (i);
			
			var d = document.createElement("div");
			with(d.style)
			{
				cssFloat = styleFloat = "left";
				//margin = "2px 5px 2px 2px";
				padding = "2px";
				cursor = "pointer";
				cursor = "hand";
				background = "ButtonFace";
				
			}
			
			var o = document.createElement ("img");
			o.setAttribute ("source", i);
			
			Bw.Core.bootstrap(o, "Bw.Widgets.StockIcon");
						
			d.title = l;
			d.setAttribute ("value", v);
			
			d.onclick = function()
			{ 
				self.setValue(this.getAttribute("value"));
				
				var a = eval(self.onaction); 
				
				if( typeof a == 'function' ) 
				{
					a(this.getAttribute("value"));
				}
			}
			
			d.appendChild(o);
			this.appendChild(d);
			
			var separ = document.createElement('div');
			separ.innerHTML = "&nbsp;";
			with(separ.style)
			{
				width = "5px";
				cssFloat = styleFloat = "left";
			}
			
			this.appendChild(separ);
		}

		this.setValue(this.value);
	},

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

	setValue: function (v)
	{
		if( this.childNodes.length == 0 && v != null )
		{
			var self = this;
			setTimeout(function() { self.setValue(v) }, 500);
			return;
		}
		
		for(var i = 0; i < this.childNodes.length ; i ++ )
		{
			var child = this.childNodes[i];

			if( child.nodeType != 1 || child.firstChild.style == null )
			{
				continue;
			}

			if( child.getAttribute("value") == v )
			{
				child.firstChild.style.paddingTop = "3px";
				child.firstChild.style.paddingLeft = "3px";
				child.style.borderTop = "1px solid #aaa";
				child.style.borderLeft = "1px solid #aaa";
				child.style.borderBottom = "1px solid #fff";
				child.style.borderRight = "1px solid #fff";
				this.value = v;
			}
			else
			{
				child.firstChild.style.paddingTop = "2px";
				child.firstChild.style.paddingLeft = "2px";
				child.style.borderTop = "1px solid #fff";
				child.style.borderLeft = "1px solid #fff";
				child.style.borderBottom = "1px solid #aaa";
				child.style.borderRight = "1px solid #aaa";
			}
		}
	},

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