/* Menu JavaScript */

window.onload = function() {
  initializeMenu("homeMenu", "homeActuator");
  initializeMenu("communityMenu", "communityActuator");
  initializeMenu("linksMenu", "linksActuator");
  initializeMenu("helpMenu", "helpActuator");
}

var currentMenu = null;

if (!document.getElementById)
  document.getElementById = function() { return null; }

function evHandler(e) {
  var m=null;
  var k=null;
  if(e) { // Netscape/Mozilla/Konq
    k=e.which;
    if(!k) { // Konq
      k=event.keyCode;
    }
  } else { // MSIE
    k=event.keyCode;
    m=event.button;
  }
  if(k==27 || k==1 || m==1) {
    if(currentMenu) {
      currentMenu.style.visibility = "hidden";
      currentMenu=null;
    }
  }
  if(m) {
    return false;
  }
}

document.onkeydown = evHandler;
document.onmouseup = evHandler;

function initializeMenu(menuId, actuatorId) {
  var menu = document.getElementById(menuId);
  var actuator = document.getElementById(actuatorId);

  if (menu == null || actuator == null) return;

  // if (window.opera) return; // I'm too tired

  actuator.onfocus = function() {
    if (currentMenu) {
      currentMenu.style.visibility = "hidden";
      this.showMenu();
    } else {
      this.showMenu();
    }
  }

  actuator.onmouseover = function() {
    if (currentMenu) {
      currentMenu.style.visibility = "hidden";
      this.showMenu();
    } else {
      this.showMenu();
    }
  }

  actuator.showMenu = function() {
    var ot=0;
    var ol=0;
    var p=this.offsetParent;
    while (p) {
      ot+=p.offsetTop;
      ol+=p.offsetLeft;
      p=p.offsetParent;
    }
    ol+=this.offsetLeft;
    ot=ot+this.offsetTop+this.offsetHeight-1;
    if ( ! this.offsetParent ) {
      if ( this.y ) {
        ot += this.y;
      }
      if ( this.x ) {
        ol += this.x;
      }
    }

    menu.style.left = ol+"px";
    menu.style.top = ot+"px";
    menu.style.visibility = "visible";
    currentMenu = menu;
  }
}
