/************************************************
** Advanced Customizable Text Menu Script 1.0  **
** Written by Jonathan Lipps                   **
** Copyright 2002                              **
** http://www.jonathanlipps.com/               **
*************************************************
*************************************************
** This script is free for public use, as long **
** as this message, and all other comments     **
** remain intact and no attempt is made to     **
** claim this script as original. Thanks!      **
*************************************************
*************************************************
** Installation and Use:                       **
** This is the script file, which you will need**
** to call from your html body with the        **
** <script src="textmenu.js"> method. There is **
** also a script which you need to place in the**
** html file--it contains the options you use  **
** to customize the appearance of the menu.    **
** Instructions for customization are included **
** with that portion of the script.            **
*************************************************
*************************************************
** If you find any bugs in this script, please **
** e-mail the author at jon832@stanford.edu    **
************************************************/

// This is the textmenu.js source file, version 1.0
// This script works with IE5+ PC/MAC and NN6+ PC/MAC
// NN4 just can't handle it.

var nummenus; var menuwidth; // Global variable containing the main menuitem width

function verifyCompatibleBrowser(){ 
    this.ver=navigator.appVersion 
    this.dom=document.getElementById?1:0 
    this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom)?1:0; 
    this.ie4=(document.all && !this.dom)?1:0; 
    this.ns5=(this.dom && parseInt(this.ver) >= 5) ?1:0; 
 
    this.ns4=(document.layers && !this.dom)?1:0; 
    this.bw=(this.ie5 || this.ie4 || this.ns4 || this.ns5) 
    return this 
} 
bw=new verifyCompatibleBrowser() 

// Prints the style sheet
function setStyle()
{
  var str = "<style type='text/css'>\n";

  str += ".menulink {text-decoration:none;}\n";

  str += ".menu { position:absolute;font-family:" + menutextfont + ";font-size:" + menutextsize + ";color:" + menutextcolor;
  str += ";border:" + menuborderwidth + "px " + menuborderstyle + " " + menubordercolor + ";height:" + (menuheight) + "px";
  str += ";background: " + menubgcolor + "; padding-left:0px;padding-right:0px;"+menufontstyle;
  str += ";padding-top:"+menutoppad+"px;padding-bottom:0px;vertical-align:text-bottom;z-index:3}\n";

  str += ".submenu { position:absolute;font-family:" + subtextfont + ";font-size:" + subtextsize + ";color:" + subtextcolor;
  str += ";border:" + subborderwidth + "px " + subborderstyle + " " + subbordercolor + ";height:" + subheight + "px";
  str += ";background: " + subbgcolor + "; padding-left:0px;padding-right:0px;"+subfontstyle;
  str += ";padding-top:"+subtoppad+"px;padding-bottom:0px;z-index:3;vertical-align:middle;}\n";

  str += ".hanger { position:absolute;border-left:" + hangerthickness + "px ";
  str += hangerstyle + " " + hangercolor + "; border-right:" + hangerthickness + "px " + hangerstyle + " " + hangercolor;
  str += ";background:" + hangerbgcolor + ";font-size:0pt;z-index:2;}\n";

  str += "</style>\n";
 
  document.write(str);
}

// Shows or hides submenus, based on the name sent to it.
// Operation is different when user has click-driven menus enabled
function showHideDiv(name)
{
  var thismenu = nummenus + 1;
  var catchobj = document.getElementById('catchmouseoffs') ? document.getElementById('catchmouseoffs').style : '';
  var changed=false;

  if (parseInt(name.charAt(4))) thismenu = parseInt(name.charAt(4));
  for (var i=1; i < (nummenus + 1); i++)
    if(document.getElementById("submenu" + i))
    {
      obj = document.getElementById("submenu" + i).style;
      obj.visibility = (i==thismenu) ? ((click && (obj.visibility=='visible')) ? 'hidden' : 'visible') : 'hidden';
      catchobj.visibility='hidden';
    }
  if (thismenu < (nummenus + 1))
    catchobj.visibility='visible';
}

// Changes the background color of a menuitem. First it checks
// to see if name is a mainmenu or a submenu, then if we want 
// the highlight or the normal color, and0 changes the color accordingly.
function changeBackground(name, on)
{
  obj = document.getElementById(name).style;
  obj.background = (name.charAt(0)=='m') ? on ? menuovercolor: menubgcolor : on ? subovercolor : subbgcolor;
}

function Init()
{

  if(bw.ns5) 
  {
    menuheight -= menuborderwidth*2;
    subheight -= subborderwidth*2;
    menubetween += menuborderwidth*2;
    hangerheight += menuborderwidth*2;
    subbetween += subborderwidth*2;
  }
  setStyle();
}

// Writes the mainmenu divisions based on the user script.
function makeMain()
{
  var text, link, sub, i=0, str, j=1, menuleft;
  var args = makeMain.arguments;

  // Make sure user didn't mess up inputting menuitems
  if ((args.length%3) == 0)  nummenus = args.length / 3;
  else alert('Error: incorrect number of arguments for creation of main menu');
  
  menuwidth = (parseInt(allmenuwidth / nummenus)) - menubetween;
  
  str = "";

  // We need to make a DIV to catch mouseoffs so we can hide the submenus, and IE6 needs something IN the DIV,
  // hence the table
  if (!click)
  {
    str = '<div id = "catchmouseoffs" style="position:absolute;left:0px;top:0px;width:100%;height:600px;z-index:2;visibility:visible;" onmouseover="showHideDiv(\'all\');">';
    str += '<table width="100%" height="100%" cellspacing=0 cellpadding=0><tr><td></td></tr></table></div>\n\n';
  }
  while (i < args.length-1)
  {
    menuleft = allxdisp + (j-1)*(menuwidth+menubetween);
    if (!((menubetween>0) || (menuover==1)) || (bw.ns5 && !((menubetween>(menuborderwidth*2)) || (menuover==1)))) menuleft-=(j-1)*menuborderwidth; 
    text = args[i++];  link = args[i++]; sub = args[i++];
    if (link) str+= '<a class="menulink" href="'+link+'">';

    str += '<div id="menu'+j+'" class="menu" style="position:absolute;left:'+menuleft+'px;top:'+allydisp+'px;width:'+menuwidth+'px;" on';
    if (click)
    {
      str+= 'click="showHideDiv(';
      if (sub) str += '\'menu'+j+'\'';
      else str += '\'all\'';
      str += ')"; onmouseover="';
    }
    else
    {
      str += 'mouseover="showHideDiv(';
      if (sub) str += '\'menu'+j+'\'';
      else str += '\'all\'';
      str += ');';
    }
    str += 'changeBackground(\'menu' + j + '\', true)" onmouseout="changeBackground(\'menu'+j+'\', false);">';
    for (var k=0;k<menuleftpad;k++)
      str += '&nbsp;';
    str += text + '</div>';
    if (link) str += '</a>';
    j++;
  }
  document.write(str);
}

// Writes the submenu divisions
function makeSub()
{
  var num, text, link, i=0, str, j=1;
  var subleft, subtop, subwidth, hangerwidthpx, hangerleft;
  var args = makeSub.arguments;

  subwidth = menuwidth - subleftindent - subrightindent;
  hangerwidthpx = parseInt(subwidth * (hangerwidth/100));

  if (!((args.length)%2 == 1)) alert('Error: invalid number of arguments for creation of submenu'+args[i]+'!');
  num = args[i++];

  subleft = (num-1)*(menuwidth+menubetween) + allxdisp + subleftindent + suboffset;
  if (!((menubetween>0) || (menuover==1)) || (bw.ns5 && !((menubetween>(menuborderwidth*2)) || (menuover==1))))
  { 
    subleft-=(num-1)*menuborderwidth; 
    subleft+=parseInt((menuborderwidth/2));
    subwidth-=(menuborderwidth-subborderwidth);
  }
  subtop = 0;
  hangerleft = parseInt(((subwidth-hangerwidthpx) / 2)) + hangeroffset;
  str = '';
  str = '<div id="submenu' + num + '" style="position:absolute;left:'+subleft+'px;top:'+(allydisp + menuheight + hangerdrop - (bw.ns5?0:1))+'px;';
  str += 'width:'+subwidth+'px;z-index:3;visibility:hidden">\n<table cellpadding=0 cellspacing=0><tr><td>\n';

  if(hangerheight)
  {
    str += '<div style="position:relative;left:0px;top:'+subtop+'px;width:'+(hangerleft)+'px;height:'+hangerheight+'px;z-index:2;"></div>\n';
    str += '<div id="hanger'+num+'" class="hanger" style="left:'+hangerleft+'px;';
    str += 'top:'+subtop+'px;width:'+hangerwidthpx+'px;height:'+hangerheight+'px;"></div>\n';
    str += '<div style="position:relative;left:'+(hangerleft+hangerwidthpx)+'px;top:'+subtop+'px;width:'+(hangerleft-(hangeroffset*2))+'px;height:'+hangerheight+'px;z-index:2"></div>\n';
    subtop += hangerheight;
  }
  if (menuover && !(menubetween>0)) subleft+=menuborderwidth;
  while (i < args.length-1)
  {
    text = args[i++]; link = args[i++];
    str += '<a class="menulink" href="'+link+'">';
    str += '<div id="submenu'+num+'-'+j+'" class="submenu"';
    str += ' style="left:0px;top:'+subtop+'px;width:'+subwidth+'px;"';
    str += ' onmouseover="changeBackground(\'submenu'+num+'-'+j+'\', true);"';
    str += ' onmouseout="changeBackground(\'submenu'+num+'-'+j+'\', false);">';
    for (var k=0;k<subleftpad;k++)
      str += '&nbsp;';
    str += text + '</div></a>\n';
    subtop += subheight;
    if (i < args.length-1)
    {
      if (subbetween>0)
      {
        str += '<div style="position:absolute;left:0px;top:'+subtop+'px;width:';
        if (midhanger)
        {
          str += (hangerleft-subleft)+'px;height:'+subbetween+'px;z-index:2;"></div>\n';
          str += '<div id="midhanger'+num+'-'+j+'" class="hanger" style="left:'+hangerleft+'px;';
          str += 'top:'+subtop+'px;width:'+hangerwidthpx+'px;height:'+subbetween+'px;"></div>\n';
          str += '<div style="position:absolute;left:'+(hangerleft+hangerwidthpx)+'px;top:'+subtop+'px;width:'+(hangerleft-(hangeroffset*2));
        }
        else
          str += subwidth;
        str += 'px;height:'+subbetween+'px;z-index:2;"></div>\n';
        subtop += subbetween;  
      }
      if (!((subbetween>0) || (subdrop>0)) || (bw.ns5 && !((subbetween>(subborderwidth*2)) || (subdrop==1)))) subtop-=subborderwidth;
      j++;
    }

  }
  if (!click) // circumvents a bug which disallowed activation of mouseoff layer directly beneath submenuX
  {
    str += '<div style="position:absolute;left:0px;top:'+subtop+'px;width:'+subwidth+'px;height:15px;z-index:2;"';
    str += ' onmouseover="showHideDiv(\'all\');"></div>\n';
  }
  str += '</div></tr></td></table></div>\n';
  document.write(str);

}