var _rootpath ='http://www.sourcingvision.com/';
//alert(_rootpath);

function RootMenu_Tab(Id,SubmenuId,Name,Href){
  var content;  
  content='';
  content+="<td";
  if (SubmenuId!='') content+=" SubMenuID='"+SubmenuId+"'";
  content+=" id='mimi"+Id+"' class='curs'>";
  content+="<table border='0' width='100%' cellpadding='0' cellspacing='0'><tr>";
  content+="<td><img id='ir"+Id+"' src= '" + _rootpath + "images/menu/"+(htmlDir=='rtl'?'right':'left')+".gif'></td>";
  content+="<td><img id='icon"+Id+"' src= '" + _rootpath + "images/menu/root/"+Id+".gif'></td>";
  content+="<td nowrap class='menu' id='ic"+Id+"' onMouseOver=\"javascript:ov('"+Id+"','Over');\" onMouseOut=\"javascript:ov('"+Id+"','Normal');\" class='menu'><a href='"+ _rootpath + Href+"' class='menulnk'>"+Name+"</a></td>";
  content+="<td><img id='il"+Id+"' src= '" + _rootpath + "images/menu/"+(htmlDir=='rtl'?'left':'right')+".gif'></td>";
  content+="</tr></table>";
  content+="</td>";
  return content;
}

function ov(id,action){
  if (action=='Over'){
    document.getElementById('il'+id).src= _rootpath + 'images/menu/'+(htmlDir=='rtl'?'left':'right')+'_down.gif';
    document.getElementById('icon'+id).src= _rootpath + 'images/menu/root/'+id+'_down.gif';
    document.getElementById('ic'+id).className='menu_over';
    document.getElementById('ir'+id).src= _rootpath + 'images/menu/'+(htmlDir=='rtl'?'right':'left')+'_down.gif';
  }else{
    document.getElementById('il'+id).src= _rootpath + 'images/menu/'+(htmlDir=='rtl'?'left':'right')+'.gif';
    document.getElementById('icon'+id).src= _rootpath + 'images/menu/root/'+id+'.gif';
    document.getElementById('ic'+id).className='menu';
    document.getElementById('ir'+id).src= _rootpath  + 'images/menu/'+(htmlDir=='rtl'?'right':'left')+'.gif';
  }
}
  
function ZSMenu(TableId){
  this.TableId=TableId;
  this.Horizontal=1;
  this.MenuDependences=new Object();
  this.MenuParentArray=new Object();
  this.Timer=0;
  this.CurrentMenuId=0;
  this.MenuHideDelay=1000;
  this.MenuHorizontalDirection=1;
  this.MenuVerticalDirection=1;
  
  this.TmpHorizontalDirection=1;
  this.TmpVerticalDirection=1;

  this.ObjectsCache=new Object();
}


ZSMenu.prototype.Show=function() {
  this.TmpHorizontalDirection=this.MenuHorizontalDirection;
  this.MenuVerticalDirection=this.MenuVerticalDirection;

  this.BuildMenuLevels(this.GetE(this.TableId),0,'');
  for(var key in this.MenuDependences){
    this.MenuParentArray[key]=new Object();
    var ttt=this.MenuDependences[key].substr(1).split(',');
    for (var x=0;x<ttt.length;x++){
      this.MenuParentArray[key][ttt[x]]=1;
    }
    this.MenuParentArray[key][key]=1;
  }
  for(var x in this.MenuParentArray[key]) this.MenuParentArray[key][x]=1;
  
}

ZSMenu.prototype.GetE=function(objID){
  if (!this.ObjectsCache[objID]) this.ObjectsCache[objID]=document.getElementById(objID);
  return this.ObjectsCache[objID];
}

//****************************************************************************************************************
ZSMenu.prototype.getBoxProperty=function(el) {
  var r = { x: el.offsetLeft, y: el.offsetTop };
  if (el.offsetParent){
    var tmp = this.getBoxProperty(el.offsetParent);
    r.x += tmp.x;
    r.y += tmp.y;
  }
  r.w=el.offsetWidth;
  r.h=el.offsetHeight;
  return r;
};


  function makeEventFunc(param1){
    return function(){ShowMenuReqIE(param1)}
  }

  var kintiObj='';

  function ShowMenuReqIE(param){
    clearTimeout(kintiObj.Timer);
    kintiObj.ShowMenu(param);
  }
  
//****************************************************************************************************************
ZSMenu.prototype.BuildMenuLevels=function(tbl,deep,ParentIdk){
  kintiObj=this;
  
  var thisClass =this;
  var ShowMenuReq=function(){
    clearTimeout(thisClass.Timer);
    thisClass.ShowMenu(this);
  }
  
  var HideSubMenuReq=function(){
    eval('Var1_'+thisClass.TableId+'=thisClass;');
    eval('Var2_'+thisClass.TableId+'=this;');
    if (this.Timer==0 || this.Timer==undefined) thisClass.Timer=setTimeout('Var1_'+thisClass.TableId+'.HideSubMenu(Var2_'+thisClass.TableId+')',thisClass.MenuHideDelay);
  }

  for (var r=0;r<tbl.rows.length;r++){
    for (var c=0;c<tbl.rows[r].cells.length;c++){
      tdElement=tbl.rows[r].cells[c];
      if (tdElement.attributes['SubMenuID']){
        this.MenuDependences[tdElement.attributes['SubMenuID'].value]=new Object();
        this.MenuDependences[tdElement.attributes['SubMenuID'].value]=ParentIdk;
        SubMenuElement=this.GetE(tdElement.attributes['SubMenuID'].value);
        if (SubMenuElement){
          if (tdElement.addEventListener) { // for FF and NS and Opera
            tdElement.addEventListener('mouseover',ShowMenuReq,false);
            tdElement.addEventListener('mouseout',HideSubMenuReq,false);
          }else{
            tdElement.attachEvent('onmouseover',makeEventFunc(tdElement));
            tdElement.attachEvent('onmouseout',HideSubMenuReq);
          }
          tdElement.deep=deep;
          this.BuildMenuLevels(SubMenuElement,deep+1,ParentIdk+','+tdElement.attributes['SubMenuID'].value);
        }else{
          alert('Submenu '+tdElement.attributes['SubMenuID'].value+' not exists !!!! ');
        }
      }else{
        tdElement.TableID=tbl.id;
        if (tdElement.addEventListener) { // for FF and NS and Opera
          tdElement.addEventListener('mouseover',ShowMenuReq,false);
          tdElement.addEventListener('mouseout',HideSubMenuReq,false);
        }else{
          tdElement.attachEvent('onmouseover',makeEventFunc(tdElement));
          tdElement.attachEvent('onmouseout',HideSubMenuReq);
        }
      }
    }
  }
}

//****************************************************************************************************************
ZSMenu.prototype.ShowMenu=function(td){
  if (td.attributes['SubMenuID']){
    this.CurrentMenuId=td.attributes['SubMenuID'].value;

    SubMenuElement=this.GetE(td.attributes['SubMenuID'].value);

    var ThisProperty='';
    ThisProperty=this.getBoxProperty(td);
    
      
    //if (this.Horizontal && td.deep==0){
    if (this.Horizontal && td.deep==0){
      SubMenuElement.style.left=ThisProperty.x;
      SubMenuElement.style.top=ThisProperty.y+ThisProperty.h+4;
    }else{
      var SubMenuElementProperty=this.getBoxProperty(SubMenuElement);
      if (ThisProperty.x+ThisProperty.w+SubMenuElementProperty.w>document.body.clientWidth && this.MenuHorizontalDirection==1) this.MenuHorizontalDirection=-1;
      if (this.MenuHorizontalDirection==1){
        SubMenuElement.style.left=ThisProperty.x+ThisProperty.w+1;
      }else{
        MenuProperty=this.getBoxProperty(SubMenuElement);
        SubMenuElement.style.left=ThisProperty.x-MenuProperty.w;
      }
      
      
      if (ThisProperty.y+SubMenuElementProperty.h>document.body.clientHeight && this.MenuVerticalDirection==1) this.MenuVerticalDirection=-1;
      if (ThisProperty.y-SubMenuElementProperty.h<0) this.MenuVerticalDirection=1;
      
      if (this.MenuVerticalDirection==1){
        SubMenuElement.style.top=ThisProperty.y;
      }else{
        MenuProperty=this.getBoxProperty(SubMenuElement);
        SubMenuElement.style.top=ThisProperty.y-MenuProperty.h;
      }
    }  
  }else{
    this.CurrentMenuId=td.TableID;
  }
  this.UpdateMenu();
}

//****************************************************************************************************************
ZSMenu.prototype.HideSubMenu=function(SubMenuElement){
  this.TmpHorizontalDirection=this.MenuHorizontalDirection;
  this.MenuVerticalDirection=this.MenuVerticalDirection;

  this.TmpVerticalDirection=1;
  this.CurrentMenuId=0;
  this.UpdateMenu();
}

//****************************************************************************************************************
ZSMenu.prototype.UpdateMenu=function(){
  var aktivmenuk='';
  if (this.CurrentMenuId!=0 && this.MenuParentArray[this.CurrentMenuId]) aktivmenuk=this.MenuParentArray[this.CurrentMenuId];
  for(var key in this.MenuParentArray){
    if (aktivmenuk[key]==1) {
      this.GetE(key).style.visibility='visible';
    }else{
      this.GetE(key).style.visibility='hidden';
    }
  }
}