//
//All the functions needed to handle a drop down navigation menu
//Use with Menu.ascx
//

var eOpenMenu = null;
var timeoutid = null;

//
//Opens a menu and places it underneath the item that called it
//
function OpenMenu(eSrc,eMenu)
{
	var menuBar = document.getElementById("divMenuBar");
	//position the drop down menu
	if (eSrc.className == "clsMenuHeaderHover" || eSrc.className == "clsImg" )
	{
		eMenu.style.left = findPosX(eSrc);
	}
	eMenu.style.top = findPosY(eSrc) + menuBar.offsetHeight;	
	eMenu.style.visibility = "visible";
	eOpenMenu = eMenu;
}


//
//Hides the menu to close it
//
function CloseMenu(eMenu)
{
	if(eMenu != null)
	{
		eMenu.style.visibility = "hidden";
		eOpenMenu = null;
		timeoutid = null;
	}
}

//
//Handles all the mouse over functions, deciding if menu's should be opened
//or hover effects should happen etc.
//
function GeneralOnMouseOver()
{
	var eSrc = window.event.srcElement;
	
	//weird result when a control is disabled
	if(eSrc == null)
		return; 
		
	var menuBar = document.getElementById("divMenuBar");
	
	//mouseover the menu
	if("clsDropMenu" == eSrc.className || "clsDropMenuItem" == eSrc.className || "clsDropMenuLink" == eSrc.className)
	{
		//mouseover the link in the menu
		if("clsDropMenuLink" == eSrc.className)
		{
			eSrc.parentNode.className="clsDropMenuItemHover";
		}
		clearTimeout(timeoutid);
		timeoutid = null;
	}
	//mouseover the header
	else if ("clsMenuHeader" == eSrc.className || "clsImg" == eSrc.className)
	{
		//create hover effect
		if("clsMenuHeader" == eSrc.className)
		{
			eSrc.className = "clsMenuHeaderHover";
		}
		else
		{
			var hover = eSrc.hover;
			if(hover != null)
			{
				var temp = eSrc.src;
				eSrc.src = hover;
				eSrc.hover = temp;
			}
		}
		//open or close menu
		var replace = eSrc.id.replace("tdMenuBarItem","divMenu");
		var eMenu = document.getElementById(replace);
		if (eOpenMenu != null && eOpenMenu != eMenu) 
		{
			CloseMenu(eOpenMenu);
		}
		if (eMenu) 
		{
			clearTimeout(timeoutid);
			timeoutid = null;
			OpenMenu(eSrc,eMenu);
		}
	}
	else if (eOpenMenu && !contains(eOpenMenu,eSrc) && !contains(menuBar, eSrc)) 
	{
		if(timeoutid == null)
		{
			timeoutid = setTimeout("CloseMenu(eOpenMenu)", 750);
		}
	}
}

//
//Handles all the mouse out functions.  Decides if hover's should be removed
//
function GeneralOnMouseOut ()
{
	//remove hover effect
	var eSrc = window.event.srcElement;
	
	//weird result if control is disabled
	if(eSrc == null)
		return;
		
	if (eSrc.className == "clsMenuHeaderHover" )
	{
		eSrc.className ="clsMenuHeader"; 
	}
	else if(eSrc.className == "clsDropMenuLink")
	{
		eSrc.parentNode.className = "clsDropMenuItem";
	}
	else if(eSrc.className == "clsImg")
	{
		var hover = eSrc.hover;
		if(hover != null)
		{
			var temp = eSrc.src;
			eSrc.src = hover;
			eSrc.hover = temp;
		}
	}		
}

//---helper functions --------------

//
// 
//
function InitMenu()
{
	if(navigator.appName == "Netscape")
	{
		netscapeEventCapture();
	}
	else if(navigator.appName.indexOf("Internet Explorer") != -1)
	{
		document.write('<script src="/common/menus/menuIE.js" type="text/javascript"></script>');
	}
}


//
//finds the exact x position of the passed in object
//
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;
}

//
//Finds the exact y position of the passed in object
//Note: not used now
//
function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

//
//Decides if item is a child of menu returns true if it is, false if it isn't
//*Note - ie has a function to do this, netscape does not
//
function contains(menu, item)
{
	var compare = item;
	
	while(compare)
	{
		if(compare == menu)
			return true;
		compare = compare.parentNode;
	}

	return false;
}

//--------Netscape functions -----------------------

//
//Captures the mouse over and mouse out events for netscape so that the GeneralMouseOver
//function can be used for all mouse over and out events
//
function netscapeEventCapture()
{
	window.captureEvents(Event.MOUSEOVER | Event.MOUSEOUT);
	window.onmouseover = netscapeMouseOver;
	window.onmouseout = netscapeMouseOut;
}

//
//Maps the netscape events to IE's window event
//
function setupEventObject(e) {
	if (e==null)
		return // makes sure that ie didn't accidently call this
	
	window.event = e
	window.event.fromElement = e.target
	window.event.toElement = e.target
	window.event.srcElement = e.target
	window.event.x = e.x
	window.event.y = e.y
}

//
//Allows netscape to handle all the mouse out events in one function that is
//also understandable by IE
//
function netscapeMouseOut(e)
{
	setupEventObject(e);
	GeneralOnMouseOut();
}

//
//Allows netscape to handle all the mouse over events in one function that is
//also understandable by IE
//
function netscapeMouseOver(e)
{
	setupEventObject(e);
	GeneralOnMouseOver();
}

