var WM_TOP = "top";
var WM_BOTTOM = "bottom";
var WM_RIGHT = "right";
var WM_LEFT = "left";

var MARGIN_BOTTOM = 0;
var MARGIN_RIGHT = 20;

var currentItem = null;
var menuTrail = new Array();

var currentStyleOff = null;

function wmItemOn(item, level, styleOn, styleOff, submenuId, submenuPosition) {

  if (item.onmouseout == null) {
    item.onmouseout = startOffTimer;
  }

  stopOffTimer();

  if (currentItem != null) {
    if (styleOff != currentStyleOff && currentStyleOff != null) {
      currentItem.className = currentStyleOff;
    } else {
      currentItem.className = styleOff;
    }
  }

  currentItem = item;
  item.className = styleOn;
  currentStyleOff = styleOff;


  if (submenuId != null) {
    hide(level);

    var menu = document.getElementById(submenuId);
    var cat = document.getElementById("cat");

    if (submenuPosition == WM_TOP) {
      menu.style.top = findOffsetTop(item) - menu.offsetHeight + 10;
      menu.style.left = findOffsetLeft(item) + item.offsetWidth + MARGIN_RIGHT;
    }

    if (submenuPosition == WM_BOTTOM) {
      menu.style.top = findOffsetTop(item) + item.offsetHeight + MARGIN_BOTTOM;
      menu.style.left = findOffsetLeft(item);
    }

    if (submenuPosition == WM_RIGHT) {
      menu.style.top = findOffsetTop(item);
      menu.style.left = findOffsetLeft(item) + item.offsetWidth;
    }
    
    if (submenuPosition == WM_LEFT) {
      menu.style.top = findOffsetTop(item);
      menu.style.left = findOffsetLeft(item) - item.offsetWidth - MARGIN_RIGHT;
    }
    
    menu.style.visibility = "visible";
    if (cat != null) {
      cat.style.visibility = "hidden";
    }

    menuTrail[level] = menu;
  } else {

    // item has no submenu attached, update the menuTrail
    
    hide(level);
  }

}


// Hide all submenus after the level passed in, inclusive.
function hide(level) {
  for (var i = level; i < menuTrail.length; i++) {
    menuTrail[i].style.visibility = "hidden";
    var cat = document.getElementById("cat");
    if (cat != null) {
      cat.style.visibility = "visible";
    }
  }
}


// *** timer ******************************************************************

// Timer is needed to turn off all the items and submenus, 
// once mouse moves away from the menu.

var timerID = null;
var timerOn = false;
var timecount = 250;

function startOffTimer() {
  if (timerOn == false) {
    timerID = setTimeout("offAll()", timecount);
    timerOn = true;
  }
}

function stopOffTimer() {
  if (timerOn) {
    clearTimeout(timerID);
    timerID = null;
    timerOn = false;
  }
}

// Hide all submenus and turn current item off.
function offAll() {
  hide(0);
  
  if (currentStyleOff != null) {
    currentItem.className = currentStyleOff;
  }
}

// *** position ***************************************************************

// Utility code to calculate element position.

// Find total left offset.
function findOffsetLeft(obj){
  var curleft = 0;
  if (obj.offsetParent){
    while (obj.offsetParent){
      curleft += obj.offsetLeft;
      obj = obj.offsetParent;
    }
  } else if (obj.x) {
    curleft += obj.x;
  }

  return curleft;
}

// Find total top offset.
function findOffsetTop(obj){
  var curtop = 0;
  if (obj.offsetParent)	{
    while (obj.offsetParent){
      curtop += obj.offsetTop;
      obj = obj.offsetParent;
    }
  }else if (obj.y){
    curtop += obj.y;
  }

  return curtop;
}