// Global menu element handle:
var LiveMenu = null;

// Global menu timeout handle:
var Timeout_ID = null;

// Opens or keeps open a given menu
// and shuts any previous menu:

function menuOver(MenuID){
  // If DOM1 supported and element exists ...
  if((document.getElementById)&&(document.getElementById(MenuID)!=null)){
    // If this menu is already open ...
    if(LiveMenu==document.getElementById(MenuID)){
      // Do not close it
      clearTimeout(Timeout_ID);
    }
    // Another might still be open ...
    else{
      // If another menu is open ...
      if(LiveMenu!=null){
        // Do not wait, shut it now
        clearTimeout(Timeout_ID);
        hideNow();
      }
    }
    // This is the new 'live' menu, make it visible
    LiveMenu = document.getElementById(MenuID);
    // LiveMenu.style.visibility is
    // initially empty in IE5 until
    // it is assigned by these
    // functions, so must check that
    // it's not null before proceeding...
    if((LiveMenu.style)&&(LiveMenu.style.visibility!=null)){
      LiveMenu.style.visibility = 'visible';
    }
  }
}

// Stops menu links from opening menu
// onmouseover when shut to
// workaround mouse events which are
// not hidden by z-index in Opera 4!
function stayOpen(MenuID){
  // If menuOver has not been called or the menu is hidden, do nothing
  if((LiveMenu==null) || ( (LiveMenu.style) && (LiveMenu.style.visibility) && (LiveMenu.style.visibility=='hidden') )) {
    return;
  } else {
    menuOver(MenuID);
  }
}
// Shuts a given menu in 50
// milliseconds, unless timeout is
// cleared by menuOver()
function menuOut(MenuID){
  // If DOM1 supported and a menu is open ...
  if((document.getElementById)&&(document.getElementById(MenuID)!=null)){
    // Get the current live menu
    LiveMenu = document.getElementById(MenuID);
    // Prepare to shut it in 50 milliseconds
    Timeout_ID = window.setTimeout('hideNow();',50);
  }
}
// Called by menu handlers to shut
// previous menu immediately
function hideNow(){
  if((LiveMenu.style)&&(LiveMenu.style.visibility)){
    LiveMenu.style.visibility = 'hidden';
  }
}

function positionMenus() {
  for ( var i = 1; i <= 4; i++ ) {
    var img = document.getElementById( 'Image' + i );
    var pos = absolutePositionOf( img );
    var menu = document.getElementById( 'Menu' + i );
    menu.style.left = pos.x + 'px';
  }
}

// calulates the visual absolute position of the node passed to function
function absolutePositionOf( object ) {
  var el = object; // I dont remember if object is a copy or alias
  for (var lx=0, ly=0; el != null; lx += el.offsetLeft,ly += el.offsetTop, el = el.offsetParent);
  return {x:lx,y:ly}
}

