var delay = 1000;
var seltab = null;
var prevtab = null;
function tabs_init(id)
{
	seltab = document.getElementById(id);
	showTab(seltab, false);
}
function hideMenu(menu)
{
	if (menu != null)
		menu.style.display = 'none';
}
function showMenu(menu, tab)
{
	if (menu != null)
	{
		
		menu.style.display = 'inline';
		
		if (!menu.style.marginLeft)
		{
			var TextIndent = 0;
			var TabTable = document.getElementById("TabTable");
			var TotalWidth = TabTable.offsetWidth;
			var TabLeft = findPosX(tab)-findPosX(TabTable);
			var TabCenter = TabLeft+(tab.offsetWidth/2);
			var TextWidth = menu.offsetWidth;
			var TextCenter = TextWidth/2;
			
			if (TextCenter > (TotalWidth - TabCenter)) 
				TextIndent = (TotalWidth-TextWidth-20); //Right justify
			else if (TextCenter < TabCenter)
				TextIndent= (TabCenter - TextCenter-5);	//center
		
			menu.style.marginLeft = TextIndent;	
		}
	}
}
function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}


function tab_onMouseOver(tab)
{
	showTab(tab, true);
}
function tab_onMouseOut(tab)
{
	hideTab(tab);
}
function showTab(tab, displayMenu)
{
	if (tab != null)
	{
		if (typeof(tab) == 'string')
			tab = document.getElementById(tab);

		tab.state = 'A';
		tab.className = (tab == seltab) ? 'seltab' : 'hovtab';
		
		if (prevtab != tab)
			doHideTab(prevtab);
			
		if (displayMenu)
		{
			var menu = document.getElementById(tab.id + 'Menu');	
			showMenu(menu, tab);			
		}
		prevtab = tab;
	}
}
function hideTab(tab)
{
	if (tab != null)
	{
		if (typeof(tab) == 'string')
			tab = document.getElementById(tab);
			
		if (tab.state == 'A')
		{
			tab.state = 'H';
			setTimeout(function() {
				if (tab.state == 'H')
				{
					doHideTab(tab);
					prevtab = null;
				}
			}, delay);
		}
	}
}
function doHideTab(tab)
{
	if (tab != null)
	{
		tab.state = null;
		tab.className = (tab == seltab) ? 'seltab' : 'tab';
		var menu = document.getElementById(tab.id + 'Menu');
		hideMenu(menu);
	}
}

