// JavaScript Document

// global setting for overall menu width. This is only required
// for IE on Mac OS X. Not sure about versions of Mac OS prior
// to OS X. If you find menus are being cut off or have too much
// right-hand padding, reset this value.
var MAC_IE_MENU_WIDTH = 150;

/*** 
 *
 * Generic BrowserInfo() class.
 *
 ***/
function BrowserInfo()
{
  this.version = navigator.appVersion;
  this.platform = navigator.platform;
  this.agent = navigator.userAgent;
  
  this.ie = false;
  this.netscape = false;

  // look for Mac
  this.mac = (this.platform.indexOf("MacPPC") != -1) ? true : false;
  
  // look for IE.
  if (this.version.indexOf("MSIE") != -1)
  {
  	str = this.version.split("MSIE");
	this.ie = true;
	this.version = parseFloat(str[1]);
	return;
  }

  // look for netscape  
  if (this.agent.indexOf("Netscape6/") != -1) 
  {
    this.netscape = true;
    return;
  }
  
  // gecko is netscape!
  if (this.agent.indexOf("Gecko") != -1) 
  {
    this.netscape = true;
    this.version = 6.1;
    return;
  }

  // don't care about Opera or anything else.  

}

// browser object. this is to determine which browser
// we are currently using.
var bi = new BrowserInfo();

// currentButton is the currently selected menu's button..
var currentButton = null; 

/*** 
 *
 * hideMenu: Hides the current menu by setting its visibility to hidden..
 *
 ***/
function hideMenu()
{

    // if no menu is current displayed, bail.
	if (currentButton == null)
		return;
		
	currentButton.menu.style.visibility = "hidden";
} 

/***
 *
 * showMenu: When a mouseover occurs over a root menu button,
 *           display the menu associated with it.
 *
 ***/
function showMenu(event, menuName, topLoc, leftLoc)
{

	//  The menus are made up of the "Menu" class, and several
	// "menuItem" classes beneath. We don't need to display the
	// menu name, as that is taken care of by the button. 
	// Instead, we just show the "menuItem" items in the popup.	

	button = getElement(event); // get the root menu button

	currentButton = button;
	
  	if (button.menu == null)
  	    button.menu = document.getElementById(menuName);
 
    if (button.menu == null)
        return;     // something bad happened, bail
 
    // x will be the right-hand side of the root menu button         
	var x = getXOffset(button) + button.offsetWidth;
	
	// y will be the top of the root menu button
	var y = getYOffset(button);

	// netscape seems to not line-up properly here. need to subtract
	// offsetHeight value.
	if (bi.netscape) y -= button.offsetHeight;

    // IE for Mac will use the entire width of the page for each menu item
    // unless we tame it. This forces it to the MAC_IE_MENU_WIDTH value.
	if (bi.ie && bi.mac) button.menu.style.width = parseInt(MAC_IE_MENU_WIDTH) + "px";

	//button.menu.style.top  = y + "px";        // set the y value
	//button.menu.style.left = (x-18) + "px";	      // set the x value
	button.menu.style.top  = parseInt(topLoc) + "px";        // set the y value
	button.menu.style.left = parseInt(leftLoc) + "px";	      // set the x value
	button.menu.style.visibility = "visible"; // make it visible
    
}

/***
 * getXOffset: returns the X position of the element e.
***/
 
function getXOffset(e)
{
	var x = 0;

	if (e.offsetParent)
	{
		while (e.offsetParent)
		{
			x += e.offsetLeft;
			e = e.offsetParent;
		}
	}
	else
		if (e.x)
			x += e.x;
	
	return x;
}

/***
 *
 * getYOffset: returns the Y position of the element e.
 *
 ***/
function getYOffset(e)
{
	var y = 0;

	if (e.offsetParent)
	{
		while (e.offsetParent)
		{
			y += e.offsetTop;
			e = e.offsetParent;
		}
	}
	else
		if (e.y)
			y += e.y;
	
	return y;
}

/***
 *
 * getElement: returns the element associated with the event.
 *             this is called from showMenu, so the event is
 *             associated with a mouseOver.
 ***/
function getElement(event)
{
	var button = null;

  	if (bi.ie) button = window.event.srcElement;
  	else button = event.currentTarget;

	return button;
		
}

/***
 *
 * doMouseDownEvent: This is called when the user clicks somewhere
 *                   on the page. We need to trap this so if the user
 *                   clicks outside of the menu, we can hide the menu.
 *
 ***/                   
function doMouseDownEvent(event)
{

	var e = null;

	if (bi.ie) e = window.event.srcElement;
    else 
	{
		if (event.target.tagName != null)
			e = event.target;
		else
			e = event.target.parentNode;
	}

    // if no menu is currently visible, or the same button is clicked,
    // do nothing.
	if (currentButton == null || e == currentButton)
		return;
  
    // hide the menu for any non-menuItem object
    if (e.tagName != "A" && e.tagName != "menuItem")
		hideMenu();
    
    // hide the menu!
//    hideMenu();	
}



function getImageXfromLeft(imgID) {
  if (bi.netscape) return eval(imgID).x
  else return getRealLeft(imgID);
}


function getRealLeft(imgElem) {
        xPos = eval(imgElem).offsetLeft;
        tempEl = eval(imgElem).offsetParent;
        while (tempEl != null) {
                xPos += tempEl.offsetLeft;
                tempEl = tempEl.offsetParent;
        }
        return xPos;
}




// Main

// set up event handlers. we want to trap all mouse-clicks so if the
// user clicks outside of a menu, it will disappear. We do this by
// sending all mouse-clicks to doMouseDownEvent.

if (bi.ie) document.onmousedown = doMouseDownEvent;
else document.addEventListener("mousedown", doMouseDownEvent, true);