/*
 * Derivative of Mike Hall's DHTML menu (http://www.brainjar.com)
 */


//
// Determine browser.
//

var isMinNS4 = (navigator.appName.indexOf("Netscape") >= 0 &&
                parseFloat(navigator.appVersion) >= 4) ? 1 : 0;
var isMinIE4 = (document.all) ? 1 : 0;





//
// Initialize the menubar.
//

var MENUBAR;         // Base layer.
var MENUBARWidth;    // Navigation bar width.
var MAXLEVELS = 20;
var LayerIds = new Array(MAXLEVELS);
var menu = new Array();
var isOverItem = false;


function MENUBARInit() {
  var layer, norm, high, dmmy;
  var width;
  var on, off;
  var i, j, x, y, z;
  var link;
  var linkIdx;


  if(isMinNS4) {
//    document.captureEvents(Event.MOUSEMOVE | Event.MOUSEDOWN | Event.MOUSEUP | Event.MOUSEOVER | Event.MOUSEOUT);
    document.captureEvents(Event.MOUSEMOVE | Event.MOUSEDOWN | Event.MOUSEUP);
    document.onmousedown=MENUBARDownNS;
  }


  if (!isMinNS4 && !isMinIE4)
    return;


  for (i=0; i<MAXLEVELS; i++)
    LayerIds[i] = 0;


  if (MENUBARReloadOnResize) {
    // Fix for resize bug.

    window.onresize = MENUBARReload;
  }


MENUBARWidth = 130;

  // Check sizes.

  if (MENUBARMenuWidth < MENUBARHeaderWidth)
    MENUBARMenuWidth = MENUBARHeaderWidth;

  // Build the navigation bar.

  MENUBARBuild();


  moveLayerTo(MENUBAR, MENUBARX, MENUBARY);
  showLayer(MENUBAR);


 // Get each menu, position it, initialize it and set up event handling.

  x = 0;
  y = 0;
  i = 0;
  linkIdx=0;

  while ((layer = getLayer("menu" + (i + 1), window)) != null) {

    // Set menu width.

    width = MENUBARMenuWidth;
// pkb -- shouldn't need this sizing now that we're vertical

/*
    if (i == MENUBARMenus.length - 1)
      width = Math.max(MENUBARHeaderWidth,
              MENUBARWidth - (MENUBARMenus.length - 1) * MENUBARHeaderWidth);
*/
    // Initialize the menu container.

    menu[i] = layer;
    moveLayerTo(layer, x, y);

    // Initialize the three layers that make up each menu item.

    z = 0;
    j = 0;
    while ((layer = getLayer("item" + (i + 1) + "_" + (j + 1), menu[i])) != null) {

      // Normal item layer.

      norm = layer;
      moveLayerTo(norm, 0, z);



      clipLayer(norm, MENUBARBorderWidth, MENUBARBorderWidth,
                width - MENUBARBorderWidth,
                getHeight(norm) - MENUBARBorderWidth);


      if (isMinNS4 && j > 0)
        layer.visibility = "inherit";
      else
        showLayer(norm);

      // Highlighted item layer.

      high = getLayer("high" + (i + 1) + "_" + (j + 1), menu[i]);
      moveLayerTo(high, 0, z);



      clipLayer(high, MENUBARBorderWidth, MENUBARBorderWidth,
                width - MENUBARBorderWidth,
                getHeight(norm) - MENUBARBorderWidth);


      hideLayer(high);


      // Transparent, dummy item layer. Used to capture mouse events.

      dmmy = getLayer("dmmy" + (i + 1) + "_" + (j + 1), menu[i]);
      moveLayerTo(dmmy, 0, z);


      clipLayer(dmmy, 0, 0, width, getHeight(norm));


      if (j == 0)
        menu[i].high = high;
      else {
        dmmy.high = high;
        dmmy.onmouseover = MENUBARItemOn;
        dmmy.onmouseout  = MENUBARItemOff;

        high.high = high;
        high.onmouseover = MENUBARItemOn;
        high.onmouseout  = MENUBARItemOff;
      }

      link = MENUBARMenus[linkIdx * 3 + 1];


//      link = MENUBARMenus[i][j * 2 + 1];
      if (link != "") {

        if (isMinNS4) {
          dmmy.document.link = link;
          dmmy.document.onmousedown = MENUBARItemGo;

          high.document.link = link;
          high.document.onmousedown = MENUBARItemGo;
        }
        if (isMinIE4) {
          dmmy.link = link;
          dmmy.onmousedown = MENUBARItemGo;

          high.link = link;
          high.onmousedown = MENUBARItemGo;
        }
      }

      // Set up position for next item.

      z += getHeight(norm) - MENUBARBorderWidth;

      if (j == 0)
        off = z + MENUBARBorderWidth - 1;
      on = z + MENUBARBorderWidth;
      j++;
      linkIdx++;
    }


// pkb
//    x += MENUBARHeaderWidth - MENUBARBorderWidth;

    y += off;

    // Set up clipping sizes for menu container.

    menu[i].offWidth  = MENUBARHeaderWidth;
    menu[i].offHeight = off + 1;
    menu[i].onWidth   = width;
    menu[i].onHeight  = on;
    menu[i].open      = false;


    // this onmousedown works under IE but not NS
    if (!MENUBARHoverOpensMenus) {
      menu[i].onmousedown = MENUBARDown;

    }
    menu[i].onmouseover = MENUBAROver;
    menu[i].onmouseout  = MENUBAROut;



/*
      if (MENUBARHoverOpensMenus) {
        menu[i].onmouseover = MENUBARToggle;
        menu[i].onmouseout  = MENUBARToggle;
      }
      else {
        // this onmousedown works under IE but not NS
        menu[i].onmousedown = MENUBARToggle;
        menu[i].onmouseover = MENUBAROver;
        menu[i].onmouseout  = MENUBAROut;
      }
*/


/* test 4 -- needs this one pretty bad */
    clipLayer(menu[i], 0, 0, menu[i].offWidth, menu[i].offHeight);



    // Display the menu container layer.

    showLayer(menu[i]);
    i++;
  }

  // Dummy out mouse events for the filler.


/*
  if (i > 0) {
    menu[i - 1].onmouseover = null;
    menu[i - 1].onmouseout  = null;
  
  // Clip filler to fit window width.

  clipLayer(menu[i - 1], 0, 0, menu[i - 1].onWidth, menu[i - 1].offHeight);
  }
*/


  // Clip base layer so it won't interfere with underlying page.
// pkb: this clip hides all but the first item
//  clipLayer(MENUBAR, 0, 0, MENUBARWidth, menu[0].offHeight);


  // Set tracking.

  if (MENUBARTrackX || MENUBARTrackY) {
    if (isMinNS4)
      MENUBARScroll();
    if (isMinIE4)
      window.onscroll = MENUBARScroll;
  }


}






/*
 *
 * 
 *
 */

function MENUBARBuild() {

  var i, j, k, str, tmp;
  var padding, width;
  var headText, actvText, itemText, highText;
  var tblStart, tblEnd;
  var Level=0;
  var NextLevel=0;


  // Add a dummy menu for filler.

//  MENUBARMenus[MENUBARMenus.length] = new Array("&nbsp;", "");



  // Set up code for menu item text.

  padding = MENUBARPaddingWidth + MENUBARBorderWidth;
  headText = 'color:' + MENUBARHeaderFgColor + ';'
           + 'font-family:' + MENUBARHeaderFontFamily + ';'
           + 'font-size:' + MENUBARHeaderFontSize + ';'
           + 'font-style:' + MENUBARHeaderFontStyle + ';'
           + 'font-weight:' + MENUBARHeaderFontWeight + ';';
  actvText = 'color:' + MENUBARHeaderActiveFgColor + ';'
           + 'font-family:' + MENUBARHeaderFontFamily + ';'
           + 'font-size:' + MENUBARHeaderFontSize + ';'
           + 'font-style:' + MENUBARHeaderFontStyle + ';'
           + 'font-weight:' + MENUBARHeaderFontWeight + ';';
  itemText = 'color:' + MENUBARItemFgColor + ';'
           + 'font-family:' + MENUBARItemFontFamily + ';'
           + 'font-size:' + MENUBARItemFontSize + ';'
           + 'font-style:' + MENUBARItemFontStyle + ';'
           + 'font-weight:' + MENUBARItemFontWeight + ';';
  highText = 'color:' + MENUBARItemHoverFgColor + ';'
           + 'cursor: default;'
           + 'font-family:' + MENUBARItemFontFamily + ';'
           + 'font-size:' + MENUBARItemFontSize + ';'
           + 'font-style:' + MENUBARItemFontStyle + ';'
           + 'font-weight:' + MENUBARItemFontWeight + ';';
  tblStart = '<table border=0 cellpadding=' + padding
           + ' cellspacing=0 width=100%><tr><td>';
  tblEnd   = '</td></tr></table>';



  // Build HTML code for the menus.

  str = "";
//  for (i = 0; i < MENUBARMenus.length; i++) {

    // Set menu width, the filler should be wide enough to fit the window.

    width = MENUBARMenuWidth;
    if (i == MENUBARMenus.length - 1)
      width = Math.max(MENUBARHeaderWidth,
              MENUBARWidth - (MENUBARMenus.length - 1) * MENUBARHeaderWidth);


i=0;
j=0;
    for (k = 0; k < MENUBARMenus.length; k += 3) {

      Level = MENUBARMenus[k + 2];
      NextLevel = MENUBARMenus[k + 5];


      if (NextLevel > Level) {
i++;
        if (isMinNS4)
          str += '<layer name="menu' + (i) + '"'
              +  ' bgcolor="' + MENUBARBorderColor + '"'
              +  ' width=' + width
              +  ' visibility=hidden>\n'
        if (isMinIE4)
          str += '<div id="menu' + (i) + '"'
              +  ' style="position:absolute;;'
              +  ' background-color: ' + MENUBARBorderColor + ';'
              +  ' width=' + width + 'px;'
              +  ' height=100%;'
              +  ' visibility: hidden;">\n';
      }

LayerIds[i]++;
j = LayerIds[i];


//      j = k / 3;
//j++;

      tmp = (i) + "_" + (j);


      if (j == 1) {
        if (isMinNS4) {
          str += '  <layer name="item' + tmp + '" '
              +  ' bgcolor="' + MENUBARHeaderBgColor + '" '
              +  ' width=' + width + '>' + tblStart
              +  '<span style="' + headText + '">' + MENUBARMenus[k]
              +  '</span>' + tblEnd + '</layer>\n';
          str += '  <layer name="high' + tmp + '" '
              +  ' bgcolor="' + MENUBARHeaderActiveBgColor + '" '
              +  ' width=' + width + '>' + tblStart
              +  '<span style="' + actvText + '">' + MENUBARMenus[k]
              +  '</span>' + tblEnd + '</layer>\n';
          str += '  <layer name="dmmy' + tmp + '" '
              +  ' width=' + width + '>'
              +  '</layer>\n';
        }
        if (isMinIE4) {
          str += '  <div id="item' + tmp + '"'
              +  ' style="position:absolute;'
              +  ' background-color:' + MENUBARHeaderBgColor + ';'
              +  ' width:' + width + 'px;">' + tblStart
              +  '<span style="' + headText + '">' + MENUBARMenus[k]
              +  '</span>' + tblEnd + '</div>\n';
          str += '  <div id="high' + tmp + '"'
              +  ' style="position:absolute;'
              +  ' background-color:' + MENUBARHeaderActiveBgColor + ';'
              +  ' width:' + width + 'px;">' + tblStart
              +  '<span style="' + actvText + '">' + MENUBARMenus[k]
              +  '</span>' + tblEnd + '</div>\n';
          str += '  <div id="dmmy' + tmp + '"'
              +  ' style="position:absolute;'
              +  ' width=' + width + ';'
              +  ' height=100%;">'
              +  '</div>\n';
        }
      }
      else {
        if (isMinNS4) {
          str += '  <layer name="item' + tmp + '" '
              +  ' bgcolor="' + MENUBARItemBgColor + '" '
              +  ' width=' + width + '>' + tblStart
              +  '<span style="' + itemText + '">' + MENUBARMenus[k]
              +  '</span>' + tblEnd + '</layer>\n';
          str += '  <layer name="high' + tmp + '" '
              +  ' bgcolor="' + MENUBARItemHoverBgColor + '" '
              +  ' width=' + width + '>' + tblStart
              +  '<span style="' + highText + '">' + MENUBARMenus[k]
              +  '</span>' + tblEnd + '</layer>\n';
          str += '  <layer name="dmmy' + tmp + '" '
              +  ' width=' + width + '>'
              +  '</layer>\n';
        }
        if (isMinIE4) {
          str += '  <div id="item' + tmp + '"'
              +  ' style="position:absolute;'
              +  ' background-color:' + MENUBARItemBgColor + ';'
              +  ' width:' + width + 'px;">' + tblStart
              +  '<span style="' + itemText + '">' + MENUBARMenus[k]
              +  '</span>' + tblEnd + '</div>\n';
          str += '  <div id="high' + tmp + '"'
              +  ' style="position:absolute;'
              +  ' background-color:' + MENUBARItemHoverBgColor + ';'
              +  ' width:' + width + 'px;">' + tblStart
              +  '<span style="' + highText + '">' + MENUBARMenus[k]
              +  '</span>' + tblEnd + '</div>\n';
          str += '  <div id="dmmy' + tmp + '"'
              +  ' style="position:absolute;'
              +  ' width=' + width + ';'
              +  ' height=100%;">'
              +  '</div>\n';
        }
      }




      // pkb
      if (NextLevel < Level) {
        if (isMinNS4)
          str += '</layer>\n';
        if (isMinIE4)
          str += '</div>\n';

//j=0;
      }



    }



for(j=1; j<Level-1; j++) {
        if (isMinNS4)
          str += '</layer>\n';
        if (isMinIE4)
          str += '</div>\n';
}



    if (isMinNS4)
      str += '</layer>\n';
    if (isMinIE4)
      str += '</div>\n';

//  }


//return;

  // Create it as a new layer.

  if (isMinNS4) {
    MENUBAR = new Layer(width);
    MENUBAR.document.writeln(str);
    MENUBAR.document.close();

    clipLayer(MENUBAR, 0, 0, MENUBARWidth, getWindowHeight());
  }
  if (isMinIE4) {
    str = '<div id="MENUBAR" style="position:absolute;'
        + ' width:' + MENUBARWidth + 'px;'
        //+ ' height:100%;">'
        + ' ">'
        + str + '</div>\n';
    document.body.insertAdjacentHTML("BeforeEnd",str);
    MENUBAR = getLayer("MENUBAR");
  }
}



//*****************************************************************************
// Event handlers for the navigation bar.
//*****************************************************************************

function MENUBARScroll() {

  var x, y;

  // Resposition navigation bar.

  x = MENUBARX;
  if (MENUBARTrackX)
    x += getPageScrollX();
  y = MENUBARY;
  if (MENUBARTrackY)
    y += getPageScrollY();
  moveLayerTo(MENUBAR, x, y);

  // Set up next call.

  if (isMinNS4)
    setTimeout('MENUBARScroll()', 25);

  return;
}


function MENUBARToggle(Idx) {

var i;
var layer;

/*
    if (menu[i].isOver) {
      alert('over ' + i);
      break;
    }
*/


  i = 1;

  if (MENUBARLimitOpenMenus) {
    while ((layer = getLayer("menu" + i, window)) != null) {
      if (!menu[i-1].isOver && menu[i-1].open) {
        menu[i-1].open = false;
        CollapseMenu(layer);
      }

      i++;
    }
  }

// Now, toggle the selected menu

  layer = getLayer("menu" + Idx, window)

  if (menu[Idx-1].open) {
    menu[Idx-1].open = false;
    CollapseMenu(layer);
  } 
  else {
    menu[Idx-1].open = true;
    ExpandMenu(layer);
  }
}

function MENUBARMenuOn() {
  ExpandMenu(this);
}

function ExpandMenu(what) {

  // Display the menu by clipping containter to full view.

  clipLayer(what, 0, 0, what.onWidth, what.onHeight);

//pkb -- hides menu when mouse enters
//  clipLayer(MENUBAR, 0, 0, MENUBARWidth, what.onHeight);
  showLayer(what.high);

// pkb adding

var i;
var y;
var layer;
var thisFound;
var offHeight = menu[0].offHeight-1;



  i = 1;
  y = 0;
  thisFound = false;

  while ((layer = getLayer("menu" + i, window)) != null) {
    
    if (thisFound) {
      moveLayerTo(layer, 0, y);
    } 
    else {
      if (layer == what) {
        thisFound = true;
      }

    }


    if (menu[i-1].open) {
      y += menu[i-1].onHeight;
    }
    else {

      y += offHeight;
    }

    i++;
  }
}

function MENUBARMenuOff() {
  CollapseMenu(this);
}

function CollapseMenu(what) {

  // Hide the menu by clipping container to show only the header item.

  if (isMinIE4 && window.event.toElement &&
      window.event.srcElement.id.substr(4, 1) ==
      window.event.toElement.id.substr(4, 1))
    return;

  clipLayer(what, 0, 0, what.offWidth, what.offHeight);
//pkb -- hides menu when mouse leaves
//  clipLayer(MENUBAR, 0, 0, MENUBARWidth, what.offHeight);
  hideLayer(what.high);



// pkb adding

var i;
var y;
var layer;
var thisFound;
var offHeight = menu[0].offHeight-1;



  i = 1;
  y = 0;
  thisFound = false;

  while ((layer = getLayer("menu" + i, window)) != null) {

    if (thisFound)
      moveLayerTo(layer, 0, y);
    else {
      if ( layer == what ) 
        thisFound = true;
    }


    if (menu[i-1].open) {
      y += menu[i-1].onHeight;
    } 
    else {
      y += offHeight;
    }


    i++;
  }
}

function MENUBARItemOn() {

  if (isMinNS4) {
    isOverItem = true;
  }

  // Show the highlighted layer for this item.

  showLayer(this.high)
}

function MENUBARItemOff() {

  if (isMinNS4) {
    isOverItem = false;
  }


  // Hide the highlighted layer for this item.

  hideLayer(this.high)
}

function MENUBARItemGo() {

  // If the link starts with "javascript:" execute the code. Otherwise just
  // link to the URL.


//
// NS records JS errors on this but still works...
// haven't been able to get rid of the errors.
//
 
  if (this.link.indexOf("javascript:") == 0) {
    eval(this.link);
  } 
  else {
    if (MENUBARContentFrame == "")
      window.location.href = this.link;
    else
      parent.frames[MENUBARContentFrame].location.href = this.link;
  }



  // Cancel event bubbling so that clicking the menu 
  // options doesn't collapse the menu.

  if (isMinIE4)
    window.event.cancelBubble = true;

  return false;
}

function MENUBARReload() {

  // Reload page in case of a browser resize.

  window.location.href = window.location.href;
}

//*****************************************************************************
// Cross-browser layer functions.
//*****************************************************************************

function hideLayer(layer) {

  if (isMinNS4)
    layer.visibility = "hide";
  if (isMinIE4)
    layer.style.visibility = "hidden";
}

function showLayer(layer) {

  if (isMinNS4)
    layer.visibility = "show";
  if (isMinIE4)
    layer.style.visibility = "visible";
}

function moveLayerTo(layer, x, y) {

  if (isMinNS4)
    layer.moveTo(x, y);
  if (isMinIE4) {
    layer.style.left = x;
    layer.style.top  = y;
  }
}

function clipLayer(layer, clipleft, cliptop, clipright, clipbottom) {

  if (isMinNS4) {
    layer.clip.left   = clipleft;
    layer.clip.top    = cliptop;
    layer.clip.right  = clipright;
    layer.clip.bottom = clipbottom;
  }
  if (isMinIE4)
    layer.style.clip = 'rect(' + cliptop + ' ' +  clipright + ' ' + clipbottom + ' ' + clipleft +')';
}

function getHeight(layer) {

  if (isMinNS4) {
    if (layer.document.height)
      return(layer.document.height);
    else
      return(layer.clip.bottom - layer.clip.top);
  }
  if (isMinIE4) {
    if (false && layer.style.pixelHeight)
      return(layer.style.pixelHeight);
    else
      return(layer.clientHeight);
  }
  return(-1);
}

function getLayer(name) {

  if (isMinNS4)
    return findLayer(name, document);
  if (isMinIE4)
    return eval('document.all.' + name);

  return null;
}

function findLayer(name, doc) {

  var i, layer;

  for (i = 0; i < doc.layers.length; i++) {
    layer = doc.layers[i];
    if (layer.name == name)
      return layer;
    if (layer.document.layers.length > 0) {
      layer = findLayer(name, layer.document);
      if (layer != null)
        return layer;
    }
  }

  return null;
}

function getWindowWidth() {

  if (isMinNS4)
    return(window.innerWidth);
  if (isMinIE4)
    return(document.body.offsetWidth);
  return(-1);
}

function getWindowHeight() {

  if (isMinNS4)
    return(window.innerHeight);
  if (isMinIE4)
    return(document.body.offsetHeight);
  return(-1);
}

function getPageScrollX() {

  if (isMinNS4)
    return(window.pageXOffset);
  if (isMinIE4)
    return(document.body.scrollLeft);
  return(-1);
}

function getPageScrollY() {

  if (isMinNS4)
    return(window.pageYOffset);
  if (isMinIE4)
    return(document.body.scrollTop);
  return(-1);
}

function getPageWidth() {

  if (isMinNS4)
    return(document.width);
  if (isMinIE4)
    return(document.body.scrollWidth);
  return(-1);
}

function MENUBARDown()
{
var Idx;

  Idx = GetLayerIndex("menu", this);
  MENUBARToggle(Idx);
}


function MENUBARDownNS()
{
var i=1;

  if (isOverItem)
    MENUBARItemGo();
  else {

    while ((layer = getLayer("menu" + i, window)) != null) {
      if (menu[i-1].isOver) {
        MENUBARToggle(i);
        break;
      }
      i++;
    }
  }


}



function MENUBAROver()
{
var Idx;

  Idx = GetLayerIndex("menu", this);
  menu[Idx-1].isOver = true;

  if (MENUBARHoverOpensMenus) {
    MENUBARToggle(Idx);
  }
}

function MENUBAROut()
{
var Idx;

  Idx = GetLayerIndex("menu", this);
  menu[Idx-1].isOver = false;

  if (MENUBARHoverOpensMenus) {
    MENUBARToggle(Idx);
  }
}


function GetLayerIndex(prefix, matchThis)
{
var i=1;
var layer;

  while ((layer = getLayer(prefix + i, window)) != null) {
    if ( layer == matchThis) {
      return i;
    }
    i++;
  }
}

