//****************************************************
// Menu with SubMenu
// This script was originally written for the RT 22 site.
// You are free to copy the "Menu" script as long as you  
// keep this copyright notice: 
// Author: Stefano Selleri (selleri@ingfi9.det.unifi.it) November '00. 
//**************************************************************** 

function Menu () { //Constructor
  //Data
  this.TopMenu = new Array;
  this.activeButton = -1;
  this.activeSubButton = -1;

  this.Yoff0  = 0;
  this.Xoff0  = 0;
  this.Xoff0i = 0;

  this.Yoff1  = 0;
  this.Xoff1  = 0;
  this.Xoff1i = 0;

  //methods 
  this.AddButton = AddButton; 
  this.AddSubButton = AddSubButton; 
  this.ShowMenu = ShowMenu;
  this.ActivateMenu = ActivateMenu;
  this.PassOnMenu = PassOnMenu;
  this.PassOffMenu = PassOffMenu;
  this.ActivateSubMenu = ActivateSubMenu;
  this.PassOnSubMenu = PassOnSubMenu;
  this.PassOffSubMenu = PassOffSubMenu;
}

//
// Builders...
//            ... Add a top level button 
function AddButton(off,high,on,url) {

  if (this.TopMenu.length) {
    last = this.TopMenu.length;
  } else {
    last = 0;
  }

  this.TopMenu[last] = new Array(5);
  this.TopMenu[last][0] = off
  this.TopMenu[last][1] = high;
  this.TopMenu[last][2] = on;
  this.TopMenu[last][3] = url;
  this.TopMenu[last][4] = 0;

  return (last);

}

//            ... Add a sub-level button 
function AddSubButton(bn,off,high,on,url) {

  if (this.TopMenu[bn][4]==0) {
    this.TopMenu[bn][4] = new Array;
    lastsm = 0;
  } else {
    lastsm = this.TopMenu[bn][4].length; 
  }
  this.TopMenu[bn][4][lastsm] = new Array(4);
  this.TopMenu[bn][4][lastsm][0] = off
  this.TopMenu[bn][4][lastsm][1] = high;
  this.TopMenu[bn][4][lastsm][2] = on;
  this.TopMenu[bn][4][lastsm][3] = url;
}

// Silly games for mouse hovering (arrives)
function PassOnMenu(menuButton) {
  if (menuButton!=this.activeButton) {
    eval("document.menu"+menuButton+".src=\""+this.TopMenu[menuButton][1]+"\"");
  }
}

// Silly games for mouse hovering (leaves)
function PassOffMenu(menuButton) {
  if (menuButton==this.activeButton) {
    eval("document.menu"+menuButton+".src=\""+this.TopMenu[menuButton][2]+"\"");
  } else {
    eval("document.menu"+menuButton+".src=\""+this.TopMenu[menuButton][0]+"\"");
  }
}

// Silly games for mouse hovering (arrives)
function PassOnSubMenu(menuButton,subMenuButton) {
  if (subMenuButton!=this.activeSubButton) {
    eval("document.submenu"+menuButton+"_"+subMenuButton+".src=\""+this.TopMenu[menuButton][4][subMenuButton][1]+"\"");
  }
}

// Silly games for mouse hovering (leaves)
function PassOffSubMenu(menuButton,subMenuButton) {
  if (subMenuButton==this.activeSubButton) {
    eval("document.submenu"+menuButton+"_"+subMenuButton+".src=\""+this.TopMenu[menuButton][4][subMenuButton][2]+"\"");
  } else {
    eval("document.submenu"+menuButton+"_"+subMenuButton+".src=\""+this.TopMenu[menuButton][4][subMenuButton][0]+"\"");
  }
}

// Hard stuff for when a button is clicked
function ActivateMenu(menuButton){
  if (this.activeSubButton!=-1) {
    eval("document.submenu"+this.activeButton+"_"+this.activeSubButton+".src=\""+this.TopMenu[this.activeButton][4][this.activeSubButton][0]+"\"");
    this.activeSubButton=-1;
  }
  this.activeButton = menuButton;
  eval("parent.frames[\"text\"].location=\""+this.TopMenu[this.activeButton][3]+"\"");

  for(i=0;i<this.TopMenu.length;i++){
    if (i==menuButton) {
      eval("document.menu"+i+".src=\""+this.TopMenu[i][2]+"\"");
      if (this.TopMenu[i][4]!=0) {
        for(j=0;j<this.TopMenu[i][4].length;j++){
  	       if (browserVersion == 1) {
            document.all["SUBMENU_"+i+"_"+j].style.display = 'block';
          } else {
            eval("document.layers.SUBMENU_"+i+"_"+j+".visibility = 'show'");
          }
        }
      }
    } else { 
      eval("document.menu"+i+".src=\""+this.TopMenu[i][0]+"\"");
      if (this.TopMenu[i][4]!=0) {
        for(j=0;j<this.TopMenu[i][4].length;j++){
  	     if (browserVersion == 1) {
            document.all["SUBMENU_"+i+"_"+j].style.display = 'none';
          } else {
            eval("document.layers.SUBMENU_"+i+"_"+j+".visibility = 'hide'");
          }
        }
      }
    }
  }
}

// Soft stuff for when a button is clicked
function ActivateSubMenu(menuButton,menuSubButton){
  this.activeSubButton = menuSubButton;
  i1 = this.activeButton;
  i2 = this.activeSubButton;
  eval("parent.frames[\"text\"].location=\""+this.TopMenu[i1][4][i2][3]+"\"");
  for(i=0;i<this.TopMenu[i1][4].length;i++){
    if (i==menuSubButton) {
      eval("document.submenu"+this.activeButton+"_"+i+".src=\""+this.TopMenu[i1][4][i][2]+"\"");
    } else { 
      eval("document.submenu"+this.activeButton+"_"+i+".src=\""+this.TopMenu[i1][4][i][0]+"\"");
    }
  }
}

//
// Draws all the stuff for the first time
//
function ShowMenu () {

  // Background
  if (browserVersion == 2) {
    document.write("<layer id=BK top="+this.YposB+" left="+this.XposB+">");
  }
	
  document.write("<div ");
  if (browserVersion == 1) 
    document.write("class='' id='BK' style='position:absolute;top:"+this.YposB+"px;left:"+this.XposB+"'")
  document.write(">"); 
  document.write("<img src=\""+this.Bi+"\" Border=0>");
  document.write("</div>") 

  if (browserVersion == 2) {
    document.write("</layer>");
  }

  for(i=0;i<this.TopMenu.length;i++){

    if (browserVersion == 2) {
      document.write("<layer id=MENU_"+i+" top="+this.Ypos0+" left="+eval(this.Xpos0+i*this.Xpos0i)+">");
    }
	
    document.write("<div ");
    if (browserVersion == 1) 
      document.write("class='' id='MENU_" + i + "' style='position:absolute;top:"+this.Ypos0+"px;left:"+eval(this.Xpos0+i*this.Xpos0i)+"'")
    document.write(">"); 
    
    if (browserVersion > 0) {
      document.write("<a href=\"javascript:void(0)\" OnClick=\"TheMenu.ActivateMenu("+i+")\" OnMouseOver=\"TheMenu.PassOnMenu("+i+")\" OnMouseOut=\"TheMenu.PassOffMenu("+i+")\">");
    } else {
      document.write("<a>");
    }
    document.write("<img src=\""+this.TopMenu[i][0]+"\" name=\"menu"+i+"\" Border=0></A>");
    document.write("</div>") 

    if (browserVersion == 2) {
      document.write("</layer>");
    }

    if (this.TopMenu[i][4]!=0) {
      for(j=0;j<this.TopMenu[i][4].length;j++){
        if (browserVersion == 2) {
          document.write("<layer id=SUBMENU_"+i+"_"+j+" top="+this.Ypos1+" left="+eval(this.Xpos1+j*this.Xpos1i)+">");
        }
        document.write("<div ");
        if (browserVersion == 1) 
          document.write("class='' id='SUBMENU_" + i + "_" + j + "' style='position:absolute;top:"+this.Ypos1+"px;left:"+eval(this.Xpos1+j*this.Xpos1i)+"'")
        document.write(">"); 
        
        if (browserVersion > 0) {
          document.write("<a href=\"javascript:void(0)\" OnClick=\"TheMenu.ActivateSubMenu("+i+","+j+")\" OnMouseOver=\"TheMenu.PassOnSubMenu("+i+","+j+")\" OnMouseOut=\"TheMenu.PassOffSubMenu("+i+","+j+")\">");
        } else {
          document.write("<a>");
        }
        document.write("<img src=\""+this.TopMenu[i][4][j][0]+"\" name=\"submenu"+i+"_"+j+"\" Border=0></A>");
        document.write("</div>")  
        if (browserVersion == 2) {
          document.write("</layer>");
        }
	     if (browserVersion == 1) {
          document.all["SUBMENU_"+i+"_"+j].style.display = 'none';
        } else {
          eval("document.layers.SUBMENU_"+i+"_"+j+".visibility = 'hide'");
        }
      }
    }
  }
}

//
// Initializes the document...
//                         ... And sets some globals
function initializeDocument() 
{ 
  if (document.all) 
    browserVersion = 1 //IE4   
  else 
    if (document.layers) 
      browserVersion = 2 //NS4 
    else 
      browserVersion = 0 //other 
 
  TheMenu.ShowMenu();
} 
