Bw.Widgets.Expander =
{
	superclassName: "Bw.Widgets.Widget",
	selfclassName: "Bw.Widgets.Expander",
	
	create: function (label, closed)
	{
		var obj = document.createElement ("div");
		
		obj.setAttribute ("label", label);
		obj.setAttribute ("closed", closed);
		
		Bw.Core.bootstrap (obj, "Bw.Widgets.Expander");
		
		return obj;
	},

	initialize: function ()
	{
		Bw.Widgets.Widget.initialize.call(this);
		
		this.view = null;
		this.control = null;

		var txt = this.getAttribute ("label");
		var c = Bw.Widgets.ImageLabel.create (null, txt);
		c.style.cursor="default";
		this.control = c;
		
		c.picture = Bw.Widgets.StockIcon.create ("closed");
		var p = c.picture;
		p.style.marginLeft="0px";
		c.insertBefore (p, c.text);
		
		var _this = this;
		c.onclick = function(){_this.toggle();};
		
		var v = Bw.Widgets.View.create();
		v.style.marginTop="5px";
		this.view = v;
		
		var child = this.firstChild;
		while (child != null)
		{
			this.removeChild (child);
			v.appendChild (child);
			child = this.firstChild;
		}
		
		this.appendChild (c);
		this.appendChild (v);
		
		var opened = this.getAttribute ("opened");
		if (opened != null && opened == "true") {
			this.toggle();
		}
		
		this.onopen = this.getAttribute ("onopen");
		
		return false;
	},
	
	toggle: function ()
	{
		(this.view.visible()) ? this.close() : this.open();
	},
	
	open: function ()
	{
		this.control.picture.setSource("opened");
		this.view.show();
		
		Bw.Util.call	(this, this.onopen);
	},
	
	close: function ()
	{
		this.control.picture.setSource("closed");
		this.view.hide();
	}
};


