//Author: Younes Bouab
//
//Date:   17-05-01
//
//Title:  xMenu Generator: xMenu.js
//
//Version: 1.1 >> Supports Frames
//
//Copyright: Younes Bouab 2001 
//
//Technical Support: bouaby@SUPEReDITION.com
////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
//Copyright (c) 2002 Younes Bouab (www.SUPEReDITION.com) Version 1.0
//
//Eperience the DHTML xMenu - Get it at www.SUPEReDITION.com
//
//This script can be used freely as long as all copyright messages are intact.
//
//xMenu HomePage: http://www.superedition.com/Main.asp?Page=Tutorials&query=Javascript
//////////////////////////////////////////////////////////////////////////////////////
//xMenu Configuration File
/////////////////////////////////////////////////
/////You can change the value of a variable 
/////below or turn it off by making it equal "" 
/////to suit your needs, but you should not
/////delete any variable.
///////////////////////////////////////////////

/*******************************************/
//Dreamweaver Show/Hide/Obj xLayer functions
/*******************************************/
function showHideLayers() { 
  var xi,p,v,obj,args=showHideLayers.arguments;
  for (xi=0; xi<(args.length-2); xi+=3) if ((obj=findObj(args[xi]))!=null) { v=args[xi+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
    obj.visibility=v; }
}
function findObj(n, d) { 
  var p,xi,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 (xi=0;!x&&xi<d.forms.length;xi++) x=d.forms[xi][n];
  for(xi=0;!x&&d.layers&&xi<d.layers.length;xi++) x=findObj(n,d.layers[xi].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
/******************************************/
//xMenu Array
xMenu =new Array(); //xMenu[x][0]=INDEX - xMenu[x][1]=PARENT - xMenu[x][2]=Text - xMenu[x][3]=Image - xMenu[x][4]=Roll Image - xMenu[x][5]=URL 
var xi=0;
var xii=0; //used in validation of xMenu Array
var xMaxMenuIndex=0;

//Default xLayer Frame for IE and NS6.X
var xLayer="\"<div id='\"+xIDLABEL+\"' class='sCBORDER' Style='Position:absolute; visibility:\"+xVISIBILITY+\"; left:\"+(xLEFT+NS6X)+\"px; top:\"+(xTOP+NS6X)+\"px; width:\"+(xWIDTH+NS6X)+\"px; height:\"+(xHEIGHT+NS6X)+\"px; background-color:\"+MyLayerColor+\"; z-index:\"+xZINDEX+\"; URL(\"+xMyURL+\")' \"";
var xLayer_End="</div>";

xLAYER= new Array();//xMenu string FOR EACH ITEM
xMOUSEOVEROUT=new Array();//HOLDS THE LAYERIDs that should have OnMouseOver && OnMouseOut for each menu
xTOPLEFT=new Array(); //holds Top and left measures for each menu item
xParent_Children_ID=new Array();

//Layers Variables
var xIDLABEL="";
var xVISIBILITY="visible"; //default
var xZINDEX=0;
var xMyURL="";

//Position function variables
var xTopParent=xTOP;
var xLeftParent=xLEFT;
var xORGWIDTH=xWIDTH;
Parent_Child_Count=new Array(); //Counts the children of each Main xMenu
var Main_Parent_Count=0; //Counts the number of the Main menu 

//Validation Variable
Error=false; 
SortingError=false; 
var mytarget="";

//NS CORRECTIONS
var NS_Table_Width_Corr=0;
var NS_Table_Heigth_Corr=0;
var NS6X=0; if(!document.all && document.getElementById) {NS6X=-2;} 


/******************************************/
//Add xMenu item to xMenu Array
/******************************************/
function AddSideMenu(ID, Parent_ID, Text, Image, RollImage, URL, TARGET)
{ 
  //Validation
  var valid = "0123456789"; 
  var temp; 
  if(ID.length==0) {alert("The xMenu #"+(xii+1)+" does not have an ID assigned"); Error=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 the xMenu #"+(xii+1)+" should be a number value"); Error=true;}
    }
  }
  if(Parent_ID.length==0) {alert("The xMenu #"+(xii+1)+" does not have a Parent_ID assigned"); Error=true;}
  //check if Parent_ID is composed of only numbers and dashes "-"
  else 
  { 
    //Checking if number
    for(var j=0;j<Parent_ID.length;j++)
    {
      temp=Parent_ID.substring(j,j+1);
      if(valid.indexOf(temp)==-1) {alert("The Parent_ID of the xMenu #"+(xii+1)+" should be a number value"); Error=true;}
    }
  }
  if(Text.length==0 && Image.length==0) {alert("The xMenu #"+(xii+1)+" should have either an image or a text assigned"); Error=true;}
  xii++;
   
  xMenu[xi] = new Array();    
  if(!Error)
  {
     for (var j=0; j < 7 ;j++)
     {         
        if (j==0) { xMenu[xi][0]=ID;         } 
        if (j==1) { xMenu[xi][1]=Parent_ID;  } 
        if (j==2) { xMenu[xi][2]=Text;       }
        if (j==3) { xMenu[xi][3]=Image;      }
        if (j==4) { xMenu[xi][4]=RollImage;  } 
        if (j==5) { xMenu[xi][5]=URL;        }
        if (j==6) { xMenu[xi][6]=TARGET;     }
     }
     xi++; 
     xMaxMenuIndex = xi;
  }  
  //Reinitialze Error Value
  Error=false;
}

/******************************************/
//Build Menus
/******************************************/
function BuildSide()
{
    //Browser Check
    xNS4XCorrection()
    
    //Initilize Position Variables
    for (var j=0;j<xMaxMenuIndex;j++)
    { 
      xTOPLEFT[j]=new Array();
      Parent_Child_Count[j]=0;
      xMOUSEOVEROUT[j]=new Array(); 
      xParent_Children_ID[j]=new Array();  
      xTOPLEFT[j][0]=0;
      xTOPLEFT[j][1]=0;
    }   
 
   //Sort and check menu for errors
   if(Sort==1) {xSorting();}

   if(!SortingError)
   {  
   
    //Build CSS: xLayer Border
    if (xLayerBorderSize!="")
    {document.writeln("<Style type=\"text/css\">");
     document.writeln(".sCBORDER {");
     document.writeln(" width:"+xWIDTH+"px;"); 
     document.writeln(" heigth:"+xHEIGHT+"px;");  
     document.writeln(" border: "+LayerBorderStyle+" "+LayerBorderColor+" "+xLayerBorderSize+"px;"); 
     document.writeln("}\n</style>\n");
    }
          
   //Build each xMenu
   for (xi=0; xi < xMaxMenuIndex;xi++)
   {
     //Calculates Main Positions, visibility, and determines ID
     xCalcLayerVariables(xi); 

     //Global Properties
     var MyLayerColor=xLayerColor;
     var MyLayerRollColor=xLayerRollColor;
     //Parent Color
     if(xMenu[xi][0]==xMenu[xi][1])
     {
        //BG ROLL Color 
        if (xMain_Parent_LayerColor!="" )
        {
            MyLayerColor=xMain_Parent_LayerColor;
        }            
     }     
     //xLayer Code  
     xLAYER[xi]=eval(xLayer);           
   }

   //Add samelevel mouse events
   xSameLevelMouseEvents(); 

   //Build
   for (var j=0;j<xMaxMenuIndex;j++)
   {
     var MyFONT=xFONT;
     var MyFONTCOLOR=xFONTCOLOR;
     var MyFONTSIZE=xFONTSIZE;
     var MyFONTSTYLE=xFONTSTYLE;
     var MySTARTCHAR=xSTART_CHAR;

     //Parent/Children xFONT Properties
     if(xMenu[j][0]==xMenu[j][1])
     {   
        if(xMain_Parent_FONT!="")
        {
            MyFONT=xMain_Parent_FONT;
        }  
        if(xMain_Parent_FONTCOLOR!="")
        {
            MyFONTCOLOR=xMain_Parent_FONTCOLOR;
        }  
        if(xMain_Parent_FONTSIZE!="")
        {
            MyFONTSIZE=xMain_Parent_FONTSIZE;
        }    
        if(xMain_Parent_FONTSTYLE!="")
        {
            MyFONTSTYLE=xMain_Parent_FONTSTYLE;
        }  
        if(Main_Parent_START_CHAR!="")
        {
            MySTARTCHAR=Main_Parent_START_CHAR;
        }        
     }  


     var MOUSEOVERCODE="";
     var MOUSEOUTCODE="";
     for (var jj=0;jj<xMaxMenuIndex;jj++)
     {
       if(xMOUSEOVEROUT[j][jj]!=null)
       {
          MOUSEOVERCODE=MOUSEOVERCODE+"showHideLayers('"+xMOUSEOVEROUT[j][jj]+"','','show');";
          MOUSEOUTCODE=MOUSEOUTCODE+"showHideLayers('"+xMOUSEOVEROUT[j][jj]+"','','hide');";
       } 
       else
       { 
          MOUSEOVERCODE=MOUSEOVERCODE+"xPathRoad("+j+",1)";           
          MOUSEOUTCODE=MOUSEOUTCODE+"xPathRoad("+j+",0)";          
          break;
       } 
     } 

     //Linking the entire layer area
     mytarget=""; 
     if(xMenu[j][6]!=""){mytarget=xMenu[j][6];}
     document.write(xLAYER[j]+" onMouseOver=\""+MOUSEOVERCODE+"\" onMouseOut=\""+MOUSEOUTCODE+"\"");
     if(xMenu[j][5]!="") //{document.write(" class=\"location.href='"+xMenu[j][5]+"';\"");}
     {document.write(" onClick=\" ")
      if(mytarget!="") {
           document.write(mytarget+".");
           }
       document.write("location.href='"+xMenu[j][5]+"';\"");
     }
     document.write(" >");     
     
     var LINK="";      
     //display link if any and setup rollover image if any
     if(xMenu[j][5]!="")
     {   
        LINK="<A href=\""+xMenu[j][5]+"\"";
        if(mytarget!="")
        {
          LINK=LINK+" target=\""+mytarget+"\"";
        }
        LINK=LINK+" >";
     }
     
     //Image Code
     var ImageCode="";
     if(xMenu[j][3]!="")
     {
       ImageCode=ImageCode+"<IMG Name='Image"+xMenu[j][0]+"' SRC='"+xMenu[j][3]+"' BORDER=0>";  
     } 
     
     //Fonts if any 
     var FONT_PROPERTIES="";  
     if(MyFONT!="")     {FONT_PROPERTIES=" Face='"+MyFONT+"'";}
     if(MyFONTSIZE!="") {FONT_PROPERTIES=FONT_PROPERTIES+" size='"+MyFONTSIZE+"'";}
     if(MyFONTCOLOR!=""){FONT_PROPERTIES=FONT_PROPERTIES+" color='"+MyFONTCOLOR+"'";}
     if(FONT_PROPERTIES!="" ){FONT_PROPERTIES="<FONT "+FONT_PROPERTIES+">";}
     if(MyFONTSTYLE!=""){FONT_PROPERTIES="<"+MyFONTSTYLE+">"+FONT_PROPERTIES;}
     

     //Display IMAGE  and TEXT
     document.write("<table border='0' xWIDTH='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(xMenu[j][2]!="")
     {
       document.write("<td ");
       if(HALIGN !=""){document.write("  align='"+HALIGN+"' ");}
       if(VALIGN !=""){document.write(" valign='"+VALIGN+"' ");}
       document.write(">");

       if(FONT_PROPERTIES!="") {document.write(FONT_PROPERTIES);}
       document.write(MySTARTCHAR);
       if(LINK!="") {document.write(LINK);} 
       document.write(xMenu[j][2]);       
       if(LINK!=""){document.write("</a>");}
       if(FONT_PROPERTIES!="")  {document.write("</FONT>");} 
       if(MyFONTSTYLE!="") {document.write("</"+MyFONTSTYLE+">");}
       document.write("</td>"); 
     }
     document.write("</tr></table>");

      //close layer
      document.writeln(xLayer_End);
   }
  }   
}
/*********************************************************************************/
////Sort in-case not ordered and check if every menu parent has a matching index
/********************************************************************************/
function xSorting()
{
  //Place all Parents before children
  for (xi=0; xi<(MaxMenuIndex-1);xi++)
  { 
    TheParent=xMenu[xi][1];    
    for (j=(xi+1); j<xMaxMenuIndex; j++)
    { 
       if(xMenu[j][0]==TheParent)    
       {              
           for(var f=0;f<6;f++)
           {           
             temp=xMenu[xi][f];               
             xMenu[xi][f]=xMenu[j][f];
             xMenu[j][f]=temp;  
           }
           xi=0;   
           break;           
       }
    } 
  }
}
/******************************************/
//Netscape 4.X Correction 
/******************************************/
function xNS4XCorrection()
{
  if(document.layers)
  {
   xLayer="\"<layer class='sCBORDER' id='\"+xIDLABEL+\"' position='absolute' visibility='\"+xVISIBILITY+\"' left='\"+xLEFT+\"' top='\"+xTOP+\"' width='\"+xWIDTH+\"' height='\"+xHEIGHT+\"' bgcolor='\"+MyLayerColor+\"' z-index='\"+xZINDEX+\"'\" ";    
   xLayer_End="</layer>";
  }
  NS_Table_Width_Corr=5;
  NS_Table_Heigth_Corr=5;
}

function xNS4ImageCorrect(Index)
{
  var NS4FIX="";
  if(document.layers)
  {
     return "layers.xLayer"+xMenu[Index][0]+".document.";
  }
  return ""; 
}  

/******************************************/
//Calculates xMenu Item Position                            
/******************************************/
function xCalcLayerVariables(Index)
{
       var ID= xMenu[Index][0];
       var Parent_ID= xMenu[Index][1];

       //if Main Parent   
       if (ID==Parent_ID)
       {   
          //xMenu Type  
          if(xMENU_TYPE==1)
          {  
            xTOP=xTopParent; 
            xLEFT=(Main_Parent_Count * xWIDTH)+ xLeftParent - (xLayerBorderSize*Main_Parent_Count);  
          }
          else
          {
            xTOP=(Main_Parent_Count * xHEIGHT)+xTopParent - (xLayerBorderSize*Main_Parent_Count);
            xLEFT=xLeftParent;  
          }
          xTOPLEFT[Index][0]=xTOP;        
          xTOPLEFT[Index][1]=xLEFT;                   
          xIDLABEL="xLayer"+xMenu[xi][0];        
          xVISIBILITY="visible";
          xZINDEX=100; //on top of the first 99 layers in the page!
          xWIDTH=((+xORGWIDTH)-(+xLayerBorderSize));

          Main_Parent_Count++;                
       }
       //Child
       else 
       {  
          //Find Parent        
          ChildofAParent=false;        
          var ParentIndex=0; 
          for (var j=0;j<xMaxMenuIndex;j++)
          { 
            if (xMenu[j][0]== Parent_ID)
            {  
               //collecting Children of Parents 
               for (var g=0;g<xMaxMenuIndex;g++)
               {
                 if(xParent_Children_ID[j][g]==null)
                 { xParent_Children_ID[j][g]=xIDLABEL;
                   break;
                 }
               } 
               if(xMenu[j][0]==xMenu[j][1]) 
               {ChildofAParent=true;}
               ParentIndex=j; 
               break;
            }
          }
          //if child of a Main Parent and xMenu Type is Horizental                        
          if(ChildofAParent && xMENU_TYPE==1)
          { 
             Parent_Child_Count[ParentIndex] = Parent_Child_Count[ParentIndex] + 1;                                   
             xTOP=xTopParent+(Parent_Child_Count[ParentIndex] * xHEIGHT) - ((Parent_Child_Count[ParentIndex])*xLayerBorderSize);
             xLEFT=xTOPLEFT[ParentIndex][1];                  
             xZINDEX=101; //on top of the first 100 layers in the page!                  
          } 
          //if a child of a child  
          else
          { 
             Parent_Child_Count[ParentIndex] = Parent_Child_Count[ParentIndex] + 1; 
             xTOP=(xTOPLEFT[ParentIndex][0])+((Parent_Child_Count[ParentIndex]-1) * xHEIGHT) - ((Parent_Child_Count[ParentIndex]-1)*xLayerBorderSize) + TOP_OFFSET;
             xLEFT=(xWIDTH + xTOPLEFT[ParentIndex][1]) - LEFT_OFFSET - xLayerBorderSize;               
             if(ChildofAParent){xZINDEX=101;}else{xZINDEX=102;} //on top of the first 100 layers in the page,100 is Main Parent and 101 is Parent
          }        
          xVISIBILITY="hidden";  
          xIDLABEL="xLayer"+xMenu[Index][0];          
          xTOPLEFT[Index][0]=xTOP;                                              
          xTOPLEFT[Index][1]=xLEFT;
          xWIDTH=((+xORGWIDTH)-(+xLayerBorderSize));
       }    
}
/******************************************/
//Generates onMouseOver Event for same 
//level layers
//                           
/******************************************/
function xSameLevelMouseEvents()
{
  //1 showHide Main Parent children
  for(var u=0;u<xMaxMenuIndex;u++)
  {
     //if Main Parent  
     if (xMenu[u][0]==xMenu[u][1])
     { 
          for(var y=0;y<xMaxMenuIndex;y++)
          {
             //if not the one being tested and has the same parent
             if(y!=u && (xMenu[y][1]==xMenu[u][0]))
             { 
               for(var z=0;z<xMaxMenuIndex;z++) {if(xMOUSEOVEROUT[u][z]==null) {xMOUSEOVEROUT[u][z]="xLayer"+xMenu[y][0];break;}} 
             }
          } 
      }
   }
   //2 show hide same Parent menu items
   for(var u=0;u<xMaxMenuIndex;u++)
   {
      if (xMenu[u][1]!=xMenu[u][0])
      {
         for(var z=0;z<xMaxMenuIndex;z++) {if(xMOUSEOVEROUT[u][z]==null) {xMOUSEOVEROUT[u][z]="xLayer"+xMenu[u][0];break;}} 
         for(var y=0;y<xMaxMenuIndex;y++)
          {
             //if not the one being tested and has the same parent and bot the parent menu
             if(y!=u && (xMenu[y][1]==xMenu[u][1]) && (xMenu[y][0]!=xMenu[y][1]) )
             { 
                for(var z=0;z<xMaxMenuIndex;z++) {if(xMOUSEOVEROUT[u][z]==null) {xMOUSEOVEROUT[u][z]="xLayer"+xMenu[y][0];break;}}  
             }
          }
      }
   } 
   //3 show hide children of non Main Parent
   var same="";
   for(var u=0;u<xMaxMenuIndex;u++)
   {
       if (xMenu[u][1]!=xMenu[u][0])
       {
          for(var y=0;y<xMaxMenuIndex;y++)
          {
             if ((xMenu[u][0]==xMenu[y][1]) && (xMenu[y][0]!=xMenu[y][1]) && y!=u)
             {
                for(var z=0;z<xMaxMenuIndex;z++) 
                {if(xMOUSEOVEROUT[u][z]==null) {xMOUSEOVEROUT[u][z]="xLayer"+xMenu[y][0];same=z;break;}}                 
                //Pass Parent's show hide to the children
                for(var z=0;z<xMaxMenuIndex;z++) 
                {
                  if(xMOUSEOVEROUT[u][z]!=null) 
                  {
                    for(var x=0;x<xMaxMenuIndex;x++) 
                    {
                      if(xMOUSEOVEROUT[y][x]==null) {xMOUSEOVEROUT[y][x]=xMOUSEOVEROUT[u][z];break;}
                    }
                  }
                  else {break;}
                }  
             }
          }
       }
   }   

}
/******************************/
//PathColor
/******************************/
function xPathRoad(Parent,flag)
{
  AtStart=false;
  while (!AtStart) 
  {  
     //change both layer color and image     
     if(flag==1) //Roll
     {
       //xLayer's Image: Browser Check 
       if(xMain_Parent_LayerRollColor!="" && xMenu[Parent][0]==xMenu[Parent][1]) {MyLayerRollColor=xMain_Parent_LayerRollColor;} else {MyLayerRollColor=xLayerRollColor;}        
       if(xMenu[Parent][4]!="")
       {
         eval("document."+xNS4ImageCorrect(Parent)+"images.Image"+xMenu[Parent][0]+".src='"+xMenu[Parent][4]+"'"); 
       }

       //xLayer Color: Browser Check 
       if(document.layers)
       {
         eval("changeProp('xLayer"+xMenu[Parent][0]+"','','document.bgColor','"+MyLayerRollColor+"','xLAYER')");
       }
       else  if(document.getElementById || document.all)
       {

         eval("changeProp('xLayer"+xMenu[Parent][0]+"','','style.backgroundColor','"+MyLayerRollColor+"','DIV')");
       }
     }
     else //Origin Image and Color
     {
       //xLayer's Image: Browser Check 
       if(xMain_Parent_LayerColor!="" && xMenu[Parent][0]==xMenu[Parent][1]) {MyLayerColor=xMain_Parent_LayerColor;} else {MyLayerColor=xLayerColor;} 
       if(xMenu[Parent][3]!="")
       {
         eval("document."+xNS4ImageCorrect(Parent)+"images.Image"+xMenu[Parent][0]+".src='"+xMenu[Parent][3]+"'"); 
       } 
       //xLayer Color: Browser Check  
       if(document.layers){eval("changeProp('xLayer"+xMenu[Parent][0]+"','','document.bgColor','"+MyLayerColor+"','xLAYER')");}
       else if(document.getElementById || document.all){eval("changeProp('xLayer"+xMenu[Parent][0]+"','','style.backgroundColor','"+MyLayerColor+"','DIV')");}      
     }
     if(xMenu[Parent][0] == xMenu[Parent][1])
     {
       AtStart=true;
     }
     Parent=xINDEXof(xMenu[Parent][1]);  
  }
}
//Find the index of the xMenu Array with the Parent: Parent
function xINDEXof(Parent)    
{
 for(var j=0;j<xMaxMenuIndex;j++)
 {
   if(xMenu[j][0]==Parent)
   {
     return (j);
   }  
 } 
}