var maxMeaningful = 6;   // including the first root location. 
var namelookup = new Array();


var string = href.split( "/" );

var activeTab = null;   // current active Tab in this page

function getname(str){
	return namelookup[str];
}

function settab(id){
	// don't need to clear, cuase the page just loaded!
	// set the active tab
	var tabobj = document.getElementById("tab_"+ id);
	if (tabobj) {
		tabobj.className="active";
		activeTab = id; 
	}else{
		activeTab = ""; 
	}
	// enable the menu below that tab
	var menuobj = document.getElementById("menu_"+id);
	if (menuobj){
		menuobj.className="active";
	}
	// set active current active tab(for webapp and webstatic) 
	initTabEvent(); 
}

function showcursel(idtab, id){
	var menuitem=document.getElementById("item_"+idtab+ "_" + id);
	if (menuitem){
		menuitem.className="active";
	}
}

function initPath(){
		if(string.length>3){
			// menu's lacation for webstatic
			settab(string[2].toLowerCase()); // this part of URL define the tab
			var id = string[3].toLowerCase().split("."); // get rid of those with ".html" 
			showcursel(string[2].toLowerCase(), id[0]);  // this part of URL define the location of 2nd level menu
		}
}

/*
  following code is used to make a hover cool effect in tab menu (suggestion by hansen)
*/

var lastHighlightTab = null; 
function highlightTab(tab)
{
	if (lastHighlightTab != tab)
	{
		// recover old 
		if (lastHighlightTab)
		{
			lastHighlightTab.className = lastHighlightTab.savedClassName;
		}

		// make the hover effect stable for this tab
		if (tab.id != "tab_"+activeTab)
		{
			tab.savedClassName = tab.className;
			tab.className="highlight"; 
			lastHighlightTab = tab;
		}
	}
}

var lastShowMenu = null; 
function showMenu(id) 
{
	if (lastShowMenu)
	{
		if (lastShowMenu.id == id)
		{
			return;
		}
		if ((id==null || id==""))
		{
			if(lastHighlightTab) lastHighlightTab.className = "";
			if(lastShowMenu) lastShowMenu.style.display = "none";
			return;
		}
	}
	var menuobj = document.getElementById("menu_"+id);
	var tabobj = document.getElementById("tab_"+id);
	if (menuobj){
		// switch to menu related to current hover tab
		if (lastShowMenu)
		{
			lastShowMenu.style.display = "none"; 
		}
		menuobj.style.display = "inline";
		lastShowMenu = menuobj;

		// high light the current hover tab, so even when move leave, it still remain high lighted
		if (tabobj)
		{
			highlightTab(tabobj);
		}
	}	

	// change the background of navi bar to make it display better
	var bg = document.getElementById("naviMenu"); 
	if (bg)
	{
		if (id == activeTab)
		{
			//bg.style.background = " ";
			bg.onmouseover = null;
			bg.onmouseout = null;
		}
		else {
			//bg.style.background = " ";
			bg.onmouseover = onHoverMenuBg;
			bg.onmouseout = onMouseOutMenuBg;
		}
	}
}

var isOverMenu = false;
var isOverTab = false; 
function onHoverTab() 
{
	isOverTab = true; 
	showMenu(this.uid); 
}

function onMouseOutTab()
{
	isOverTab = false;
	// use time out to 
	setTimeout("delayRecoverMenu()", 400);
}

function onHoverMenu()
{
	isOverMenu = true; 
}

function onMouseOutMenu()
{
	isOverMenu = false;
	// use time out to 
	//setTimeout("delayRecoverMenu()", 200);
}

function delayRecoverMenu()
{
	if (isOverMenu || isOverTab) {
		// don't recover
	}
	else {
		showMenu(activeTab); 
		isOverMenu = false; 
		isOverTab = false;

		lastHighlightTab = null; 
	}
}

function onHoverMenuBg()
{
	isOverTab = true;
}
function onMouseOutMenuBg()
{
	if(isOverTab){
		isOverTab = false; 
		// use time out to 
		setTimeout("delayRecoverMenu()", 200);
	}
}
function initTabEvent() 
{
	if (activeTab)
	{
		showMenu(activeTab); 
	}
	var ids = ["index", "collection", "search", "blog", "group", "manage","help"]; // only list those tab who need this hover effect
	for (i=0 ; i < ids.length ; i++ )
	{
		var tab = document.getElementById("tab_" + ids[i]); 
		if (tab != null)
		{
			tab.uid = ids[i];
			tab.onmouseover=onHoverTab; 
			tab.onmouseout = onMouseOutTab;
		}
		var menu = document.getElementById("menu_" + ids[i]); 
		if (menu != null)
		{
			menu.onmouseover=onHoverMenu; 
			menu.onmouseout = onMouseOutMenu;
		}
	}
}
initPath();


