/**********************************************************************
Copyright (c)2003 by Graphical Dynamics, Inc. all rights reserved.

Popup menu functions.

 Date   Who Changes
------- --- -----------------------------------------------------------
03sep05 jlp Created.
03sep06 jlp New approach inspired by AiG menus.
03sep06     Last changed.
**********************************************************************/
g_oCurrMain		 = null;  // Current main menu item
g_oCurrPopup	 = null;  // Current popup menu
g_bMouseOverMenu = false; // Is mouse still over the popup?


/**********************************************************************
Utility functions

getElementLeft, getElementTop
Gets the true absolute left & top for an element. Elements that don't 
have style.position="absolute" have left & top values that are relative 
to their containing elements. In this context most items are contained 
in the BODY, or sometimes a DIV - but also, a TD is always contained by 
its TR.
**********************************************************************/
function getElementLeft (p_oThis)
	{
	var oThis;
	var nLeft;
	
	oThis = p_oThis;
	nLeft = 0;
	while (oThis)
		{
		nLeft += oThis.offsetLeft;
		oThis  = oThis.offsetParent;
		}
	return nLeft;
	}

/*********************************************************************/
function getElementTop (p_oThis)
	{
	var oThis;
	var nTop;
	
	oThis = p_oThis;
	nTop = 0;
	while (oThis)
		{
		nTop  += oThis.offsetTop;
		oThis  = oThis.offsetParent;
		}
	return nTop;
	}

	
/**********************************************************************
PMMouseOver

Mouseover event for a main menu item. This displays this main menu 
item's popup menu. If it doesn't have a popup, we want to close any 
current popup. If it does have a popup, initialize it if necessary, 
then show the popup.
**********************************************************************/
function PMMouseOver (p_sID)
	{
	if (!p_sID)
		{// This main menu item doesn't have a popup associated with it...
		PMPopClose ();
		}
	else
		{// There should be a popup defined for us as ourid-popup...
		g_bMouseOverMenu = true; // In case mouse came back here from the popup
		g_oCurrMain = document.getElementById (p_sID);
		g_oCurrMain.onmouseout = PMMouseOut;
		oPopup = document.getElementById (g_oCurrMain.id + "-popup");
		
		if (!oPopup || oPopup != g_oCurrPopup)
			{// Either there wasn't a popup after all, or we moved onto 
			//  another main menu item...
			PMPopClose ();
			}
		if (!oPopup)
			{return;
			}
		
		g_oCurrMain.style.cursor = "default"; // force an arrow
		
		if (!oPopup.bPMInitialized)
			{// Initialize this menu's popup menu...
			oPopup.style.left	= getElementLeft(g_oCurrMain);
			oPopup.style.top	= getElementTop(g_oCurrMain) + g_oCurrMain.offsetHeight;
			oPopup.onmouseover	= PMPopMouseOver;
			oPopup.onmouseout	= PMMouseOut;
			oPopup.onclick		= PMPopMouseClick;
			
			// Initialize its TD & A descendants...
			aTDs = oPopup.getElementsByTagName ("TD");
			for (n=0; n<aTDs.length; n++)
				{
				aTDs[n].className	= "pmpopupitem";
				aTDs[n].onmouseover	= PMPopTDMouseOver;
				aTDs[n].onmouseout	= PMPopTDMouseOut;
				aTDs[n].onclick		= PMPopTDMouseClick;
				aAs = aTDs[n].getElementsByTagName ("A");
				aTDs[n].myA = aAs[0];
				aAs[0].className	= "pmpopupitem";
				aAs[0].onmouseover	= PMPopAMouseOver;
				aAs[0].onmouseout	= PMPopAMouseOut;
				}
			
			oPopup.bPMInitialized = true;
			}
		
		// For each TD & A tag in this popup, set its style...
		oPopup.style.visibility = "visible";
		g_oCurrPopup = oPopup;
		}
	}


/**********************************************************************
PMMouseOut
PMMouseCheck

The mouse moved off the main menu item, or off the current popup.
Either way, set us up to check again in 1/10 sec. to see if we're 
"really" off the item.
**********************************************************************/
function PMMouseOut ()
	{
	g_bMouseOverMenu = false;
	
	// If mouse is still over a popup menu or main menu item 1/10 of a 
	// sec from now, then this wesn't a real mouseout...
	setTimeout ("PMMouseCheck ()", 100 );
	}

/*********************************************************************/
function PMMouseCheck ()
	{
	if (!g_bMouseOverMenu && g_oCurrPopup)
		{// The mouse really is off of a popup menu...
		PMPopClose ();
		}
	}


/**********************************************************************
PMPopMouseOver

The mouse moved across the popup menu.
**********************************************************************/
function PMPopMouseOver ()
	{
	g_oCurrPopup = this;
	g_bMouseOverMenu = true;
	}


/**********************************************************************
PMPopMouseClick

The user clicked the mouse within a TD.
**********************************************************************/
function PMPopMouseClick ()
	{
	PMPopClose ();
	}


/**********************************************************************
PMPopClose

Close the current popup menu.
**********************************************************************/
function PMPopClose ()
	{
	if (g_oCurrPopup)
		{
		g_oCurrPopup.style.visibility = "hidden";
		g_oCurrPopup = null;
		}
	}


/**********************************************************************
PMPopTDMouseOver
PMPopTDMouseOut
PMPopTDMouseClick

These make it look & act as if the A link spanned the whole TD.
**********************************************************************/
function PMPopTDMouseOver ()
	{
	this.className	   = "pmpopupitem-hover";
	this.myA.className = "pmpopupitem-hover";
	}

/*********************************************************************/
function PMPopTDMouseOut ()
	{
	this.className	   = "pmpopupitem";
	this.myA.className = "pmpopupitem";
	}

/*********************************************************************/
function PMPopTDMouseClick ()
	{
	window.location.replace (this.myA.href);
	}


/**********************************************************************
PMPopAMouseOver
PMPopAMouseOut
PMPopAMouseClick

These make it look & act as if the A link spanned the whole TD.
**********************************************************************/
function PMPopAMouseOver ()
	{
	this.parentNode.className = "pmpopupitem-hover";
	}

/*********************************************************************/
function PMPopAMouseOut ()
	{
	this.parentNode.className = "pmpopupitem";
	}



