var lastMenuId = 0;
var timer;
var mSheets = new Array();
var currZ = 100;
var mReady = false;

function setClass(obj, cl){
  if (!cl) cl = "";
  obj.className = cl;
}

function setClassById(objid, cl){
  obj = document.getElementById(objid);
  setClass(obj, cl);
}

function changeDisplayById(objId){
  for (c = 0; c < changeDisplayById.arguments.length; c++){
    obj = document.getElementById(changeDisplayById.arguments[c]);
    if (obj.style.display == 'none') obj.style.display = 'block';
    else obj.style.display = 'none';
  }
}

function gotoURL(url){
  if (!url) url = "/";
  if (window.event){
    var src = window.event.srcElement;
    if(src.tagName != 'A'){
      if (window.event.shiftKey) window.open(url);
      else document.location = url;
    }
  } else document.location = url;
}

function getLeftPos(obj){
  var res = 0;
  while (obj){
    res += obj.offsetLeft;
    obj = obj.offsetParent;
  }
  return res;
}

function getTopPos(obj){
  var res = 0;
  while (obj){
    res += obj.offsetTop;
    obj = obj.offsetParent;
  }
  return res;
}

function MenuLink(textVal, linkVal, subVal){
  this.text = textVal;
  this.action = linkVal;
  this.submenu = subVal;
}

function menuHideAll(){
 for (var c in mSheets) mSheets[c].hide();
}

function menuHideTimerSet(){
  timer = window.setTimeout(menuHideAll, 100);
}

function menuHideTimerReset(){
  if (timer) window.clearTimeout(timer);
}

function menuAddLink(textVal, linkVal){
  this.links[this.links.length] = new MenuLink(textVal, linkVal, null);
}

function menuShow(leftVal, topVal){
  this.block.style.left = leftVal;
  this.block.style.top = topVal;
  this.block.style.display = "block";
}

function menuHide(){
  this.hideCh();
  this.block.style.display = "none";
}

function menuFlip(leftVal, topVal){
  var disp = this.block.style.display;
  if (disp == "none") this.show(leftVal, topVal);
  else this.hide();
}

function menuHideCh(){
  for (var c in this.links){
    curLink = this.links[c];
    if (curLink.submenu) curLink.submenu.hide();
  }
}

function menuCreate(path){
  var res = "<table cellpadding=\"0\" cellspacing=\"0\" onmouseout=\"menuHideTimerSet()\" onmouseover=\"menuHideTimerReset()\" class=\"menu-sheet\">";
  var curLink;
  var newPath;
  if (path == null) path = "mSheets[" + this.id + "]";
  for (var c in this.links){
    curLink = this.links[c];
    res += "<tr><td class=\" menu-sh";
    res += "\" onmouseover=\"setClass(this, 'menu-sh-act'); ";
    res += path + ".hideCh()";
    res += "\" onmouseout=\"setClass(this, 'menu-sh')\" onclick=\"gotoURL('"+curLink.action+"')\"><nobr>" + curLink.text + "</nobr></td></tr>";
  }
  res += "</table>";
//  alert(res);
  this.block.innerHTML = res;
}

function MenuSheet(parentObj){
  this.links = new Array();
  this.addLink = menuAddLink;

  this.create = menuCreate;
  this.show = menuShow;
  this.hide = menuHide;
  this.flip = menuFlip;
  this.hideCh = menuHideCh;
  this.id = lastMenuId;
  lastMenuId++;
  this.parent = parentObj;
  this.block = document.createElement("DIV");
  this.block.style.display = "none";
  this.block.style.zIndex = currZ;
  currZ++;
  this.block.className = "menu";
  this.block.id = "ms" + this.id;
  document.body.appendChild(this.block);
}

function mOvr(src,clrOver)
{
  if (!src.contains(event.fromElement)) {
    src.style.cursor = 'hand';
    src.style.backgroundColor = clrOver;
  }
}
function mOut(src,clrIn)
{
  if (!src.contains(event.toElement)) {
    src.style.cursor = 'default';
    src.style.backgroundColor = clrIn;
  }
}

function hideMenu(objVal, numVal){
  if (mReady){
    menuHideTimerSet();
  }
}
