/****************************************************************************** 
FoldoutMenu2 
*   Copyright (C) 2001 <a href="/dhtmlcentral/thomas_brattli.asp">Thomas Brattli</a>
*   This script was released at DHTMLCentral.com
*   Visit for more great scripts!
*   This may be used and changed freely as long as this msg is intact!
*   We will also appreciate any links you could give us.
*
*   Made by <a href="/dhtmlcentral/thomas_brattli.asp">Thomas Brattli</a> 
*******************************************************************************/

// Version modified by Dave Schontzler [ stilleye.com | madhatter@pullman.com ]
// Added the auto-expanding onLoad

// Version modified by Everett Stauffer [ everett@ucsd.edu ]
// Added ability to deal with multiple menus

function lib_bwcheck(){ //Browsercheck (needed)
  this.ver=navigator.appVersion
  this.agent=navigator.userAgent
  this.dom=document.getElementById?1:0
  this.opera5=this.agent.indexOf("Opera 5")>-1
  this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom && !this.opera5)?1:0; 
  this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom && !this.opera5)?1:0;
  this.ie7=(this.ver.indexOf("MSIE 7")>-1 && this.dom && !this.opera5)?1:0;
  this.ie4=(document.all && !this.dom && !this.opera5)?1:0;
  this.ie=this.ie4||this.ie5||this.ie6||this.ie7
  this.mac=this.agent.indexOf("Mac")>-1
  this.ns6=(this.dom && parseInt(this.ver) >= 5) ?1:0; 
  this.ns4=(document.layers && !this.dom)?1:0;
  this.bw=(this.ie7 || this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.opera5)
  return this
}
var bw=new lib_bwcheck()

if(document.layers){ //NS4 resize fix...
  scrX = innerWidth; scrY = innerHeight;
  onresize = function() {
    if(scrX!= innerWidth || scrY!= innerHeight) { history.go(0); }
  }
}

//Opera 5 resize fix.
if(navigator.userAgent.indexOf('Opera')>-1 && document.getElementById) {
  scrX = innerWidth; scrY= innerHeight;
  document.onmousemove = function() {
    if(scrX<innerWidth-10 || scrY<innerHeight-10 ||
       scrX>innerWidth+10 || scrY>innerHeight+10){
      scrX = innerWidth;
      scrY = innerHeight;
      foldInit();
    }
  };
}
/******************************************************************************
This is the new foldoutmenu, with more levels!
This is in some ways a rather advanced menu so please read every
note in this script and in the comments in the page to understand how
it works.

NOTE: if you change the position of menuCont from absolute to relative, 
you can put the menu in a table. HOWEVER it will no longer work in netscape 4.
If you wish to support netscape 4, you have to use absolute positioning.
******************************************************************************/

//Do you want images (if not set to 0 and remove the images from the body)?
foldImg=1

// Here are the images for the top links.
//The image for the closed state.
foldinImg=new Image();
//The image for the open state.
foldoutImg=new Image();

//Here are the images for the sub links.
//The image for the "in" state.
foldsubinImg=new Image();
//The image for the "out" state.
foldsuboutImg=new Image();

/*******************************************************************************
Making cross-browser objects
*******************************************************************************/
function makeMenu(obj,nest) {
  nest=(!nest) ? "":'document.'+nest+'.';
  this.css= bw.dom? document.getElementById(obj).style:bw.ie4?document.all[obj].style:bw.ns4?eval(nest+"document.layers." +obj):0;
  this.elm= bw.dom?document.getElementById(obj):bw.ie4?document.all[obj]:bw.ns4?eval(nest+'document.'+obj):0;
  this.ref= bw.dom || bw.ie4? document:bw.ns4?eval(nest+"document.layers." +obj+".document"):0;
  this.x= bw.ns4?this.css.left:bw.opera5?this.css.pixelLeft:this.elm.offsetLeft;
  this.y= bw.ns4?this.css.top:bw.opera5?this.css.pixelTop:this.elm.offsetTop;
  this.height= bw.ns4?this.ref.height:bw.opera5?this.css.pixelHeight:this.elm.offsetHeight;
  this.hideIt= b_hideIt;
  this.showIt= b_showIt;
  this.moveIt= b_moveIt;
  this.status= 0;
  return this;
}

// A unit of measure that will be added when setting the position of a layer.
var px = bw.ns4||window.opera?"":"px";

function b_showIt() {
  this.css.visibility="visible"; this.status=1;
}
function b_hideIt() {
  this.css.visibility="hidden"; this.status=0;
}
function b_moveIt(x,y) {
  this.x=x; this.y=y;
  this.css.left=this.x+px;
  this.css.top=this.y+px;
}
/*******************************************************************************
Initiating the page and making the menu
*******************************************************************************/
var oFolds = new Array();
var oFoldCont = new Array();

function foldInit(){
  //Fixing the browsercheck for opera... this can be removed if the browsercheck has been updated!!
  bw.opera5 = (navigator.userAgent.indexOf("Opera")>-1 && document.getElementById)?true:false
  if (bw.opera5) bw.ns6 = 0;
  
  // loop through the number of menus
  for(var k=0; k<nummenus; k++) {
    // Creating the foldoutmenu
    var oFold = new Array();
    y = 0;
    suby = 0;
    for(var i=0; i<mainmenus[k]; i++) {
      oFold[i] = new makeMenu('menuItem'+k+'_'+i,'menuCont'+k);
      oFold[i].moveIt(0,y);
      oFold[i].size = oFold[i].height;
      y += oFold[i].height+pxbetweenmain;
      if (bw.ns4) oFold[i].css.clip.bottom = 500;
      oFold[i].sub = new Array();
      oFold[i].subs = top_subs;
      suby = oFold[i].height+pxbetweensub;
      for (var j=0; j<submenus[k][i]; j++){
        oFold[i].sub[j] = new makeMenu('submenuItem'+k+'_'+i+'_'+j,'menuCont'+k+'.document.menuItem'+k+'_'+i);
        oFold[i].sub[j].sub = new makeMenu('subItem'+k+'_'+i+'_'+j,'menuCont'+k+'.document.menuItem'+k+'_'+i+'.document.submenuItem'+k+'_'+i+'_'+j);
        oFold[i].sub[j].hideIt();
        oFold[i].sub[j].sub.hideIt();
        oFold[i].sub[j].moveIt(10,suby);
        suby += oFold[i].sub[j].height+pxbetweensub;
      }
    }
    oFolds[k] = oFold;
    oFoldCont[k] = new makeMenu('menuCont'+k,0);
    if (bw.ns4) oFoldCont[k].css.clip.bottom = 800;
    oFoldCont[k].showIt(); //Showing the menu when the menu have been "created"
  }
if(showCurrentOnLoad) expandOnLoad(); // Added by D. Schontzler
}
/************************************************************************************
Object function to the top of the menus.
************************************************************************************/
function top_subs(show, which, num) {
  for(var j=0; j<this.sub.length; j++){
    if(show) {
      this.sub[j].showIt();
      if(foldImg) this.ref["img"+which+"_"+num].src = foldoutImg.src;
      if(substayfolded) {
        if(this.sub[j].sub.status) this.sub[j].sub.showIt();
      } else {
        if(foldImg)
          this.sub[j].ref["imgSub"+which+"_"+num+"_"+j].src = foldsubinImg.src;
        this.sub[j].sub.hideIt();
      }
    } else {
      this.sub[j].hideIt();
      if (foldImg) this.ref["img"+which+"_"+num].src = foldinImg.src;
      if (!substayfolded) this.sub[j].sub.hideIt();
      else if (substayfolded) this.sub[j].sub.css.visibility="hidden";
    }
  }
}

/*******************************************************************************
Checking height of the menus, and moves them to the correct position.
*******************************************************************************/
function checkheight(which, org) {
  var oFold = oFolds[which];

  if(mainstayfolded) istart=org;
  else istart=0;

  for(i=istart; i<oFold.length; i++) {
    oFold[i].size=oFold[i].height;
    for(j=0; j<oFold[i].sub.length; j++){
      oFold[i].sub[j].size=oFold[i].sub[j].height+pxbetweensub;
      if(oFold[i].sub[j].status){
        oFold[i].size+=oFold[i].sub[j].size;
        if(oFold[i].sub[j].sub.status){
          oFold[i].size+=oFold[i].sub[j].sub.height+pxbetweensub;
          oFold[i].sub[j].size+=oFold[i].sub[j].sub.height+pxbetweensub;
        }
      }
      if(j!=0)
        oFold[i].sub[j].moveIt(10,oFold[i].sub[j-1].y+oFold[i].sub[j-1].size);
    }
    if(i!=0) oFold[i].moveIt(0,oFold[i-1].y+oFold[i-1].size+pxbetweenmain);
  }
}

/*******************************************************************************
Fold in and out the submenus.
*******************************************************************************/
function submenu(which,main,sub) {
  var oFold = oFolds[which];
  if (!substayfolded) {
    for (var i=0; i<oFold[main].sub.length; i++){
      if (i!=sub){
        oFold[main].sub[i].sub.hideIt();
        if (foldImg)
          oFold[main].sub[i].ref["imgSub"+which+"_"+main+"_"+i].src = foldsubinImg.src
      }
    }
  }
  if (!oFold[main].sub[sub].sub.status){
    oFold[main].sub[sub].sub.showIt()
    if(foldImg)
      oFold[main].sub[sub].ref["imgSub"+which+"_"+main+"_"+sub].src = foldsuboutImg.src;
  }
  else {
    oFold[main].sub[sub].sub.hideIt()
    if(foldImg)
      oFold[main].sub[sub].ref["imgSub"+which+"_"+main+"_"+sub].src = foldsubinImg.src;
  }
  checkheight(which,main);
}
/*******************************************************************************
Fold in and out the mainmenus
*******************************************************************************/
function foldmenu(which, num) {
  var oFold = oFolds[which];

  if (!mainstayfolded) {
    for(var i=0; i<oFold.length; i++) {
      if(i!=num) oFold[i].subs(0,which,i);
    }
  }
  if(!oFold[num].sub[0].status) oFold[num].subs(1,which,num);
  else oFold[num].subs(0,which,num);
  checkheight(which,num);
}

//Initiating the menus onload, if the browser is ok.
if(bw.bw) onload = foldInit;

var expanded = 0;
// expandOnLoad function added by D. Schontzler
function expandOnLoad() {
  if(expanded) return;
  expanded = 1;
  // check to see if browser supports DOM structure

  if(document.getElementsByTagName) {
    el = document.getElementsByTagName('a');
    tgs = new Array();
    fTags = new Array();
    for(i=0;(el.item(i));i++) {
      if(el.item(i).className.indexOf('cl')>-1) tgs[tgs.length] = el.item(i)
    }
    for(i in tgs) {
      if(tgs[i].className.indexOf('Links')>-1&&tgs[i].href.indexOf('#')==-1) 
        fTags[fTags.length] = tgs[i];
    }
    useLevel = useTopLevel ? top : window;
    thisLoc = useLevel.location.href.toLowerCase();
    if(thisLoc.indexOf('#')) thisLoc = thisLoc.split('#')[0];
    if(thisLoc.indexOf('?')) thisLoc = thisLoc.split('?')[0];
    for(i in fTags) {
      if(thisLoc == fTags[i].href.toLowerCase()) {
        subH = fTags[i].parentNode.id.split('_');
        setH = new Array();
        for(i in subH) setH[i] = subH[i].charAt(subH[i].length-1)
        if(setH.length<=2) foldmenu(setH[0],setH[1]);
        else if(setH.length>2) {
          foldmenu(setH[0],setH[1]);
          submenu(setH[0],setH[1],setH[2]);
        }
        break;
      }
    }
    // clear varibles
    delete el; delete tgs; delete subH;
    delete setH; delete thisLoc;
  }
}
