//  These functions require data arrays that are declared elsewhere

var active = new Array();

// buttonSetDivs and buttonSetStrs are required to map DOM names to specific
//   sub-menu items for subsequent manipulation
//   the arrays are populated by the putNavbar function 

// buttonSetDivs Array holds the DOM names of the divs holdng sub-menu buttons 
// and determines their indentations, as implemented by putNavbar function
var   buttonSetDivs = new Array();

// buttonSetStrs holds the DOM names of the divs holding strings for sub-menu buttons
var   buttonSetStrs = new Array();

var   buttonSetState = new Array();

function putNavbar(whichToActivate, whichToOpen) {
   // make only one copy of the navbar here, not in each page
   var navbarContent = "";
   var contentDiv = "hello";
   var i, j, k;
   k = 0;

   for (i = 0; i < numButtonSets; i++) {  // for each top level button
      buttonSetDivs[i] = new Array();
      buttonSetStrs[i] = new Array();
      // put in main level buttons
      contentDiv = "<div id='navRow\
" + k + "' class='button_row'>\n\   <div class='button_text top_button_text' id='navButton\
" + k + "' onMouseOver='hoverButton(\
" + k + ")' onMouseOut='normalButton(\
" + k + ")' onClick='linkTo(\
" + k + "); '>\
" + buttons[k][0] + "</div>\n\
";
      if (buttonsInSet[i] > 0)
           contentDiv = contentDiv + "   <img class='arrow_img' id='arrow\
" + i + "' onMouseOver='hoverArrow(\
" + i + ")' onMouseOut='normalArrow(\
" + i + ")' onClick='toggleButtonSet(\
" + i + ")' src='\
" + normalPtr[0] + "'/>\n\
";
      contentDiv = contentDiv + "</div>\n";
      if (debug > 1) alert(contentDiv);
      navbarContent = navbarContent +  contentDiv;
      active[k] = 0;
      k++;
      for (j = 0; j < buttonsInSet[i]; j++) { // for each sub button
         // populate buttonSetDivs and buttonSetStrs Arrays
         buttonSetDivs[i][j] = "navRow" + k;
         buttonSetStrs[i][j] = "navButton" + k;
         // put in sub level buttons
         contentDiv = "<div class='button_row' id='navRow\
" + k + "'>\n\    <div class='button_text sub_button_text' id='navButton\
" + k + "' onMouseOver='hoverButton(\
" + k + ")' onMouseOut='normalButton(\
" + k + ")' onClick='linkTo(\
" + k + ");'>\
" + buttons[k][0] + "</div>\n\
</div>\n\
" ;
         if (debug > 1) alert(contentDiv);
         navbarContent = navbarContent +  contentDiv;
         buttonSetState[i] = 1;
         active[k] = 0;
         k++;
      }
   }

   if (debug > 0) {
     myoutput = document.getElementById('debug') ;
     myoutput.innerHTML = "<h3>Javascript-generated Code</h3><xmp>\n" + navbarContent + "\n</xmp>";
    }
    mylayer = document.getElementById('navbar') ;
    mylayer.innerHTML = navbarContent;
    rollupAllButtons();
    if (whichToOpen > -1) {
        //  need to delay opening of button set until rollupAllButtons() is completed
        str = "openButtonSet(" + whichToOpen + ")";
        setTimeout(str,(nSlices+5)*delayUnit);
    }
    if (whichToActivate > -1) activeButton(whichToActivate);
}

function linkTo(linkIndex) {
   window.open(buttons[linkIndex][1], buttons[linkIndex][2])
}

function hoverButton(index) {
    buttonStyle = document.getElementById("navButton" + index).style;
    // execute only if the image is not active
    if (active[index] == 0) {
        buttonStyle.backgroundColor = hoverColor;
    }
}

function normalButton(index) {
    buttonStyle = document.getElementById("navButton" + index).style;
    // execute only if the image is not active
    if (active[index] == 0) {
        buttonStyle.backgroundColor = normalColor;
    }
}

function setAllNormal() {
    var q;
    for (q = 0; q < active.length; q++) {
        active[q] = 0;
        buttonStyle = document.getElementById("navButton" + q).style;
        buttonStyle.backgroundColor = normalColor;
    }
}

function activeButton(index) {
    setAllNormal();
    active[index] = 1;
    buttonStyle = document.getElementById("navButton" + index).style;
    buttonStyle.backgroundColor = activeColor;
}

function normalArrow(index) {
    arrowObj = document.getElementById("arrow" + index);
    if (buttonSetState[index] == 1) {
       arrowObj.src = normalPtr[1];
    } else {
       arrowObj.src = normalPtr[0];
    }
}

function hoverArrow(index) {
    arrowObj = document.getElementById("arrow" + index);
    if (buttonSetState[index] == 1) {
       arrowObj.src = hoverPtr[1];
    } else {
       arrowObj.src = hoverPtr[0];
    }
}

function rollup(whichdiv) {
    // animation for menu rollup
    //  argument is DOM id for a div
    var i;
    var h;
    var str;
    var hIncrement = initialHeight / nSlices;
    for (i = 1; i < nSlices; i++) {
        h = initialHeight - hIncrement * i;
        str = "setH('" + whichdiv + "','" + h + "px')";
        setTimeout(str,i * delayUnit);
    }
    str = "collapse('" + whichdiv + "')";
    setTimeout(str,i * delayUnit);
}

function rolldown(whichdiv) {
    // animation for menu rolldown
    //  argument is DOM id for a div
    var i = 0;
    var h;
    var str;
    expand(whichdiv);
    var hIncrement = initialHeight / nSlices;
    for (i = 1; i < nSlices; i++) {
        h = hIncrement * i;
        str = "setH('" + whichdiv + "','" + h + "px')";
        setTimeout(str,i * delayUnit);
    }
}

function expand( which ) {
  var objStyle;
  objStyle= document.getElementById(which).style;
  objStyle.display = 'block';
}

 function collapse(which) {
    var objStyle;
   objStyle = document.getElementById(which).style;
    objStyle.display = 'none';
 }

function setH(which, h) {
    var objStyle;
    objStyle = document.getElementById(which).style ;
    objStyle.height = h;
}

 function rollupAllButtons() {
    // buttonSetDivs contains the DOM id's of the div's for the menu sub-buttons
    for (i in buttonSetDivs) {
       if ((buttonSetDivs[i].length > 0) && (buttonSetState[i] == 1)) {
          document.getElementById("arrow" + i).src = normalPtr[0];
          for(j in buttonSetDivs[i]) {
              rollup(buttonSetDivs[i][j]);
          }
          buttonSetState[i] = 0;
       }
    }
}

function openButtonSet(setIndex) {
    document.getElementById("arrow" + setIndex).src = normalPtr[1];
    for(j in buttonSetDivs[setIndex]) {
       rolldown(buttonSetDivs[setIndex][j]);
    }
    buttonSetState[setIndex] = 1;
}


function toggleButtonSet( setIndex ) {
   var tempState = buttonSetState[setIndex];
   rollupAllButtons();
   if (tempState == 0) {
      openButtonSet(setIndex);
   }
}
