Bw.Widgets.Notebook =
{
	superclassName: "Bw.Widgets.Widget",
	selfclassName: "Bw.Widgets.Notebook",
	
	initialize: function ()
	{
		Bw.Widgets.Widget.initialize.call (this);
	
		this.tabContainer = null;
	
		this.tabs = [];
		this.pages = [];
	
		var s = this.getAttribute ("selected");
		this.selectedTab = (s) ? parseInt (s) : -1;
		
		var t = document.createElement ("div");
		t.className = "tabContainer";
		
		if (this.firstChild == null)
		{
			this.appendChild (t);
		}
		else
		{
			this.insertBefore (t, this.firstChild);
		}
		
		var s = document.createElement ("div");
		s.style.clear = "both";
		t.appendChild (s);
		
		s = s.cloneNode (false);
		t.appendChild (s);
		
		this.tabContainer = t;
		
		this.onselect = this.getAttribute ("onselect");
		
		return true;
	},
	
	addPage: function (page)
	{
		var i = this.pages.length;
		
		this.pages[i] = page;
		this.tabs[i] = page.tab;
		
		var c = this.tabContainer;
		c.insertBefore (page.tab, c.lastChild);
		
		if (page.tab.visible() && this.selectedTab == -1)
		{
			this.selectedTab = i;
		}
		else
		{
			page.hide();
		}
		
		this.updateTabs();
		
		return (this.selectedTab == i);
	},

	tabFromIndex: function (i)
	{
		return (i < this.tabs.length) ? this.tabs[i] : null;
	},
	
	indexFromTab: function (tab)
	{
		var l = this.tabs.length;
		for (var i = 0; i < l; i++)
		{
			if (this.tabs[i] == tab)
			{
				return i;
			}
		}
		return -1;
	},
	
	getSelectedTab: function ()
	{
		return this.tabs[this.selectedTab];
	},
	
	firstTab: function ()
	{
		var c = this.tabContainer.firstChild;
		while (c != null)
		{
			if (Bw.instanceOf (c, Bw.Widgets.Notebook.Tab) && c.visible()) break;
			c = c.nextSibling;
		}
		return c;
	},
	
	updateTabs: function ()
	{
		var s = this.getSelectedTab();
		var c = this.firstTab();
		while (c != null)
		{
			(c == s) ? c.renderSelected() : c.renderDeselected();
			c = c.nextTab();
		}
	}
};


Bw.Widgets.Notebook.Page =
{
	superclassName: "Bw.Widgets.View",
	selfclassName: "Bw.Widgets.Notebook.Page",
	
	initialize: function ()
	{
		Bw.Widgets.View.initialize.call (this);
		
		var i = this.getAttribute ("image");
		var l = this.getAttribute ("label");

		this.tab = Bw.Widgets.Notebook.Tab.create (i, l);

		var v = this.getAttribute ("hidden");
		if (v == "true")
		{
			Bw.Widgets.Widget.hide.call (this.tab);
		}
		
		var n = this.getContainer (Bw.Widgets.Notebook);
		var b = n.addPage (this);
		if (b) this.show();
		
		return false;
	}
};


Bw.Widgets.Notebook.Tab =
{
	superclassName: "Bw.Widgets.ImageLabel",
	selfclassName: "Bw.Widgets.Notebook.Tab",
	
	create: function (image, label)
	{
		var obj = document.createElement ("DIV");
			
		if (image && image != null) {
			obj.setAttribute ("image", image);
		}
		if (label && label != null) {
			obj.setAttribute ("label", label);
		}
		
		Bw.Core.bootstrap (obj, "Bw.Widgets.Notebook.Tab");
		
		return obj;
	},
	
	initialize: function ()
	{
		Bw.Widgets.ImageLabel.initialize.call (this);
	
		with (this.style)
		{
			cssFloat = styleFloat = "left";
			position = "relative";
			cursor = "default";
		}
		
		var self = this;
		this.onclick = function () { self.select(); };
		
		return false;
	},

	renderSelected: function ()
	{
		this.className = "tab selected";
	},
	
	renderDeselected: function ()
	{
		var n = this.getContainer (Bw.Widgets.Notebook);
		
		this.className = "tab";
	
		if (this.isFirstTab())
		{
			this.className +=" first";
		} 
		
		if (this.nextTab() == n.getSelectedTab())
		{
			this.className += " beforesel";
		}
	},
	
	isFirstTab: function ()
	{
		return (this._prev(this.previousSibling) == null);
	},
	
	nextTab: function ()
	{
		return this._next (this.nextSibling);
	},
	
	previousTab: function ()
	{
		return this._prev (this.previousSibling);
	},
	
	_prev: function (from)
	{
		var t = from;
		while (t != null)
		{
			if (Bw.instanceOf (t, Bw.Widgets.Notebook.Tab) && t.visible()) break;
			t = t.previousSibling;
		}
		return t;
	},
	
	_next: function (from)
	{
		var t = from;
		while (t != null)
		{
			if (Bw.instanceOf (t, Bw.Widgets.Notebook.Tab) && t.visible()) break;
			t = t.nextSibling;
		}
		return t;
	},
	
	show: function ()
	{
		if (this.visible()) return;
		
		Bw.Widgets.Widget.show.call (this);
		
		this.getContainer (Bw.Widgets.Notebook).updateTabs();
	},
	
	hide: function ()
	{
		if (!this.visible()) return;

		var n = this.getContainer (Bw.Widgets.Notebook);
		var o = null;
		
		if (n.getSelectedTab() == this)
		{
			o = this.previousTab();
			if (!o)	o = this.nextTab();
			if (!o)	return;
		}
		
		Bw.Widgets.Widget.hide.call (this);
		
		if (o) o.select();
		n.updateTabs();
	},
	
	select: function ()
	{
		var n = this.getContainer (Bw.Widgets.Notebook);
		var i = n.indexFromTab (this);
		var p = n.selectedTab;
		
		n.selectedTab = i;
		
		n.pages[p].hide();
		n.pages[i].show();
				
		n.updateTabs();
		
		if (i != p && n.onselect)
		{
			Bw.Util.call (n, n.onselect);
		}
	}
};


