/*
* Common Functions
*/

function OnSelectDropDown(dropdownname)
{
	var dropdown = document.getElementById(dropdownname);

	window.location = dropdown.options[dropdown.selectedIndex].value;
}

/*
getOffsetLeft
Return the absolute left dimension of the object in the current 
*/
function getOffsetLeft(objThis)
{
	var objOffsetParent = objThis.offsetParent;
	if (objOffsetParent)
	{
		return objThis.offsetLeft + getOffsetLeft(objOffsetParent);
	}
	else
	{
		return 0;
	}
}

function getMenuBarOffsetRight(objThis)
{
	var objParent = objThis.parentElement;
	if (objParent)
	{
		if (objParent.id == "globalNavBar")
		{
			return getOffsetLeft(objParent) + objParent.offsetWidth - 1;
		}
		else
		{
			return getMenuBarOffsetRight(objParent);
		}
	}
	else
	{
		return 0;
	}
}

function getOffsetTop(objThis)
{
	var objOffsetParent = objThis.offsetParent;
	if (objOffsetParent)
	{
		return objThis.offsetTop + getOffsetTop(objOffsetParent);
	}
	else
	{
		return 0;
	}
}

/*
* menuDropdown.js - implements an dropdown menu based on a HTML list
* Author: Dave Lindquist (dave@gazingus.org)
*/

var currentMenu = null;
var currentPath = new String(document.URL);
currentPath = currentPath.substring(currentPath.indexOf("/", currentPath.indexOf("//") + 2), currentPath.length);

if (!document.getElementById)
{
	document.getElementById = function() { return null; }
}

function initializeMenu(actuatorNum, actuatorPath)
{
	var mainmenu = document.getElementById("globalNavSubmenu_" + actuatorNum);
	var actuator = document.getElementById("globalNavLink_" + actuatorNum);
	var actuatorTab = document.getElementById("globalNav_" + actuatorNum);
	var blocker = document.getElementById("menuBlocker");

	if (actuator != null)
	{
		// Check if we need to highlight this tab
		if (actuatorTab != null)
		{
			var isCurrentTab;
			
			if (actuatorPath == "/")
			{
				// This is the "home" tab.  Only highlight this if we are actually in the home channel
				// i.e. there is only one slask
				isCurrentTab = (currentPath.indexOf("/",1) < 0);
			}
			else if (actuatorPath.length > 1)
			{
				isCurrentTab = (currentPath.indexOf(actuatorPath) == 0);
			}
			
			if (isCurrentTab)
			{
				if (actuatorTab.className != '')
				{
					actuatorTab.className = actuatorTab.className + ' globalNavItemActive';
				}
				else
				{
					actuatorTab.className = "globalNavItemActive";
				}
			}
		}

		//if (window.opera) return; // I'm too tired

		// Actuator Mouseover Event
		actuator.onmouseover = function()
		{
			if (currentMenu == null)
			{
				if (mainmenu != null)
				{
					this.showMenu();
				}
			}
			if (currentMenu)
			{
				currentMenu.style.visibility = "hidden";
				
				if (blocker)
				{
					blocker.style.visibility = "hidden";
				}
				
				if (mainmenu != null)
				{
					this.showMenu();
				}
			}
		}

		// Main Menu-related functions		
		if (mainmenu != null)
		{			
			mainmenu.onmouseover = function()
			{
				if (currentMenu == null)
				{
					this.showagainMenu();
				}
				if (currentMenu)
				{
					currentMenu.style.visibility = "hidden";
					this.showagainMenu();
				}
			}
			
			mainmenu.onmouseout = function()
			{
				if (currentMenu == null)
				{
					this.hideMenu();
				}
				if (currentMenu)
				{
					currentMenu.style.visibility = "visible";
					this.hideMenu();
				}
				
				// this.hideMenu();
			}

			actuator.showMenu = function()
			{
				var myLeft = getOffsetLeft(this);
				var myTop = getOffsetTop(this);
				var barRight = getMenuBarOffsetRight(this);
				
				if (((myLeft + mainmenu.offsetWidth) > barRight) && (barRight > 0))
				{
					myLeft = barRight - mainmenu.offsetWidth + 1;
				}
				
				mainmenu.style.left = myLeft + "px";
				mainmenu.style.top = (myTop + this.offsetHeight) + "px";
				if (blocker)
				{
					blocker.style.top = (myTop + this.offsetHeight) + "px";
					blocker.style.left = myLeft + "px";
					blocker.style.width = mainmenu.offsetWidth + "px";
					blocker.style.height = mainmenu.offsetHeight + "px";
					blocker.style.zIndex = "99";
					blocker.style.visibility = "visible";
				}
				mainmenu.style.visibility = "visible";
				mainmenu.style.zIndex = "100";
				currentMenu = mainmenu;
			}
			
			mainmenu.showMenu = function()
			{
				alert('How did we get here?');
				actuator.showMenu();
			}

			mainmenu.showagainMenu = function()
			{
				mainmenu.style.visibility = "visible";
				if (blocker)
				{
					blocker.style.visibility = "visible";
				}
				currentMenu = mainmenu;
			}
			
			mainmenu.hideMenu = function()
			{
				mainmenu.style.visibility = "hidden";
				if (blocker)
				{
					blocker.style.visibility = "hidden";
				}
				currentMenu = mainmenu;
			}
		}
	}
}
