//----------------------------------------------------------------------------
// Code to determine the browser and version.
//----------------------------------------------------------------------------

function Browser() {

  var ua, s, i;

  this.isIE    = false;  // Internet Explorer
  this.isNS    = false;  // Netscape
  this.version = null;

  ua = navigator.userAgent;

  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  // Treat any other "Gecko" browser as NS 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }
}

var browser = new Browser();

var activeMenuItem = null;

function resetMenuItem(menuItem){
	// Restore the menu item style class.	
	menuItem.className = "menuItem";
		
	// Hide the sub menu, first closing any sub menus.
	if (menuItem.subMenu != null) {
		closeSubMenu(menuItem.subMenu);
		menuItem.subMenu.style.display = "none";
	}
}

function activateMenuItem(menuItem, subMenuID){

	// Associate the named sub menu to this menu item if not already done.
	if (menuItem.subMenu == null && subMenuID!=null) {
		menuItem.subMenu = document.getElementById(subMenuID);
	}
	
	// Set mouseout event handler for the menu item, if not already done.
	if (menuItem.onmouseout==null){
		menuItem.onmouseout=menuObjectMouseOut;
	}
	
	// Exit if this menu item is the currently active one.
	if (menuItem == activeMenuItem)
		return;
	
	// Reset the currently active menu item, if any.
	if (activeMenuItem != null)
		resetMenuItem(activeMenuItem);

	// Activate this button, unless it was the currently active one.
	if (menuItem != activeMenuItem) {
		var x, y;
		
		// Update the button's style class to make it look like it's
		// depressed.
		menuItem.className = "menuItemOnMouseOver";
		
		// Set mouseout event handler for the button, if not already done.
		if (menuItem.onmouseout == null)
			menuItem.onmouseout = menuObjectMouseOut;
			
		if (menuItem.subMenu!=null && menuItem.subMenu.onmouseout == null)
			menuItem.subMenu.onmouseout = menuObjectMouseOut;

		if(menuItem.subMenu!=null){
			// Position the associated drop down menu under the button and
			// show it.			
			x = getPageOffsetLeft(menuItem);
			y = getPageOffsetTop(menuItem) + menuItem.offsetHeight;
			
			// For IE, adjust position.
			if (browser.isIE) {
				x += menuItem.offsetParent.clientLeft;
				y += menuItem.offsetParent.clientTop;
			}
		
			menuItem.subMenu.style.left = x + "px";
			menuItem.subMenu.style.top  = y + "px";
			menuItem.subMenu.style.display = "block";
		}
		
		activeMenuItem = menuItem;
	}
	else
		activeMenuItem = null;
	
}

function menuItemMouseover(menuItem){
	menuItemMouseover(menuItem, null);
	
}
function menuItemMouseover(menuItem, subMenuID) {

	//activate this menu item if no other is currently activate
	if (activeMenuItem==null){
		activateMenuItem(menuItem, subMenuID);
	}
	
	//if any other menu item is active, activate this one instead
	if (activeMenuItem!=null && activeMenuItem!=menuItem){
		activateMenuItem(menuItem, subMenuID);
	}
}

function closeSubMenu(subMenu)
{
	if (subMenu == null || subMenu.activeMenuItem == null)
		return;
	
	// Recursively close any sub menus.
	
	if (subMenu.activeMenuItem.subMenu != null) {
		closeSubMenu(subMenu.activeMenuItem.subMenu);
		subMenu.activeMenuItem.subMenu.style.display = "none";
		subMenu.activeMenuItem.subMenu = null;
	}
	//-----------------------------------------------------------
	//-----------------------------------------------------------
	// may need to find mechanism to deactivate sub menus here.
	subMenu.activeItem = null;
}

function getPageOffsetLeft(el) {

  var x;

  // Return the x coordinate of an element relative to the page.

  x = el.offsetLeft;
  if (el.offsetParent != null)
    x += getPageOffsetLeft(el.offsetParent);

  return x;
}

function getPageOffsetTop(el) {

  var y;

  // Return the x coordinate of an element relative to the page.

  y = el.offsetTop;
  if (el.offsetParent != null)
    y += getPageOffsetTop(el.offsetParent);

  return y;
}

function menuObjectMouseOut(event) {
	
	var el;
	
	// If there is no active button, exit.	
	if (activeMenuItem == null)
		return;
	
	// Find the element the mouse is moving to.
	if (browser.isIE)
		el = window.event.toElement;
	else if (event.relatedTarget != null)
		el = (event.relatedTarget.tagName ? event.relatedTarget : event.relatedTarget.parentNode);
	
	// If the element is not part of a menu, reset the active button.
	if (getContainerWith(el, "DIV", "subMenu") == null) {
		resetMenuItem(activeMenuItem);
		activeMenuItem = null;
	}
}

function getContainerWith(node, tagName, className) {

	// Starting with the given node, find the nearest containing element
	// with the specified tag name and style class.
	
	while (node != null) {
		if (node.tagName != null && node.tagName == tagName && hasClassName(node, className))
			return node;
		node = node.parentNode;
	}
	return node;
}

function hasClassName(el, name) {

	var i, list;
	
	// Return true if the given element currently has the given class
	// name.
	
	list = el.className.split(" ");
	for (i = 0; i < list.length; i++)
		if (list[i] == name) return true;
		
	return false;
}

function errorHandler(message, url, line)
{
	// message == text-based error description
	// url     == url which exhibited the script error
	// line    == the line number being executed when the error occurred
	
	// handle the error here
	alert("FOUND ERROR\n\nmessage:  "+message+"\nurl:  "+url+"\nline:  "+line);
	// stop the event from bubbling up to the default window.onerror handler
	// (see the "For More Info" section for an article on event bubbling)
	return true;
}

// install the global error-handler
window.onerror = errorHandler;
