﻿// JScript File

    function element()
    {
     this.opening=false;
     this.closing=false;
     this.endw="";
     this.endh="";
     this.id="";
     this.inout=0;
     this.top=0;
     this.left=0;
     this.width=0;
     this.height=0;
     this.opened=false;
     this.parent=null;
     this.closed=true;
     this.forcedclose=false;
    }
    
    function findMenu(id)
    {
     var k=0;
     for(k=0;k<window.opening.length;k++)
     {
      if(window.opening[k].id==id) return window.opening[k];
     }
     return null;
    }
    function childOpen(id)
    {
     var k=0;
     for(k=0;k<window.opening.length;k++)
     {
      if(window.opening[k].parent!=null)
      if(window.opening[k].parent.id==id)
      if(window.opening[k].opened) return true;
     }
     return false;
    }
    function closeSiblings(id)
    {
     var k=0;
     var par=findMenu(id).parent;
     for(k=0;k<window.opening.length;k++)
     {
      if(window.opening[k].parent!=null && par!=null)
      if(window.opening[k].parent.id==par.id)
      if(window.opening[k].id!=id) startClose(window.opening[k].id);
      if(window.opening[k].parent==null && par==null)
      if(window.opening[k].id!=id) startClose(window.opening[k].id);
      if(window.opening[k].parent!=null) if (window.opening[k].parent.closing) startClose(window.opening[k].id);
     }
     return false;
    }
     function closeAll()
    {
     var k=0;
     for(k=0;k<window.opening.length;k++)
     {
     if(window.opening[k].opened) window.opening[k].forcedclose=true;
      startClose(window.opening[k].id);
     }
    }
    
    function startOpen(e)
    {
    var ev;
    if (e==null) ev=event; else ev=e;
    var el=ev.target;
    if (el==null) el=ev.srcElement;
    var item=el.name;
if (findMenu(item)==null) return;
 if(findMenu(item).forcedclose) return;
  if(!findMenu(item).opened)
  {
  closeSiblings(item);
    document.getElementById(item+"_div").style.border="solid 2px";
    document.getElementById(item+"_div").style.visibility="visible";
    document.getElementById(item+"_div1").style.visibility="hidden";
    findMenu(item).opening=true;
    findMenu(item).closed=false;
    findMenu(item).closing=false;
    findMenu(item).opening=true;
   }
    }
    
    function startClose(item)
    {
    if (findMenu(item)==null) return;
    if (!findMenu(item).closed)
    {
     findMenu(item).opened=false;
     document.getElementById(item+"_div").style.border="solid 2px";
     document.getElementById(item+"_div").style.visibility="visible";
     //document.getElementById(item+"_div").style.overflow="hidden";
     document.getElementById(item+"_div1").style.visibility="hidden";
     findMenu(item).opening=false;
     findMenu(item).closing=true;
    }    

    }
    
        

function getMouseXY(e) 
{
  if (IE) { 
    window.mouseX = event.clientX + document.body.scrollLeft;
    window.mouseY = event.clientY + document.body.scrollTop;
  } else {  
    window.mouseX = e.pageX;
    window.mouseY = e.pageY;
  }  
  if (window.mouseX < 0){window.mouseX = 0;}
  if (window.mouseY < 0){window.mouseY = 0;}  
  
 for (k=0;k<window.opening.length;k++)
 {
  var obj=window.opening[k];
  if(!underMouse(obj)) 
  {
   if(!childOpen(obj.id)) 
   {
    startClose(window.opening[k].id);
   }
  }else
  {
  }
 }
  return true;
}

function underMouse(obj)
{
  
  if(window.mouseX>(obj.left-parseInt(window.menuOffsetLeft)-5) && window.mouseX<(obj.left+10+obj.width+parseInt(window.menuOffsetLeft)) && window.mouseY>(obj.top-parseInt(window.menuHeight)-10) && window.mouseY<(obj.top+obj.height+parseInt(window.menuHeight)+30))
  {
   return true;
  }
 
  return false;
}
   
    function openDiv()
    {
    var k;
     for (k=0;k<window.opening.length;k++)
     {
     if (window.opening[k].opening==true)
     {
      var w=parseInt(document.getElementById(window.opening[k].id+"_div").style.width);
      var h=parseInt(document.getElementById(window.opening[k].id+"_div").style.height);
      if (w<=window.opening[k].endw-20) document.getElementById(window.opening[k].id+"_div").style.width=(w+20)+ "px";
      if (h<=window.opening[k].endh-20) document.getElementById(window.opening[k].id+"_div").style.height=(h+20)+ "px";
      if (h>=window.opening[k].endh-20 && w>=window.opening[k].endw-20)
      {
      document.getElementById(window.opening[k].id+"_div").style.width=(window.opening[k].endw+5)+"px";
      document.getElementById(window.opening[k].id+"_div").style.height=(window.opening[k].endh+5)+"px";
//      document.getElementById(window.opening[k].id+"_div1").style.position="relative";
//       document.getElementById(window.opening[k].id+"_div1").style.left="-40px";
       document.getElementById(window.opening[k].id+"_div1").style.width=(window.opening[k].width) + "px";
       document.getElementById(window.opening[k].id+"_div1").style.height=(window.opening[k].height) + "px";
       document.getElementById(window.opening[k].id+"_div").style.border=window.menuContainerBorder;
      window.opening[k].opening=false;
      window.opening[k].opened=true;
      document.getElementById(window.opening[k].id+"_div").style.overflow="visible";
      document.getElementById(window.opening[k].id+"_div1").style.visibility="visible";
      }
     }
     }
     setTimeout("openDiv()",20);
    }
    
    function closeDiv()
    {
    var k;
     for (k=0;k<window.opening.length;k++)
     {
     if (window.opening[k].closing==true)
     {
      var w=parseInt(document.getElementById(window.opening[k].id+"_div").style.width);
      var h=parseInt(document.getElementById(window.opening[k].id+"_div").style.height);
      document.getElementById(window.opening[k].id+"_div").style.zIndex=99;
      document.getElementById(window.opening[k].id+"_div1").style.zIndex=99;
      if (h>20) document.getElementById(window.opening[k].id+"_div").style.height=(h-20)+ "px";
      if (w>20) document.getElementById(window.opening[k].id+"_div").style.width=(w-20) + "px";
      if (h<=20 && w<=20)
      {
      document.getElementById(window.opening[k].id+"_div").style.visibility="hidden";
      window.opening[k].closing=false;
      window.opening[k].closed=true;
      window.opening[k].forcedclose=false;
      } 
     }
     }
     setTimeout("closeDiv()",20);
    }
    
    window.firstload=true;
    function load_menu()
    {
    window.opening=new Array();
    window.mouseX=0;
    window.mouseY=0;
    window.IE = document.all?true:false
    if (!window.IE) document.captureEvents(Event.MOUSEMOVE)
    document.onmousemove = getMouseXY;
    window.menuWidth="115px";
    window.menuChildWidth="130px";
    window.menuHeight="20px"
    window.menuOffsetLeft="20";
    window.menuOffsetTop="20";
    window.menuBackColor="#d4d1fe";
    window.menuBackColorMore="#d4d1fe";
    window.menuBorder="none 0px Black";
    window.menuContainerBorder="solid 2px Blue";
    var root=document.getElementById("mainMenu");
    //var maindiv=document.getElementById("mainDiv");
    //alert(maindiv.offsetLeft + "#" + maindiv.offsetTop);
    setElements(root,0);
    if (window.firstload) 
    {
     setTimeout("closeDiv()",20);
     setTimeout("openDiv()",20);
     document.body.onresize=function(){scrollEvent();load_menu();};
     window.onresize=function(){scrollEvent();load_menu();};
    }
    window.firstload=false;
    }
   
    function setElements(root,depth,toplold,toptold)
    {
    var oh=0;
     var lis = root.childNodes;
     var k=0;
     var topt;
     var topl;
     //root.style.zIndex=depth+50;
     if (depth==0)
     {
//      var parent=root.parentNode;
//      var offsl=0;
//      var offst=0;
//      var dep=0;
//      while(parent!=null)
//      {
//       dep+=1;
//       parent=parent.parentNode
//      }
//      parent=root;
//      var k=0;
//      for(k=0;k<dep;k++)
//      {
//       //if(parent.tagName=="TABLE")
//       //{
//        if(parent.offsetTop!=null)offst+=parent.offsetTop;
//        if(parent.offsetLeft!=null)offsl+=parent.offsetLeft;
//        alert(parent.tagName + "l:" + parent.offsetLeft + "t:" + parent.offsetTop);
//       //}
//       parent=parent.parentNode
//      }
      topt=root.offsetTop+parseInt(window.menuHeight);
      topl=root.offsetLeft;
      root.style.position="relative";
      root.style.zIndex=99;
      //root.style.top=0;
      //root.style.left=0;
     }else
     if(depth==1)
     {
      topt=toptold+parseInt(window.menuOffsetTop);
      topl=toplold+parseInt(window.menuOffsetLeft);
     }else
     {
      topt=toptold+parseInt(window.menuOffsetTop);
      topl=toplold+parseInt(window.menuOffsetLeft);
     }

     //alert(topl + "," + topt);
//     root.style.top=topt + "px";
//     root.style.left=topl + "px";
     for (k=0;k<lis.length;k++)
     {
     if (lis[k].tagName=="LI")
     {
      window.IE = document.all?true:false
      var ch=lis[k].childNodes;
      var nam="";
      if (depth==0) nam="menu"; else nam=root.name + "_";
      lis[k].id=nam+(k);
      lis[k].name=nam+(k);
      //lis[k].style.float="left";
      if (depth==0) lis[k].style.width=window.menuWidth; else lis[k].style.width=window.menuChildWidth;
       if(lis[k].style.height==0) lis[k].style.height=window.menuHeight;
      //lis[k].style.display="block";
      if(lis[k].style.border==0) lis[k].style.border=window.menuBorder;
      lis[k].style.textDecoration="none";//topt;       
      lis[k].style.position="absolute";
      if(depth==0)
      {
      lis[k].style.left=k*parseInt(window.menuWidth);//topl;
      lis[k].style.top=0;//topt;       
      }else
      {
       topt+=oh;
       //lis[k].style.position="relative";
       lis[k].style.left=-root.offsetLeft;
       lis[k].style.top=oh;       
      }
      lis[k].style.zIndex=depth+50;
      oh+=parseInt(lis[k].style.height);
      var j=0;
      var one=false;
      for (j=0;j<ch.length;j++)
      {
       if(ch[j].tagName=="A")
       {
        ch[j].onclick=function(){closeAll();};
        ch[j].name=lis[k].name;
        ch[j].onmouseover=function(e){startOpen(e);};
        //ch[j].style.position="absolute";
        //ch[j].style.left=0;//topl;
        //ch[j].style.top=0;//topt;
        ch[j].style.zIndex=depth+50;
        ch[j].style.textDecoration="none";//topt;       
       }
       lis[k].style.backgroundColor=window.menuBackColor;
       if(ch[j].tagName=="DIV")
       {
       lis[k].style.backgroundColor=window.menuBackColorMore;
        ch[j].style.backgroundColor=window.menuBackColor;         
        ch[j].style.position="absolute";
            ch[j].style.left=parseInt(window.menuOffsetLeft);//topl;
            ch[j].style.top=parseInt(window.menuOffsetTop);;//topt;
        ch[j].style.zIndex=depth+50;

        ch[j].id=lis[k].name+"_div";
        ch[j].name=lis[k].name;
        if (!one)
        {
         one=true;
         lis[k].onmouseover=function(e){startOpen(e);};
         var elem=new element();
         elem.id=lis[k].name;
         elem.parent=findMenu(root.name);
         window.opening[window.opening.length]=elem;
        }
        ch[j].style.visibility="hidden";
        var ch1=ch[j].childNodes;
        var l=0;
        for (l=0;l<ch1.length;l++)
        {
         if(ch1[l].tagName=="DIV")
         {
          ch1[l].id=lis[k].name+"_div1";
          ch1[l].name=lis[k].name;
            ch1[l].style.position="absolute";
            ch1[l].style.top=0;//topt;
            ch1[l].style.left=0;//topl;
            ch1[l].style.zIndex=depth+50;


          
          ch1[l].style.visibility="hidden";         
          ch1[l].style.width=window.menuChildWidth;
          var ch2=ch1[l].childNodes;
          var m=0;
          for (m=0;m<ch2.length;m++)
          {
           if(ch2[m].tagName=="UL")
           {
            //ch2[m].style.listSyleType="none";
            ch2[m].name=lis[k].name;
            ch2[m].id=lis[k].name + "_container";
            ch2[m].style.position="absolute";
            ch2[m].style.left="0px";//topl;
            ch2[m].style.top="0px";//topt;
            ch2[m].style.zIndex=depth+50;
            //alert("depth " + depth + ",pass " + k + ":" + topl + "," + topt);
            var ch3=ch2[l].childNodes;
            var n=0;
            var w=0;
            var h=0;
            for (n=0;n<ch3.length;n++)
            {
             if(ch3[n].tagName=="LI")
             {
              //ch3[n].style.listSyleType="none";
              //ch3[n].style.listSyleImage="none";
              //ch3[n].style.float="left";
              ch3[n].style.width=window.menuChildWidth;
              if(ch3[n].style.height==0) ch3[n].style.height=window.menuHeight;
              //ch3[n].style.display="block";
              if(w<ch3[n].clientWidth) w=parseInt(ch3[n].style.width);
//              ch3[n].style.position="absolute";
//              ch3[n].style.left=topl;
//              ch3[n].style.top=topt+h;
              h+=parseInt(ch3[n].style.height);
             }
            }
            findMenu(lis[k].name).width=w;
            findMenu(lis[k].name).height=h;
            findMenu(lis[k].name).endw=w;
            findMenu(lis[k].name).endh=h;
            //ch[j].style.position="relative";
            //ch[j].style.left=window.menuOffsetLeft +"px";
            //ch[j].style.top=window.menuOffsetTop +"px";
            findMenu(lis[k].name).top=topt;
            findMenu(lis[k].name).left=topl;
            //alert("depth " + depth + ",pass " + k + ":" + topl + "," + topt + "," + w + "," + h);

            document.getElementById(lis[k].name+"_div").style.width="0px";
            document.getElementById(lis[k].name+"_div").style.height="0px";
            setElements(ch2[m],depth+1,topl,topt-15);
           }
          }
         }
        }        
       }
      }
//      var t=lis[k].offsetTop;
//      var l=lis[k].offsetLeft;
//      lis[k].style.position="absolute";
//       lis[k].style.top=t;
//       lis[k].style.left=l;
if (depth==0) topl+=parseInt(lis[k].style.width);
     }
     }
    }
