var g_id = null;  				// Timer action ID
var g_pending_object = null;	// The last menu table 
var g_pending_show = null;		// The last event (Show when mouse over, hide when mouse out) 
var g_openMenus = [];			// The open menus list(from last menu table to all parent menu tables)


// onMenuMouseOver
function m_over(obj)
{
	// The mouse over and out event bubbles up to all parent menu table.
	// This function will be called multiple times up to root parent menu item when a child menu event occurs

	//1. Change the current menu item to hightlight status
	//a.menu row
	obj.className = "MenuItemTRHighlight";
	//b. menu link (first td)
	obj.children[0].children[0].className = "MenuItemLinkHighlight";
	//2. If there are sub menus
	if (obj.children[1].children.length>1)
	{
		//a. menu arrow (second td)
		obj.children[1].children[0].style.display = "none";
		obj.children[1].children[1].style.display = "block";
		
		//b. Start timer and show child menu table and all parent menu tables
		startTimer(obj,true);
	}
}

// onMenuMouseOut
function m_out(obj)
{
	// The mouse over and out event bubbles up to all parent menu table.
	//  This function will be called multiple times up to root parent menu item when a child menu event occurs

	//1. Change the current menu item to normal status
	//a.menu row
	obj.className = "MenuItemTR";
	//b. menu link (first td)
	obj.children[0].children[0].className = "MenuItemLink";
	//2. If there are sub menus
	if (obj.children[1].children.length>1)
	{
		//a. menu arrow (second td)
		obj.children[1].children[0].style.display = "block";
		obj.children[1].children[1].style.display = "none";

		//c. Start timer and hide all pop up menus
		//obj = event.srcElement;
		startTimer(obj,false);
	}
	
}

//Go to the specified url
function go(ref, target) 
{ 
	if (target=="" || target==null)
	{
		window.location.href=ref;
	}
	else
	{
		window.open(ref, target);
	}
	
	window.event.cancelBubble = true;
}

//Start timer
function startTimer(obj, show)
{
	var cancel = false;

	var menuTable = getSubMenuTable(obj);	

	//If there is no sub menu table (posting), Do nothing
	if (menuTable==null)
	{
		return;
	}

	// Cancel the action (Don't start a new timer) if the menu action is bubble from child menu
	// The mouse over and out event bubbles up to all parent menu table 	
	// a.current menu table is the parent if the pending object (last menu item)
	// b.current action is the same as the pending action (last menu item)
	if (g_pending_object!=null)
	{
		if (isParent(menuTable,g_pending_object) && g_pending_show==show)
			cancel = true;
	}

	//Set timer action		
	if (cancel==false)
	{
		
		clearTimer();

		g_pending_object = menuTable;
		g_pending_show = show;
		g_id = window.setTimeout(endTimer,500);
	}
}
			
//Get the sub menu table, it is in the third "TD"
function getSubMenuTable(obj)
{
	if (obj.children.length<3)
		return null;
	var menuTables = obj.children[2].children.tags("table");
	
	if (menuTables.length>0)
		return menuTables[menuTables.length-1];

	return null;
}

//Is the first object the parent of the second object
function isParent(parent, obj)
{
	if (obj==null||parent==null)
		return false;
	if (parent.id==obj.id)
		return true;
	return isParent(parent,obj.parentElement);
}

//Show child menu tables
function showMenuTable(table)
{
	
	table.style.display = "block";
}


//Timer action (Show or hide menu) 
function endTimer()
{
	
	var menuTable = g_pending_object;
	var show =  g_pending_show ;

	//1. Close pop up menus
	//if the current action is "hide" (mouse out)
	//or if the menu table is not the parent of the current pending menu table (Last menu)

	for (var i = 0; i < g_openMenus.length; i++)
	{
		if (show==false||!isParent(g_openMenus[i],menuTable))
			g_openMenus[i].style.display = "none";
	}

	g_openMenus = [];
	
	//2. show the current pending menu table and all parent menu tables if the current action is "show" (Mouse over)
	if (show==true)
	{
		while(menuTable!=null)
		{
			if (menuTable.className=="SubMenuTable")
			{

				g_openMenus[g_openMenus.length] = menuTable;
				showMenuTable(menuTable);
			}
			menuTable = menuTable.parentElement;
		}
	}

	
	clearTimer();
}


//Stop timer 
function clearTimer()
{
	if (g_id!=null)
	{

		window.clearTimeout(g_id);
		g_pending_object = null;
		g_pending_show = null;
	}
	g_id = null;
}
//Set up menus
//This is called in the menu data javascript file
function setupMenus()
{
    
    if (document.all)
	{
		var  arrLevel0MenuItems = g_menus[3];
		//There are 4 sections in the top level
		//Level 0
		for (var i=0; i<arrLevel0MenuItems.length; i++)
		{
			var level0MenuItem = arrLevel0MenuItems[i];
			var arrLevel1MenuItems = level0MenuItem[3];
			//Level 1
			for (var j=0; j<arrLevel1MenuItems.length; j++)
    			{
				var level1MenuItem = arrLevel1MenuItems[j];
			
				var objMenu = document.all['SubMenuParent_' + level1MenuItem[0]];
				//alert(objMenu.id);
				if (objMenu)
				{
						//Can't pass parameter to the event function on the fly
						//Work around: create a wrap function	
         				//objMenu.onmouseover =function menu1_onmouseover(){m_over(this);};
	        			//objMenu.onmouseout = function menu2_onmouseout(){m_out(this);};
		        		
		        		//contain sub menu
	        			if(level1MenuItem.length==4)
	        			{	
	        				objMenu.children[1].innerHTML = SUBMENUARROW;
							//Level 2		
	        				var subMenuTableHTML;
	        				subMenuTableHTML = BuildSubMenuTable(level1MenuItem);
	        				objMenu.children[2].innerHTML = subMenuTableHTML;
	        			}
				}
			} //end of level 1
		} //end of level 0
	}
 }

//Build sub menu table
function BuildSubMenuTable(parentMenuItem)
{
	    var subMenuTableHTML;
	    var subMenuItemsHTML;
	    subMenuTableHTML = SUBMENUTABLE;
	    //1. Create menu section title
	    subMenuTableHTML = subMenuTableHTML.replace("[GUID]",parentMenuItem[0]);
	    subMenuTableHTML = subMenuTableHTML.replace("[Text]",parentMenuItem[2]);
	    
	    //2. Create sub menu item list
	    subMenuItemsHTML="";
    	var arrMenuItems = parentMenuItem[3];
	    for(var i=0;i<arrMenuItems.length;i++)
	    {
			subMenuItemsHTML += buildMenuItemRow(arrMenuItems[i]);
		}
			    
	    subMenuTableHTML = subMenuTableHTML.replace("[SubMenuItems]",subMenuItemsHTML);
	    
		return subMenuTableHTML;
}



//Build menu item row
function buildMenuItemRow(menuItem)
{
	    var menuItemHTML;
	    
	    menuItemHTML = MENUITEM;
	    //1. Create menu item
	    menuItemHTML = menuItemHTML.replace("[GUID]",menuItem[0]);
	    menuItemHTML = menuItemHTML.replace(/\[Url\]/gi,menuItem[1]); //there are two place to replace
	    menuItemHTML = menuItemHTML.replace("[Text]",menuItem[2]);
	    //2. Create sub menu table if the menu item contains sub menu (Channel)
		if(menuItem.length==4)
		{
			menuItemHTML = menuItemHTML.replace("[SubMenuArrow]",SUBMENUARROW);
			menuItemHTML = menuItemHTML.replace("[SubMenuTable]",BuildSubMenuTable(menuItem));
		}
		//No sub menu (Posting)
		else
		{
			menuItemHTML = menuItemHTML.replace("[SubMenuArrow]","");
			menuItemHTML = menuItemHTML.replace("[SubMenuTable]","");
		}
		
		return menuItemHTML;
}

 

//Sub menu table template
var SUBMENUTABLE="<table id='SubMenuTable_[GUID]' class='SubMenuTable' cellspacing='0' cellpadding='0' border='0'>";
SUBMENUTABLE+=		"<tr class='MenuSectionTR'>";
SUBMENUTABLE+=			"<td class='MenuSectionTextTD' nowrap='nowrap' colspan='3'><span class='MenuSectionText'>[Text]</span></td>";
SUBMENUTABLE+=		"</tr>";
SUBMENUTABLE+=		"[SubMenuItems]";
SUBMENUTABLE+=	"</table>"
//Menu item template
var MENUITEM="<tr class='MenuItemTR' id='MenuItem_[GUID]' onmouseover='m_over(this)' onmouseout='m_out(this)' onclick='go(\"[Url]\",\"\")'>";
MENUITEM +=		"<td class='MenuItemLinkTD' height='20'><a class='MenuItemLink' href='[Url]'>[Text]</a></td>";
MENUITEM +=		"<td class='MenuItemArrowTD'>[SubMenuArrow]</td>";
MENUITEM +=		"<td valign='top'>[SubMenuTable]</td>";
MENUITEM +="</tr>";
//Sub Menu arrow image
var SUBMENUARROW="<img src='/Heims.Web.Portal/Resources/arrow_orange.gif' alt='' border='0' />";
SUBMENUARROW+=	"<img src='/Heims.Web.Portal/Resources/arrow_orange.gif' alt='' border='0' style='display:none;' />";
