//****************************************************************************************************************
// ZSAOTLYTI M E N U ** P R I C E : 229USD+TVA + 400EURO NEGOCIABIL / 1 YEAR LICENSE *****************************
// KOPIRAJGT - 1971-2012 *****************************************************************************************
//****************************************************************************************************************
//****************************************************************************************************************

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=1500;
  this.MenuHorizontalDirection=1;
  
  this.TmpHorizontalDirection=1;
  
  this.ObjectsCache=new Object();
}


ZSMenu.prototype.Show=function() {
  this.TmpHorizontalDirection=this.MenuHorizontalDirection;
  
  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;
};

//****************************************************************************************************************
ZSMenu.prototype.BuildMenuLevels=function(tbl,deep,ParentIdk){
  
  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.onmouseover=ShowMenuReq;
            tdElement.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.onmouseover=ShowMenuReq;
          tdElement.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;
    }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;
      }else{
        MenuProperty=this.getBoxProperty(SubMenuElement);
        SubMenuElement.style.left=ThisProperty.x-MenuProperty.w;
      }
      
      var ScrollPosition=this.getScrollXY();
      if (ThisProperty.y+SubMenuElementProperty.h-ScrollPosition[1]>document.body.clientHeight){
        MenuProperty=this.getBoxProperty(SubMenuElement);
        SubMenuElement.style.top=ThisProperty.y-MenuProperty.h+ThisProperty.h;
      }else{
        SubMenuElement.style.top=ThisProperty.y;
      }
    }  
  }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';
    }
  }
}

//****************************************************************************************************************
ZSMenu.prototype.getScrollXY=function(){
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {//Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {//DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {//IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

