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

	initialize: function ()
	{
		Bw.Widgets.Widget.initialize.call (this);
		
		this.tools = [];
		this.toolsByName = {};
		
		var c, a = [];
		while ((c = this.firstChild) != null)
		{
			a.push (c);
			this.removeChild (c);
		}
		
		this.draw ();
		
		for (var i = 0; i < a.length; i++)
		{
			var w = a[i];
			Bw.Core.bootstrap (w);
			if (w.selfclass)
			{
				this.append ("tool_" + i, w);
			}
		}
		
		delete a;
	},
	
	draw: function ()
	{
		var s = document.createElement ('div');
		with (s.style)
		{
			clear = 'both';
		}
		
		this.appendChild (s);
		this.firstSpacer = s;
		
		s = s.cloneNode(false);
		this.appendChild (s);
		this.lastSpacer = s;
	},
	
	toolize: function (widget)
	{
		with (widget.style)
		{
			display = "block";
			cssFloat = styleFloat = "left";
		}
		
		return widget;
	},
	
	append: function (key, widget)
	{
		var t = this.lookup (key);
		if (t) this.remove (key);
		
		this.tools.push (widget);
		this.toolsByName[key] = widget;
		
		this.insertBefore (this.toolize (widget), this.lastSpacer);
	},
	
	insert: function (key, widget, refKey)
	{
		var t = this.lookup (key);
		if (t) this.remove (key);
		
		var ref = this.lookup (refKey);
		if (ref)
		{
			var i = this.tools.lookup (ref);
			var a1 = this.tools.slice (0, i);
			var a2 = this.tools.slice (i);
			this.tools = a1.concat (widget, a2);
			this.toolsByName[key] = widget
		
			this.insertBefore (this.toolize (widget), ref);
		}
	},
	
	remove: function (key)
	{
		var widget = this.lookup (key);
		
		var i = this.tools.lookup (widget);
		var a1 = this.tools.slice (0, i);
		var a2 = this.tools.slice (i + 1);
		this.tools = a1.concat (a2);
		delete this.toolsByName[key];
	
		this.removeChild (widget);	
	},
	
	lookup: function (key)
	{
		return this.toolsByName[key];
	}
};

Bw.Widgets.Toolbar.Button = 
{
	superclassName: "Bw.Widgets.Editable",
	selfclassName: "Bw.Widgets.Toolbar.Button",
			
	create: function (notpersistent)
	{
		var obj = document.createElement ('div');
		
		obj.setAttribute ("notpersistent", notpersistent);
		
		Bw.Core.bootstrap (obj, "Bw.Widgets.Toolbar.Button");
		
		return obj;
	},
	
	createWithImage: function (src, notpersistent)
	{
		var o = this.create (notpersistent);
		o.appendChild (Bw.Widgets.Image.create (src));
		return o;
	},
	
	createWithIcon: function (name, notpersistent)
	{
		var o = this.create (notpersistent);
		o.appendChild (Bw.Widgets.StockIcon.create (name));
		return o;
	},
	
	initialize: function ()
	{
		Bw.Widgets.Widget.initialize.call (this);
		
		this.notpersistent = this.getAttribute ("notpersistent");
		
		var self = this;
		this.onmousedown = function () { self.mousePressed(); return true; };
		this.onmouseup = function () { self.mouseReleased(); return true; };
		this.onclick = function () { self.mouseClicked(); return true; };		
	},
	
	stylist: function ()
	{
		var c = this.selfclassName.replace (/\./g, "");
				
		c += (this.checked) ? " checked" : "";
		c += (this.pressed) ? " pressed" : "";
		
		this.className = c;
	},
	
	mousePressed: function ()
	{
		this.pressed = true;
		this.setCapture();
		this.stylist ();
	},
	
	mouseReleased: function ()
	{
		this.pressed = false;
		this.releaseCapture();
		this.stylist ();
	},
	
	mouseClicked: function ()
	{
		if (!this.notpersistent)
		{
			this.checked = !this.checked;
		}
		
		this.stylist ();
		Bw.Util.call (this, this.onupdate);
	},
	
	setValue: function (val)
	{
		this.checked = val;
		this.stylist ();
	},
	
	getValue: function ()
	{
		return this.checked;
	}
};

