var Menu = function (pTable, pDiv, pMenu, pCurrent) {

  this.table = pTable;
  this.div = pDiv;
  this.menu = pMenu;
  this.current = pCurrent;
  this.timer = null;

  var tempThis = this;
  var doc = this.table.ownerDocument || this.table.document;

  if (this.table.tBodies.length == 0) {
    var tableBody = doc.createElement("tbody");
    this.table.appendChild(tableBody);
  }
  var tableBody = this.table.tBodies[0];
  while (tableBody.rows.length > 0) {
    tableBody.deleteRow(-1);
  }
  while (this.div.lastChild) {
    this.div.removeChild(this.div.lastChild);
  }

  var row = tableBody.insertRow(-1);
  for (var i = 0; i < this.menu.length; i++) {
    var item = this.menu[i];
    var section = item[0];
    var title = item[1];
    var url = item[2];
    var target = item[3];
    var menu = item[4];
    var cell = row.insertCell(-1);
    if (url) {
      if (this.current == section) {
        cell.className = "menu-current";
      }
      cell.onclick = function () {
        if (this.target == "_blank") {
          window.open(this.url);
        }
        else {
          location.href = this.url;
        }
      };
      cell.target = target;
      cell.url = url;
    }
    else {
      if (this.current == section) {
        cell.className = "menu-current-nolink";
      }
      else {
        cell.className = "menu-nolink";
      }
    }
    cell.onmouseout = function () {
      removeClassName(this, "menu-hover");
      tempThis.preHideAllMenus();
    };
    cell.onmouseover = function () {
      addClassName(this, "menu-hover");
      tempThis.showMenu(this);
    };
    cell.innerHTML = title;
    if (menu) {
      this.createSubMenu(cell, menu);
    }
  }

}

Menu.prototype.createSubMenu = function (pCell, pMenu) {

  var tempThis = this;
  var doc = this.div.ownerDocument || this.div.document;

  var table = doc.createElement("table");
  this.div.appendChild(table);
  pCell.sub = table;
  table.cellSpacing = 0;
  table.subId = this.div.childNodes.length;

  if (table.tBodies.length == 0) {
    var tableBody = doc.createElement("tbody");
    table.appendChild(tableBody);
  }

  for (var i = 0; i < pMenu.length; i++) {
    var item = pMenu[i];
    var title = item[1];
    var url = item[2];
    var target = item[3];
    var menu = item[4];
    var row = tableBody.insertRow(-1);
    var cell = row.insertCell(-1);
    cell.menu = pCell;
    if (url) {
      cell.target = target;
      cell.url = url;
      cell.onclick = function () {
        if (this.target == "_blank") {
          if (window.open(this.url)) {
            removeClassName(this, "menu-hover");
          }
        }
        else {
          location.href = this.url;
        }
      };
    }
    cell.onmouseout = function () {
      removeClassName(this, "menu-hover");
      tempThis.preHideAllMenus();
    };
    cell.onmouseover = function () {
      addClassName(this, "menu-hover");
      tempThis.showMenu(this);
    };
    cell.innerHTML = title;
    if (menu) {
      cell.innerHTML += " " + "&raquo;";
      this.createSubMenu(cell, menu);
    }
  }

}

Menu.prototype.showMenu = function (pCell) {
  clearTimeout(this.timer);
  var pShowList = new Array();
  for (var cell = pCell; cell.menu; cell = cell.menu) {
    pShowList.push(cell.menu.sub.subId);
  }
  if (pCell.sub) {
    var sub = pCell.sub;
    pShowList.push(sub.subId);
    if (pCell.menu) {
      sub.style.left = utilGetAbsoluteLeft(pCell) + pCell.offsetWidth + "px";
      sub.style.top = utilGetAbsoluteTop(pCell) + "px";
    }
    else {
      sub.style.left = utilGetAbsoluteLeft(pCell) + "px";
      sub.style.top = utilGetAbsoluteTop(pCell) + pCell.offsetHeight + 2 + "px";
    }
    sub.style.visibility = "visible";
  }
  this.hideAllMenus(pShowList);
}

Menu.prototype.preHideAllMenus = function () {
  var tempThis = this;
  this.timer = setTimeout(function () {
    tempThis.hideAllMenus();
  }, 300);
}

Menu.prototype.hideAllMenus = function (pShowList) {
  for (var i = 0; i < this.div.childNodes.length; i++) {
    var sub = this.div.childNodes[i];
    if (sub.style.visibility == "visible" && findInArray(pShowList, sub.subId) == -1) {
      sub.style.left = "-1000px";
      sub.style.top = "-1000px";
      sub.style.visibility = "hidden";
    }
  }
}

