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') && ((src.tagName != 'IMG') || (src.parentElement.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 setImgSrc(idVal, srcVal){
  if (document.images) document.images[idVal].src = ""+ srcVal;
}

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 menuAddSubmenu(textVal, linkVal){
  this.links[this.links.length] = new MenuLink(textVal, linkVal, new MenuSheet(this));
}

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=\"tab-menu-sh\">";
  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=\"blk-menu-sh";
    if (curLink.submenu) res += " blk-menu-arr";
    res += "\" onmouseover=\"setClass(this, 'blk-menu-sh-act";
    if (curLink.submenu) res += " blk-menu-arr-act";    
    res += "'); ";     
    res += path + ".hideCh()";
    if (curLink.submenu){
      newPath = path + ".links[" + c + "].submenu";
      res += "; " + newPath + ".show(getLeftPos(this) + this.offsetWidth, getTopPos(this) - 2)";
      curLink.submenu.create(newPath);      
    }
    res += "\" onmouseout=\"setClass(this, 'blk-menu-sh";
    if (curLink.submenu) res += " blk-menu-arr";    
    res += "')\" 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.addSubmenu = menuAddSubmenu;
  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 = "blk-menu";
  this.block.id = "ms" + this.id; 
  document.body.appendChild(this.block);      
}


//var mImgNames = new Array({row:IMAGES}{IMAGE}{/row:IMAGES});

function showMenu(objVal, numVal){
  if (mReady){
    menuHideAll();
    objVal.className = "item act";
    setImgSrc("m_tit" + numVal, ImgPath  + mImgNames[numVal] + "-act.gif");
  
    mSheets[numVal].show(getLeftPos(objVal) - -10, 79);
    
    menuHideTimerReset();
  }  
}

function hideMenu(objVal, numVal){
  if (mReady){
    menuHideTimerSet();
  
    objVal.className = "item";
    setImgSrc("m_tit" + numVal, ImgPath+ mImgNames[numVal] + ".gif");
  }  
}

