function TabControl_StartUp(tabBarButtonID)
{
	var tabControlButton = new TabControlButton();
	tabControlButton.button = document.getElementById(tabBarButtonID);
	tabControlButton.name = tabBarButtonID;
	tabControlButton.Depressed = false;
	
	var panelID = tabControlButton.button.getAttribute("panel");
	tabControlButton.Panel = document.getElementById(panelID);
	tabControlButton.Panel.style.display = "block";
	
	if(typeof(TabControl_Click) == "function")
		TabControl_Click(tabControlButton, null);
	
	tabControlButton.button.className = "TabControlButton_Depressed";
	tabControlButton.button.setAttribute("depressed", "true");
	
	activeTabBarButton = tabControlButton;
	
	// set all other buttons depressed = "false" and remove from depressed state bag
	var ToolControl_TabBarButtons_Depressed = document.getElementById("ToolControl_TabBarButtons_Depressed");
	var buttons = ToolControl_TabBarButtons_Depressed.value.split(",");
		
	if (!buttons) return;
	
	for(var i=0; i < buttons.length; i++)
	{
		var buttonID = buttons[i];
		if(buttonID == "")
			continue;
			
		if (buttonID != tabBarButtonID)
		{
			var button = document.getElementById(buttonID);
			button.setAttribute("depressed", "false");
			ToolControl_TabBarButtons_Depressed.value = ToolControl_TabBarButtons_Depressed.value.replace("," + buttonID, "");
		}
	}
	
	return;	
}

var activeTabBarButton = null;

function TabControlButton()
{
	this.Name = null;
	this.Depressed = false;
	this.Panel = null;
}

function TabControlButton_Disable(buttonID)
{
	 var button = document.getElementById(buttonID);
	 button.disabled = true;
}

function TabControlButton_Enable(buttonID)
{
	 var button = document.getElementById(buttonID);
	 button.disabled = false;
}

function TabControlButton_Click()
{
	var button = window.event.srcElement;
	while(button.tagName != "BUTTON")
		button = button.parentElement;
	button.blur();
	
	var tabControlButton = new TabControlButton();
	tabControlButton.button = button;
	tabControlButton.name = button.id;	
	tabControlButton.Depressed = false;
	
	var panelID = button.getAttribute("panel")
	tabControlButton.Panel = document.getElementById(panelID);
	
	if(activeTabBarButton != null)
	{
		activeTabBarButton.button.className = "TabControlButton";
		activeTabBarButton.button.setAttribute("depressed", "false");
		activeTabBarButton.Panel.style.display = "none";
	}
	activeTabBarButton = tabControlButton;
	tabControlButton.Panel.style.display = "block";
	
	if(typeof(TabControl_Click) == "function")
		TabControl_Click(tabControlButton, null);

	// add/remove from depressed state bag
	var ToolControl_TabBarButtons_Depressed = document.getElementById("ToolControl_TabBarButtons_Depressed");
	var depressedTabBarButtons = ToolControl_TabBarButtons_Depressed.value;
	
	if(!tabControlButton.Depressed)
	{
		button.setAttribute("depressed", "false");
		button.className = "TabControlButton_Over";
		
		if(depressedTabBarButtons.indexOf("," + TabControlButton.name) > -1)
			depressedTabBarButtons = depressedTabBarButtons.replace("," + toolbarButton.name, "");
	}
	else
	{
		button.setAttribute("depressed", "true");
		button.className = "TabControlButton_Depressed";
		
		if(depressedTabBarButtons.indexOf(tabControlButton.name) == -1)
			depressedTabBarButtons += "," + tabControlButton.name;
	}

	ToolControl_TabBarButtons_Depressed.value = depressedTabBarButtons;
	
	return;
}

function TabControlButton_Over()
{
	var button = window.event.srcElement;
	while(button.tagName != "BUTTON")
		button = button.parentElement;
	button.blur();
	var depressed = button.getAttribute("depressed");
	if(depressed == null || depressed == "false")
		button.className = "TabControlButton_Over";
	else
		button.className = "TabControlButton_Depressed";
}

function TabControlButton_Up()
{
	var button = window.event.srcElement;
	while(button.tagName != "BUTTON")
		button = button.parentElement;
	button.blur();
	var depressed = button.getAttribute("depressed");
	if(depressed == null || depressed == "false")
		button.className = "TabControlButton";
	else
		button.className = "TabControlButton_Depressed";
}

function TabControlButton_Out()
{
	var button = window.event.srcElement;
	while(button.tagName != "BUTTON")
		button = button.parentElement;
	button.blur();
	var depressed = button.getAttribute("depressed");
	if(depressed == null || depressed == "false")
		button.className = "TabControlButton";
	else
		button.className = "TabControlButton_Depressed";
}

function TabControlButton_Down()
{
	var button = window.event.srcElement;
	while(button.tagName != "BUTTON")
		button = button.parentElement;
	button.blur();
	var depressed = button.getAttribute("depressed");
	if(depressed == null || depressed == "false")
		button.className = "TabControlButton";
	else
		button.className = "TabControlButton_Depressed";
}
