//Author: Younes Bouab
//Date:   03-05-01
//Title:  Menu Generator: Menu.js
//Copyright: Younes Bouab 2001
//Technical Support: bouaby@SUPEReDITION.com
//Code Modifications:  David Schrey 2004  dave@grand-i-design.com
////////////////////////////////////////////
//DO NOT CHANGE ANYTHING IN THIS FILE!
//"Config.js" is the file you can edit
////////////////////////////////////////////
//Dreamweaver Show/Hide/Obj Layer functions
/*******************************************/
function showHideLayers() {
  var i,p,v,obj,args=showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
    obj.visibility=v; }
}
function findObj(n, d) {
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}
function changeProp(objName,x,theProp,theValue) {
  var obj = findObj(objName);
  if (obj && (theProp.indexOf("style.")==-1 || obj.style)) eval("obj."+theProp+"='"+theValue+"'");
}

function reloadPage(init) {
  if (init==true) with (navigator)
  {
    if ((appName=="Netscape")&&(parseInt(appVersion)==4))
    {
      document.pgW=innerWidth;
      document.pgH=innerHeight;
      onresize=reloadPage;
    }
  }
  else if (innerWidth!=document.pgW || innerHeight!=document.pgH) {location.reload();}
}
reloadPage(true);

// Initialize
/******************************************/
//Menu Array
Mn =new Array(); //Mn[x][0]=INDEX; [x][1]=PARENT; [x][2]=Text; [x][3]=Image; [x][4]=Roll Image; [x][5]=URL
var i=0;
var ii=0; //used in validation of Menu Array
var MaxIdx=0;

//Default Layer Frame for IE and NS6.X
var Layer="\"<div id='\"+IDLABEL+\"' class='CBORDER' Style='Position:absolute; visibility:\"+VIS+\"; left:\"+(LEFT+NS6X)+\"px; top:\"+(TOP+NS6Y)+\"px; width:\"+(WIDTH)+\"px; height:\"+(HEIGHT)+\"px; background-color:\"+MyLyrCol+\"; z-index:\"+ZINDEX+\"; URL(\"+MyURL+\")' \"";
var Layer_End="</div>";

LAYER= new Array();//Menu string FOR EACH ITEM
M_OVROUT=new Array();//HOLDS THE LAYERIDs that should have OnMouseOver && OnMouseOut for each menu
TOPLEFT=new Array(); //holds Top and left measures for each menu item
ParKidID=new Array();

//Layers Variables
var IDLABEL="";
var VIS="visible"; //default
var ZINDEX=0;
var MyURL="";

//Position function variables
var TopParent=TOP;
var LeftParent=LEFT;
var ORGWIDTH=WIDTH;
ParKidCnt=new Array(); //Counts the children of each Main Menu
var Main_Count=0; //Counts the number of the Main menu 

//Validation Variable
Err=false; 
SortErr=false; 

//NS CORRECTIONS
var NS_Table_Width_Corr=0;
var NS_Table_Heigth_Corr=0;
var NS6X=0; 
var NS6Y=0; if(!document.all && document.getElementById) {NS6X=NS_LEFT; NS6Y=NS_TOP;} 

//Add Menu item to Menu Array
/******************************************/
function AddMenu(ID, P_ID, Text, Image, RImg, URL)
{ 
  //Validation
  var valid = "0123456789"; 
  var temp; 
  if(ID.length==0) {alert("Menu #"+(ii+1)+" does not have an ID"); Err=true;}
  //check if ID is composed 
  else 
  { 
    //Checking if number
    for(var j=0;j<ID.length;j++)
    {
      temp=ID.substring(j,j+1);
      if(valid.indexOf(temp)==-1) {alert("The ID of Menu #"+(ii+1)+" must be numeric"); Err=true;}
    }
  }
  if(P_ID.length==0) {alert("Menu #"+(ii+1)+" does not have a P_ID"); Err=true;}
  //check if P_ID is composed of only numbers and dashes "-"
  else 
  { 
    //Checking if number
    for(var j=0;j<P_ID.length;j++)
    {
      temp=P_ID.substring(j,j+1);
      if(valid.indexOf(temp)==-1) {alert("The P_ID of the Menu #"+(ii+1)+" must be numeric"); Err=true;}
    }
  }
  if(Text.length==0 && Image.length==0) {alert("Menu #"+(ii+1)+" Must have an image or text assigned"); Err=true;}
  ii++;
   
  Mn[i] = new Array();    
  if(!Err)
  {
     Mn[i][0]=ID;
     Mn[i][1]=P_ID;
     Mn[i][2]=Text;
     Mn[i][3]=Image;
     Mn[i][4]=RImg;
     Mn[i][5]=URL;
     i++; 
     MaxIdx = i;
  }  
  //Reinitialze Err Value
  Err=false;
}

//Build Menus
/******************************************/
function Build()
{
    //Browser Check
    NS4XCorrection()
    
    //Initilize Position Variables
    for (var j=0;j<MaxIdx;j++)
    { 
      TOPLEFT[j]=new Array();
      ParKidCnt[j]=0;
      M_OVROUT[j]=new Array(); 
      ParKidID[j]=new Array();  
      TOPLEFT[j][0]=0;
      TOPLEFT[j][1]=0;
    }   
 
   //Sort and check menu for errors
   if(Sort==1) {Sorting();}

   if(!SortErr)
   {  
    //Build CSS: Layer Border
    if (LB_Size!="")
    {document.writeln("<Style type=\"text/css\">");
     document.writeln(".CBORDER {");
     document.writeln(" width:"+WIDTH+"px;"); 
     document.writeln(" heigth:"+HEIGHT+"px;");  
     document.writeln(" border: "+LB_Style+" "+LB_Color+" "+LB_Size+"px;"); 
     document.writeln("}\n</style>\n");
    }
          
   //Build each Menu
   for (i=0; i < MaxIdx;i++)
   {
     //Calculates Main Positions, visibility, and determines ID
     CalcLayerVariables(i); 

     //Global Properties
     var MyLyrCol=LayerColor;
     var MyLyrRCol=LayerRollColor;
     //Parent Color
     if(Mn[i][0]==Mn[i][1])
     {
        //BG ROLL Color 
        if (Main_LayerColor!="" )
        {
            MyLyrCol=Main_LayerColor;
        }            
     }     
     //Layer Code  
     LAYER[i]=eval(Layer);           
   }

   //Add samelevel mouse events
   SameLevelMouseEvents(); 

   //Build
   for (var j=0;j<MaxIdx;j++)
   {
     var MyF=FONT;
     var MyFCol=FONTCOLOR;
     var MyFSz=FONTSIZE;
     var MyFSty=FONTSTYLE;
     var MySTARTCHAR=START_CHAR;

     //Parent/Children FONT Properties
     if(Mn[j][0]==Mn[j][1]) //This is a main menu item
     {   
        if(Main_FONT!=""){ MyF=Main_FONT; }
        if(Main_FONTCOLOR!=""){ MyFCol=Main_FONTCOLOR; }
        if(Main_FONTSIZE!=""){ MyFSz=Main_FONTSIZE; }
        if(Main_FONTSTYLE!=""){ MyFSty=Main_FONTSTYLE; }
        if(Main_START_CHAR!=""){ MySTARTCHAR=Main_START_CHAR; }
     }

     var M_OVR="";
     var M_OUT="";
     for (var jj=0;jj<MaxIdx;jj++)
     {
       if(M_OVROUT[j][jj]!=null)
       {
          M_OVR=M_OVR+"showHideLayers('"+M_OVROUT[j][jj]+"','','show');";
          M_OUT=M_OUT+"showHideLayers('"+M_OVROUT[j][jj]+"','','hide');";
       } 
       else
       { 
          M_OVR=M_OVR+"PathRoad("+j+",1)";           
          M_OUT=M_OUT+"PathRoad("+j+",0)";          
          break;
       } 
     } 

     //Linking the entire layer area
     document.write(LAYER[j]+" onMouseOver=\""+M_OVR+"\" onMouseOut=\""+M_OUT+"\"");
     if(Mn[j][5]!="") //{document.write(" class=\"location.href='"+Mn[j][5]+"';\"");}
     {document.write(" onClick=\"location.href='"+Mn[j][5]+"';\"");}
     document.write(" >");     
     
     var LINK="";      
     //display link if any and setup rollover image if any
     if(Mn[j][5]!="")
     {   
        LINK="<A href=\""+Mn[j][5]+"\">";
     }
     
     //Image Code
     var ImageCode="";
     if(Mn[j][3]!="")
     {
       ImageCode=ImageCode+"<IMG Name='Image"+Mn[j][0]+"' SRC='"+Mn[j][3]+"' BORDER=0>";  
     } 
     
     //Fonts if any 
     var F_PROP="";  
     if(MyF!=""){F_PROP=" Type='"+MyF+"'";}
     if(MyFSz!=""){F_PROP=F_PROP+" size='"+MyFSz+"'";}
     if(MyFCol!=""){F_PROP=F_PROP+" color='"+MyFCol+"'";}
     if(F_PROP!="" ){F_PROP="<FONT "+F_PROP+">";}
     if(MyFSty!=""){F_PROP="<"+MyFSty+">"+F_PROP;}

     //Display IMAGE  and TEXT
     document.write("<table border='0' WIDTH='100%' height='100%' cellpadding='0' cellspacing='0'><tr>");     

     if(ImageCode!="")
     {
       document.write("<td ");
       if(HALIGN !=""){document.write(" align='"+HALIGN+"' ");}
       if(VALIGN !=""){document.write(" valign='"+VALIGN+"' ");}
       document.write(">");

       if(LINK!="") {document.write(LINK);}
       document.write(ImageCode);
       if(LINK!=""){document.write("</a>");}
       document.write("</td>"); 
     }

     if(Mn[j][2]!="")
     {
       document.write("<td ");
       if(HALIGN !=""){document.write("  align='"+HALIGN+"' ");}
       if(VALIGN !=""){document.write(" valign='"+VALIGN+"' ");}
       document.write(">");

       if(F_PROP!="") {document.write(F_PROP);}
       document.write(MySTARTCHAR);
       if(LINK!="") {document.write(LINK);} 
       document.write(Mn[j][2]);       
       if(LINK!=""){document.write("</a>");}
       if(F_PROP!="")  {document.write("</FONT>");} 
       if(MyFSty!="") {document.write("</"+MyFSty+">");}
       document.write("</td>"); 
     }
     document.write("</tr></table>");
     //close layer
     document.writeln(Layer_End);
   }
  }   
}

//Sort in-case not ordered & check if every menu parent has a matching index
/***************************************************************************/
function Sorting()
{
  //Place all Parents before children
  for (i=0; i<(MaxIdx-1);i++)
  { 
    TheParent=Mn[i][1];    
    for (j=(i+1); j<MaxIdx; j++)
    { 
       if(Mn[j][0]==TheParent)    
       {              
           for(var f=0;f<6;f++)
           {           
             temp=Mn[i][f];               
             Mn[i][f]=Mn[j][f];
             Mn[j][f]=temp;  
           }
           i=0;   
           break;           
       }
    } 
  }
}

//Netscape 4.X Correction 
/******************************************/
function NS4XCorrection()
{
  if(document.layers)
  {
   Layer="\"<layer class='CBORDER' id='\"+IDLABEL+\"' position='absolute' visibility='\"+VIS+\"' left='\"+LEFT+\"' top='\"+TOP+\"' width='\"+WIDTH+\"' height='\"+HEIGHT+\"' bgcolor='\"+MyLyrCol+\"' z-index='\"+ZINDEX+\"'\" ";
   Layer_End="</layer>";
  }
  NS_Table_Width_Corr=5;
  NS_Table_Heigth_Corr=5;
}

function NS4ImageCorrect(Index)
{
  var NS4FIX="";
  if(document.layers)
  {
     return "layers.Layer"+Mn[Index][0]+".document.";
  }
  return ""; 
}  

/******************************************/
//Calculates Menu Item Position                            
/******************************************/
function CalcLayerVariables(Index)
{
       var ID= Mn[Index][0];
       var P_ID= Mn[Index][1];
       //if Main Parent   
       if (ID==P_ID)
       {
          //Horiz Menu
          if(MENU_TYPE==1)
          {
            TOP=TopParent;
            LEFT=(Main_Count * MP_WIDTH)+ LeftParent - (LB_Size*Main_Count);
          }
          else  //Vert Menu
          {
            TOP=(Main_Count * MP_HEIGHT)+TopParent - (LB_Size*Main_Count);
            LEFT=LeftParent;  
          }
          TOPLEFT[Index][0]=TOP;        
          TOPLEFT[Index][1]=LEFT;                   
          IDLABEL="Layer"+Mn[i][0];        
          VIS="visible";
          ZINDEX=100; //on top of the first 99 layers in the page!
          WIDTH=((+ORGWIDTH)-(+LB_Size));

          Main_Count++;                
       }  
       else  //Child-Find Parent
       {
          ChildofAParent=false;
          var P_Idx=0;
          for (var j=0;j<MaxIdx;j++)
          {
            if (Mn[j][0]== P_ID)
            {
               //collecting Children of Parents
               for (var g=0;g<MaxIdx;g++)
               {
                 if(ParKidID[j][g]==null)
                 { ParKidID[j][g]=IDLABEL;
                   break;
                 }
               } 
               if(Mn[j][0]==Mn[j][1]) 
               {ChildofAParent=true;}
               P_Idx=j; 
               break;
            }
          }
		  ParKidCnt[P_Idx] = ParKidCnt[P_Idx] + 1;
          //if child of Main Parent & Menu is Horiz                      
          if(ChildofAParent && MENU_TYPE==1)
          { 
             TOP=TopParent+(ParKidCnt[P_Idx] * HEIGHT) + M_TOP - ((ParKidCnt[P_Idx])*LB_Size);
             LEFT=TOPLEFT[P_Idx][1] + M_LEFT;
             ZINDEX=101; //on top of the first 100 layers in the page!
          }
          // Menu Type is Vert OR NOT child of Main 
          else
          {
             TOP=(TOPLEFT[P_Idx][0])+((ParKidCnt[P_Idx]-1) * HEIGHT) - ((ParKidCnt[P_Idx]-1)*LB_Size) + C_TOP;
             LEFT=(WIDTH + TOPLEFT[P_Idx][1]) + C_LEFT - LB_Size;
             if(ChildofAParent){ZINDEX=101;}else{ZINDEX=102;} //on top of the first 100 layers in the page,100 is Main Parent and 101 is Parent
          }
          VIS="hidden";
          IDLABEL="Layer"+Mn[Index][0];
          TOPLEFT[Index][0]=TOP;
          TOPLEFT[Index][1]=LEFT;
          WIDTH=((+ORGWIDTH)-(+LB_Size));
       }
}
/******************************************/
//Gens onMouseOver Event for same level layers
/******************************************/
function SameLevelMouseEvents()
{
  //1 showHide Main Parent children
  for(var u=0;u<MaxIdx;u++)
  {
     //if Main Parent
     if (Mn[u][0]==Mn[u][1])
     {
          for(var y=0;y<MaxIdx;y++)
          {
             //if not the one being tested and has the same parent
             if(y!=u && (Mn[y][1]==Mn[u][0]))
             {
               for(var z=0;z<MaxIdx;z++) {if(M_OVROUT[u][z]==null) {M_OVROUT[u][z]="Layer"+Mn[y][0];break;}}
             }
          }
      }
   }
   //2 show hide same Parent menu items
   for(var u=0;u<MaxIdx;u++)
   {
      if (Mn[u][1]!=Mn[u][0])
      {
         for(var z=0;z<MaxIdx;z++) {if(M_OVROUT[u][z]==null) {M_OVROUT[u][z]="Layer"+Mn[u][0];break;}}
         for(var y=0;y<MaxIdx;y++)
          {
             //if not the one being tested and has the same parent and bot the parent menu
             if(y!=u && (Mn[y][1]==Mn[u][1]) && (Mn[y][0]!=Mn[y][1]) )
             {
                for(var z=0;z<MaxIdx;z++) {if(M_OVROUT[u][z]==null) {M_OVROUT[u][z]="Layer"+Mn[y][0];break;}}
             }
          }
      }
   }
   //3 show hide children of non Main Parent
   var same="";
   for(var u=0;u<MaxIdx;u++)
   {
       if (Mn[u][1]!=Mn[u][0])
       {
          for(var y=0;y<MaxIdx;y++)
          {
             if ((Mn[u][0]==Mn[y][1]) && (Mn[y][0]!=Mn[y][1]) && y!=u)
             {
                for(var z=0;z<MaxIdx;z++)
                {if(M_OVROUT[u][z]==null) {M_OVROUT[u][z]="Layer"+Mn[y][0];same=z;break;}}
                //Pass Parent's show hide to the children
                for(var z=0;z<MaxIdx;z++)
                {
                  if(M_OVROUT[u][z]!=null)
                  {
                    for(var x=0;x<MaxIdx;x++)
                    {
                      if(M_OVROUT[y][x]==null) {M_OVROUT[y][x]=M_OVROUT[u][z];break;}
                    }
                  }
                  else {break;}
                }
             }
          }
       }
   }
}
/******************************/
//PathColor
/******************************/
function PathRoad(Parent,flag)
{
  AtStart=false;
  while (!AtStart)
  {
     //change both layer color and image
     if(flag==1) //Roll
     {
       //Layer's Image: Browser Check
       if(Main_LayerRollColor!="" && Mn[Parent][0]==Mn[Parent][1]) {MyLyrRCol=Main_LayerRollColor;} else {MyLyrRCol=LayerRollColor;}
       if(Mn[Parent][4]!="")
       {
         eval("document."+NS4ImageCorrect(Parent)+"images.Image"+Mn[Parent][0]+".src='"+Mn[Parent][4]+"'");
       }

       //Layer Color: Browser Check
       if(document.layers)
       {
         eval("changeProp('Layer"+Mn[Parent][0]+"','','document.bgColor','"+MyLyrRCol+"','LAYER')");
       }
       else  if(document.getElementById || document.all)
       {

         eval("changeProp('Layer"+Mn[Parent][0]+"','','style.backgroundColor','"+MyLyrRCol+"','DIV')");
       }
     }
     else //Origin Image and Color
     {
       //Layer's Image: Browser Check
       if(Main_LayerColor!="" && Mn[Parent][0]==Mn[Parent][1]) {MyLyrCol=Main_LayerColor;} else {MyLyrCol=LayerColor;}
       if(Mn[Parent][3]!="")
       {
         eval("document."+NS4ImageCorrect(Parent)+"images.Image"+Mn[Parent][0]+".src='"+Mn[Parent][3]+"'");
       }
       //Layer Color: Browser Check
       if(document.layers){eval("changeProp('Layer"+Mn[Parent][0]+"','','document.bgColor','"+MyLyrCol+"','LAYER')");}
       else if(document.getElementById || document.all){eval("changeProp('Layer"+Mn[Parent][0]+"','','style.backgroundColor','"+MyLyrCol+"','DIV')");}
     }
     if(Mn[Parent][0] == Mn[Parent][1])
     {
       AtStart=true;
     }
     Parent=IDXof(Mn[Parent][1]);
  }
}
//Find the index of the Menu element with Parent: P
function IDXof(P){
 for(var j=0;j<MaxIdx;j++){
   if(Mn[j][0]==P){   return (j);  }
 }
}
