
var tabbars = new Array();

function TabBar(id, hiddenFieldId, current)
{
	this.id = id;
	this.hiddenFieldId = hiddenFieldId;
	this.current = current;
	this.tabs = new Array();
	
	this.addTab = __addTab;
	this.renameTab = __renameTab;
	this.select = __select;
}

function __addTab(tab)
{
	this.tabs.push(tab);
}

function __renameTab(ordinal, value)
{
	var tabCell = document.getElementById(this.id + ordinal);
	tabCell.innerHTML = value;
}

function __select(ordinal)
{
	if (this.current == ordinal)
		return;
		
	var tab = this.tabs[ordinal];
	if (tab.run())
	{
		var current = this.current;
		
		// Close current tab
		var leftImg = document.getElementById(this.id + "img" + current);
		var rightImg = document.getElementById(this.id + "img" + (current + 1));
		var tabCell = document.getElementById(this.id + current);
		leftImg.src = "/Controls/TabBar/" + (current == 0 ? "tableftback" : "tabrightbackoverlap") + ".gif";
		rightImg.src = "/Controls/TabBar/" + (current == this.tabs.length - 1 ? "tabrightback" : "tabrightbackoverlap") + ".gif";
		tabCell.className = "tabbarUnselected";
		
		// Open new tab
		leftImg = document.getElementById(this.id + "img" + ordinal);
		rightImg = document.getElementById(this.id + "img" + (ordinal + 1));
		tabCell = document.getElementById(this.id + ordinal);
		leftImg.src = "/Controls/TabBar/" + (ordinal == 0 ? "tableftfront" : "tableftoverlap") + ".gif";
		rightImg.src = "/Controls/TabBar/" + (ordinal == this.tabs.length - 1 ? "tabrightfront" : "tabrightoverlap") + ".gif";
		tabCell.className = "tabbarSelected";

		this.current = ordinal;
		document.getElementById(this.hiddenFieldId).value = ordinal;
	}
}

function Tab(action)
{
	this.action = action;
	this.run = __run;
}

function __run()
{
	if (this.action != null)
		return eval(this.action);

	return true;
}

function tabbarSelect(id, ordinal)
{
	tabbarFind(id).select(ordinal);
}

function tabbarSelectedOrdinal(id)
{
	return tabbarFind(id).current;
}

function tabbarFind(id)
{
	for (var i = 0; i < tabbars.length; i++)
	{
		if (tabbars[i].id == id)
			return tabbars[i];
	}
	
	return null;
}

function tabbarRename(id, ordinal, value)
{
	tabbarFind(id).renameTab(ordinal, value);
}

function tabbarAddTabBar(tabbar)
{
	tabbars.push(tabbar);
}

function tabbarAddTab(id, tab)
{
	tabbarFind(id).addTab(tab);
}
