/*
	Copyright (c) 2007, Canto GmbH
	Cumulus main script for handling all web application functions
*/
var JSTabbedPane = Class.create ();
JSTabbedPane.prototype =
{
	initialize: function(element)
	{
		this.element = element;
		this.element.tabbedPane = this;
		this.tabs = [];
		this.selectedIndex = 0;
		this.tabbedTitleRow = document.createElement ("div");
		Element.addClassName (this.tabbedTitleRow, "jstabbedpanerow");
		this.element.insertBefore (this.tabbedTitleRow, this.element.firstChild);
		this.element.select ('.jstabbedpage').each (function (childElement)
		{
			if (childElement.nodeType == 1)
			{
				this.addTabbedPage (childElement);
			}
		}.bind (this));
		var clearDiv = document.createElement ("div");
		Element.addClassName (clearDiv, "jstabbedClearFloat");
		this.tabbedTitleRow.appendChild (clearDiv);
	},
	getSelectedIndex: function ()
	{
		return this.selectedIndex;
	},
	setSelectedIndex: function (idx)
	{
		if (this.selectedIndex != idx)
		{
			if (this.selectedIndex != null && this.tabs[this.selectedIndex] != null)
			{
				this.tabs[this.selectedIndex].setVisible (false);
			}
			this.selectedIndex = idx;
			this.tabs[this.selectedIndex].setVisible (true);
		}
	},
	addTabbedPage: function (tabbedPageElement)
	{
		var count = this.tabs.length;
		var newTabbedPage = new JSTabbedPage (this, tabbedPageElement, count);
		this.tabs[count] = newTabbedPage;
		this.tabbedTitleRow.appendChild (newTabbedPage.tab);
		newTabbedPage.setVisible (count == this.selectedIndex);
	},
	finalize: function ()
	{
		this.element.tabbedPane = null;
		this.element = null;
		this.tabbedTitleRow = null;
		this.tabs.each (function(tab)
		{
			tab.finalize ();
		}.bind (this));
		this.tabs.clear ();
		this.tabs = null;
	}
};
var JSTabbedPage = Class.create ();
JSTabbedPage.prototype =
{
	initialize: function (tabbedPane, element, indexAtParent)
	{
		this.tabbedPane = tabbedPane;
		this.element = element;
		this.index = indexAtParent;
		this.element.select (".jstabbedpagetitle").each (function (childNode)
		{
			if (childNode.nodeType == 1)
			{
				this.tab = childNode;
			}
		}.bind (this));
		Event.observe (this.tab, "click", this.onClick.bindAsEventListener (this), false);
	},
	setVisible: function (show)
	{
		if (show)
		{
			Element.show (this.element);
			Element.addClassName (this.tab, "selected");
		}
		else
		{
			Element.hide (this.element);
			Element.removeClassName (this.tab, "selected");
		}
	},
	onClick: function ()
	{
		this.tabbedPane.setSelectedIndex (this.index);
	},
	finalize: function ()
	{
		this.element = null;
		this.tabbedPane = null;
		Event.stopObserving (this.tab, "click", this.onClick, false);
		this.tab = null;
	}
};
function initializeTabs ()
{
	$$ ('.jstabbedpane').each (function (element)
	{
		if (element.tabbedPane == undefined)
		{
			new JSTabbedPane(element);
		}
	});
}
function finalizeTabs ()
{
	$$ ('.jstabbedpane').each (function (element)
	{
		element.tabbedPane.finalize ();
	});
}
Event.observe (window, "load", initializeTabs, false);
Event.observe (window, "unload", finalizeTabs, false);

