/**
* Returns the object with the given object ID. 
*/
function getDOMObject(objectId) {
  if(document.getElementById && document.getElementById(objectId)) {
	return document.getElementById(objectId);
   }
   else if (document.all && document.all(objectId)) {  
	return document.all(objectId);
   } else {
	return false;
   }
} 

/**
* Returns the style object related to the given object ID.
*/
function getStyleObject(objectId) {
  // checkW3C DOM, then MSIE 4, then NN 4.
  //
  if(document.getElementById && document.getElementById(objectId)) {
	return document.getElementById(objectId).style;
   }
   else if (document.all && document.all(objectId)) {  
	return document.all(objectId).style;
   } 
   else if (document.layers && document.layers[objectId]) { 
	return document.layers[objectId];
   } else {
	return false;
   }
}

/**
* Returns an object containing two attributes, x and y, which describe the location
* of the given object.
*/
function getObjectPosition (object) {
        var coords = Object();
        coords.x = object.offsetLeft;
        coords.y = object.offsetTop;
 

        while ((object = object.offsetParent) != null) {
                coords.x += object.offsetLeft;
                coords.y += object.offsetTop;
        }
        return coords;
}

/*****************************************************************/
/* GLOBALS */
/*****************************************************************/
currentMenuButton = null;
currentMenuName = null;
/*****************************************************************/


/**
*
* LOAD MENUS
*/
function loadMenus(url) {
  var loader = new net.ContentLoader(url, onMenuLoad, onMenuLoadError);
}


function loadMenusInternal() {
    var placeholderDiv = getDOMObject("menusDiv");

    if (placeholderDiv && menus) {
      placeholderDiv.innerHTML = menus;
    }
}

function onMenuLoad() {
  var placeholderDiv = getDOMObject("menusDiv");
  var menus = this.req ? this.req.responseText : "";

  if (placeholderDiv && menus) {
    placeholderDiv.innerHTML = menus;
    try {
      configure_menus();
    } catch (e) {}
    try {
      build_menus();
    } catch (e) {}
  } else {
    //alert("Error loading rollover menus");
  }
}


function onMenuLoadError() {
    //alert("Error loading rollover menus");
}

/**
* Displays the DIV with name describe in "menuName" underneath the button 
* described by "menuButton". 
*/
function showMenu(menuButton, menuName) {
  showMenu(menuButton, menuName, 0);
}

/**
* Displays the DIV with name describe in "menuName" underneath the button 
* described by "menuButton". The DIV's x-position can be altered with the xOffset param.
*/
function showMenu(menuButton, menuName, xOffset) {
  //Hide any currently open menus.
  getDOMObject(currentMenuName).hide = true; 
  if (currentMenuName) {
    hideMenu('',currentMenuName);
  }

  //Retrieve the objects we're going to use.
  var buttonObj = getDOMObject(menuButton);
  var menuDiv = getStyleObject(menuName);
  var coords = getObjectPosition(buttonObj);

  //Setup mouseovers to remind the browser that the menu is open
  getDOMObject(menuName).onmousemove = markMenuLayer;
  getDOMObject(menuName).onmouseover = markMenuLayer;
  getDOMObject(menuName).onmouseout = markMenuLayer;
  getDOMObject(menuButton).onmousemove = markMenuLayer;
  getDOMObject(menuButton).onmouseover = markMenuLayer;
  getDOMObject(menuButton).onmouseout = markMenuLayer;


  //Position and Display the menu
  menuDiv.left = coords.x + (xOffset ? xOffset : 0) + "px";
  menuDiv.top = coords.y + buttonObj.height + "px";
  menuDiv.display = "block";

  //Don't hide it
  getDOMObject(menuName).hide = false;

  //Save this menu to the global variables
  currentMenuButton = menuButton;
  currentMenuName = menuName;
}

/**
* Tells the engine not to hide the current menu. It also registers the fact
* that this event has been caught, so that other mouse events won't fire.
*/
function markMenuLayer(e) {
  getDOMObject(currentMenuName).hide = false;
  getDOMObject(currentMenuName).caught = true;
}

/**
* If the current menu does not have a caught mouse action, hide it in 75ms .
*
*/
function hideMenus(e) {
  if (currentMenuName != "" && getDOMObject(currentMenuName).caught != true) { 
    getDOMObject(currentMenuName).hide = true;
    window.setTimeout("hideMenu(\'\',\'"+currentMenuName+"\')", 75);
  }
   getDOMObject(currentMenuName).caught = false;
}

/**
* Hides the menu, so long as the "hide action" is still true.
* 
*/
function hideMenu(menuButton, menuName) {
  if (menuButton != currentMenuButton && getDOMObject(menuName).hide == true) {  
    var menuDiv = getStyleObject(menuName);
    
    //Hide the menu
    menuDiv.display = "none";
    
    //Clear out the mouseover events
    getDOMObject(menuName).onmousemove = null;
    getDOMObject(menuName).onmouseover = null;
    getDOMObject(menuName).onmouseout = null;

    //Clear out the global variables
    currentMenuButton = null;
    currentMenuName = null;
  }
}
