// Morten's JavaScript Tree Menu
// written by Morten Wang <morten@treemenu.com> (c) 1998-2000
// This is version 2.2.6, dated 2000-03-30

// The script is freely distributable
// It may be used (and modified) as you wish, but retain this message
// For more information about the menu visit its home page
// http://www.treemenu.com/

/******************************************************************************
* Define the MenuItem object.                                                 *
******************************************************************************/
function PBBenuItem(text, url, target, icon) {
  this.text = text;
  this.url = url ? url : "";
  this.target =  target ? target : "";
  this.icon = icon ? icon : "";

  this.number = PBBSubNumber++;

  this.submenu     = null;
  this.expanded    = false;
  this.PBBakeSubmenu = PBBakeSubmenu;
}

function PBBakeSubmenu(menu) {
  this.submenu = menu;
}

/******************************************************************************
* Define the Menu object.                                                     *
******************************************************************************/

function PBBenu() {
  this.items   = new Array();
  this.PBBAddItem = PBBAddItem;
}

function PBBAddItem(item) {
  this.items[this.items.length] = item;
}

/******************************************************************************
* Define the icon list, addIcon function and PBBIcon item.                    *
******************************************************************************/

function IconList() {
  this.items = new Array();
  this.addIcon = addIcon;
}

function addIcon(item) {
  this.items[this.items.length] = item;
}

function PBBIcon(iconfile, match, type) {
  this.file = iconfile;
  this.match = match;
  this.type = type;
}

/******************************************************************************
* Global variables.  Not to be altered unless you know what you're doing.     *
* User-configurable options are at the end of this document.                  *
******************************************************************************/

var PBBLoaded = false;
var PBBLevel;
var PBBBar = new Array();
var PBBIndices = new Array();
var PBBBrowser = null;
var PBBNN3 = false;
var PBBNN4 = false;
var PBBIE4 = false;
var PBBUseStyle = true;

if(navigator.appName == "Netscape" && navigator.userAgent.indexOf("WebTV") == -1) {
  if(parseInt(navigator.appVersion) == 3 && (navigator.userAgent.indexOf("Opera") == -1)) {
    PBBBrowser = true;
    PBBNN3 = true;
    PBBUseStyle = false;
  } else if(parseInt(navigator.appVersion) >= 4) {
    PBBBrowser = true;
    PBBNN4 = true;
  }
} else if (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) >= 4) {
  PBBBrowser = true;
  PBBIE4 = true;
}

var PBBClickedItem = false;
var PBBExpansion = false;

var PBBSubNumber = 1;
var PBBTrackedItem = false;
var PBBTrack = false;

var PBBPreHREF = "";
if(PBBIE4 || PBBNN3) {
  PBBPreHREF += document.location.href.substring(0, document.location.href.lastIndexOf("/") +1);
}

var PBBFirstRun = true;
var PBBCurrentTime = 0; // for checking timeout.
var PBBUpdating = false;
var PBBWinSize, PBByval;
var PBBOutputString = "";

/******************************************************************************
* Code that picks up frame names of frames in the parent frameset.            *
******************************************************************************/

if(PBBBrowser) {
  var PBBFrameNames = new Array();
  for(i = 0; i < parent.frames.length; i++)
    PBBFrameNames[i] = parent.frames[i].name;
}

/******************************************************************************
* Dummy function for sub-menus without URLs                                   *
* Thanks to Michel Plungjan for the advice. :)                                *
******************************************************************************/

function myVoid() { ; }

/******************************************************************************
* Functions to draw the menu.                                                 *
******************************************************************************/

function PBBSubAction(SubItem, ReturnValue) {

  SubItem.expanded = (SubItem.expanded) ? false : true;
  if(SubItem.expanded) {
    PBBExpansion = true;
  }

  PBBClickedItem = SubItem.number;

  if(PBBTrackedItem && PBBTrackedItem != SubItem.number) {
    PBBTrackedItem = false;
  }

  if(!ReturnValue) {
    setTimeout("PBBDisplayMenu()", 10);
  }

  return ReturnValue;
}

function PBBStartMenu() {
  PBBLoaded = true;
  if(PBBFirstRun) {
    PBBCurrentTime++;
    if(PBBCurrentTime == PBBTimeOut) { // call PBBDisplayMenu
      setTimeout("PBBDisplayMenu()",10);
    } else {
      setTimeout("PBBStartMenu()",100);
    }
  } 
}

function PBBDisplayMenu() {
  if(PBBBrowser && !PBBUpdating) {
    PBBUpdating = true;
    PBBFirstRun = false;

    if(PBBTrack) { PBBTrackedItem = PBBTrackExpand(menu); }

    if(PBBExpansion && PBBSubsAutoClose) { PBBCloseSubs(menu); }

    PBBLevel = 0;
    PBBDoc = parent.frames[PBBenuFrame].document
    PBBDoc.open("text/html", "replace");
    PBBOutputString = '<html><head>';
    if(PBBLinkedSS) {
      PBBOutputString += '<link rel="stylesheet" type="text/css" href="' + PBBPreHREF + PBBSSHREF + '">';
    } else if(PBBUseStyle) {
      PBBOutputString += '<style type="text/css">body {color:' + PBBTextColor + ';background:';
      PBBOutputString += (PBBBackground == "") ? PBBBGColor : PBBakeBackImage(PBBBackground);
      PBBOutputString += ';} #root {color:' + PBBRootColor + ';background:' + ((PBBBackground == "") ? PBBBGColor : 'transparent') + ';font-family:' + PBBRootFont + ';font-size:' + PBBRootCSSize + ';} ';
      PBBOutputString += 'a {font-family:' + PBBenuFont + ';font-size:' + PBBenuCSSize + ';text-decoration:none;color:' + PBBLinkColor + ';background:' + PBBakeBackground() + ';} ';
      PBBOutputString += PBBakeA('pseudo', 'hover', PBBAhoverColor);
      PBBOutputString += PBBakeA('class', 'tracked', PBBTrackColor);
      PBBOutputString += PBBakeA('class', 'subexpanded', PBBSubExpandColor);
      PBBOutputString += PBBakeA('class', 'subclosed', PBBSubClosedColor) + '</style>';
    }

    PBBOutputString += '</head><body ';
    if(PBBBackground != "") {
      PBBOutputString += 'background="' + PBBPreHREF + PBBenuImageDirectory + PBBBackground + '" ';
    }
    PBBOutputString += 'bgcolor="' + PBBBGColor + '" text="' + PBBTextColor + '" link="' + PBBLinkColor + '" vlink="' + PBBLinkColor + '" alink="' + PBBLinkColor + '">';
    PBBOutputString += '<table border="0" cellpadding="0" cellspacing="0" width="' + PBBTableWidth + '">';
    PBBOutputString += '<tr valign="top"><td nowrap><img src="' + PBBPreHREF + PBBenuImageDirectory + PBBRootIcon + '" align="left" border="0" vspace="0" hspace="0">';
    if(PBBUseStyle) {
      PBBOutputString += '<span id="root">&nbsp;' + PBBenuText + '</span>';
    } else {
      PBBOutputString += '<font size="' + PBBRootFontSize + '" face="' + PBBRootFont + '" color="' + PBBRootColor + '">' + PBBenuText + '</font>';
    }
    PBBDoc.writeln(PBBOutputString + '</td></tr>');

    PBBListItems(menu);

    PBBDoc.writeln('</table></body></html>');
    PBBDoc.close();

    if((PBBClickedItem || PBBTrackedItem) && (PBBNN4 || PBBIE4) && !PBBFirstRun) {
      PBBItemName = "sub" + (PBBClickedItem ? PBBClickedItem : PBBTrackedItem);
      if(document.layers && parent.frames[PBBenuFrame].scrollbars) {    
        PBByval = parent.frames[PBBenuFrame].document.anchors[PBBItemName].y;
        PBBWinSize = parent.frames[PBBenuFrame].innerHeight;
      } else {
        PBByval = PBBGetPos(parent.frames[PBBenuFrame].document.all[PBBItemName]);
        PBBWinSize = parent.frames[PBBenuFrame].document.body.offsetHeight;
      }
      if(PBByval > (PBBWinSize - 60)) {
        parent.frames[PBBenuFrame].scrollBy(0, parseInt(PBByval - (PBBWinSize * 1/3)));
      }
    }

    PBBClickedItem = false;
    PBBExpansion = false;
    PBBTrack = false;
  }
PBBUpdating = false;
}

function PBBListItems(menu) {
  var i, isLast;
  for (i = 0; i < menu.items.length; i++) {
    PBBIndices[PBBLevel] = i;
    isLast = (i == menu.items.length -1);
    PBBDisplayItem(menu.items[i], isLast);

    if (menu.items[i].submenu && menu.items[i].expanded) {
      PBBBar[PBBLevel] = (isLast) ? false : true;
      PBBLevel++;
      PBBListItems(menu.items[i].submenu);
      PBBLevel--;
    } else {
      PBBBar[PBBLevel] = false;
    } 
  }
}

function PBBDisplayItem(item, last) {
  var i, img, more;

  if(item.submenu) {
    var PBBouseOverText;

    var PBBClickCmd;
    var PBBDblClickCmd = false;
    var PBBfrm = "parent.frames['code']";
    var PBBref = '.menu.items[' + PBBIndices[0] + ']';

    if(PBBLevel > 0) {
      for(i = 1; i <= PBBLevel; i++) {
        PBBref += ".submenu.items[" + PBBIndices[i] + "]";
      }
    }

    if(!PBBEmulateWE && !item.expanded && (item.url != "")) {
      PBBClickCmd = "return " + PBBfrm + ".PBBSubAction(" + PBBfrm + PBBref + ",true);";
    } else {
      PBBClickCmd = "return " + PBBfrm + ".PBBSubAction(" + PBBfrm + PBBref + ",false);";
    }

    if(item.url == "") {
      PBBouseOverText = (item.text.indexOf("'") != -1) ? PBBEscapeQuotes(item.text) : item.text;
    } else {
      PBBouseOverText = "Expand/Collapse";
    }
  }

  PBBOutputString = '<tr valign="top"><td nowrap>';
  if(PBBLevel > 0) {
    for (i = 0; i < PBBLevel; i++) {
      PBBOutputString += (PBBBar[i]) ? PBBakeImage("menu_bar.gif") : PBBakeImage("menu_pixel.gif");
    }
  }

  more = false;
  if(item.submenu) {
    if(PBBSubsGetPlus || PBBEmulateWE) {
      more = true;
    } else {
      for (i = 0; i < item.submenu.items.length; i++) {
        if (item.submenu.items[i].submenu) {
          more = true;
        }
      }
    }
  }
  if(!more) {
    img = (last) ? "menu_corner.gif" : "menu_tee.gif";
  } else {
    if(item.expanded) {
      img = (last) ? "menu_corner_minus.gif" : "menu_tee_minus.gif";
    } else {
      img = (last) ? "menu_corner_plus.gif" : "menu_tee_plus.gif";
    }
    if(item.url == "" || item.expanded || PBBEmulateWE) {
      PBBOutputString += PBBakeVoid(item, PBBClickCmd, PBBouseOverText);
    } else {
      PBBOutputString += PBBakeLink(item, true)  + ' onclick="' + PBBClickCmd + '">';
    }
  }
  PBBOutputString += PBBakeImage(img);

  if(item.submenu) {
    if(PBBEmulateWE && item.url != "") {
      PBBOutputString += '</a>' + PBBakeLink(item, false) + '>';
    }

    img = (item.expanded) ? "menu_folder_open.gif" : "menu_folder_closed.gif";

    if(!more) {
      if(item.url == "" || item.expanded) {
        PBBOutputString += PBBakeVoid(item, PBBClickCmd, PBBouseOverText);
      } else {
        PBBOutputString += PBBakeLink(item, true) + ' onclick="' + PBBClickCmd + '">';
      }
    }
    PBBOutputString += PBBakeImage(img);

  } else {
    PBBOutputString += PBBakeLink(item, true) + '>';
    img = (item.icon != "") ? item.icon : PBBFetchIcon(item.url);
    PBBOutputString += PBBakeImage(img);
  }

  if(item.submenu && (item.url != "") && (item.expanded && !PBBEmulateWE)) {
    PBBOutputString += '</a>' + PBBakeLink(item, false) + '>';
  }

  if(PBBNN3 && !PBBLinkedSS) {
    var stringColor;
    if(item.submenu && (item.url == "") && (item.number == PBBClickedItem)) {
      stringColor = (item.expanded) ? PBBSubExpandColor : PBBSubClosedColor;
    } else if(PBBTrackedItem && PBBTrackedItem == item.number) {
      stringColor = PBBTrackColor;
    } else {
      stringColor = PBBLinkColor;
    }
    PBBOutputString += '<font color="' + stringColor + '" size="' + PBBenuFontSize + '" face="' + PBBenuFont + '">';
  }
  PBBOutputString += '&nbsp;' + item.text + ((PBBNN3 && !PBBLinkedSS) ? '</font>' : '') + '</a>' ;
  PBBDoc.writeln(PBBOutputString + '</td></tr>');
}

function PBBEscapeQuotes(myString) {
  var newString = "";
  var cur_pos = myString.indexOf("'");
  var prev_pos = 0;
  while (cur_pos != -1) {
    if(cur_pos == 0) {
      newString += "\\";
    } else if(myString.charAt(cur_pos-1) != "\\") {
      newString += myString.substring(prev_pos, cur_pos) + "\\";
    } else if(myString.charAt(cur_pos-1) == "\\") {
      newString += myString.substring(prev_pos, cur_pos);
    }
    prev_pos = cur_pos++;
    cur_pos = myString.indexOf("'", cur_pos);
  }
  return(newString + myString.substring(prev_pos, myString.length));
}

function PBBTrackExpand(thisMenu) {
  var i, targetPath;
  var foundNumber = false;
  for(i = 0; i < thisMenu.items.length; i++) {
    if(thisMenu.items[i].url != "" && PBBTrackTarget(thisMenu.items[i].target)) {
      targetPath = parent.frames[thisMenu.items[i].target].location.pathname;
      if(targetPath.lastIndexOf(thisMenu.items[i].url) != -1 && (targetPath.lastIndexOf(thisMenu.items[i].url) + thisMenu.items[i].url.length) == targetPath.length) {
        return(thisMenu.items[i].number);
      }
    }
    if(thisMenu.items[i].submenu) {
      foundNumber = PBBTrackExpand(thisMenu.items[i].submenu);
      if(foundNumber) {
        if(!thisMenu.items[i].expanded) {
          thisMenu.items[i].expanded = true;
          if(!PBBClickedItem) { PBBClickedItem = thisMenu.items[i].number; }
          PBBExpansion = true;
        }
        return(foundNumber);
      }
    }
  }
return(foundNumber);
}

function PBBCloseSubs(thisMenu) {
  var i, j;
  var foundMatch = false;
  for(i = 0; i < thisMenu.items.length; i++) {
    if(thisMenu.items[i].submenu && thisMenu.items[i].expanded) {
      if(thisMenu.items[i].number == PBBClickedItem) {
        foundMatch = true;
        for(j = 0; j < thisMenu.items[i].submenu.items.length; j++) {
          if(thisMenu.items[i].submenu.items[j].expanded) {
            thisMenu.items[i].submenu.items[j].expanded = false;
          }
        }
      } else {
        if(foundMatch) {
          thisMenu.items[i].expanded = false; 
        } else {
          foundMatch = PBBCloseSubs(thisMenu.items[i].submenu);
          if(!foundMatch) {
            thisMenu.items[i].expanded = false;
          }
        }
      }
    }
  }
return(foundMatch);
}

function PBBFetchIcon(testString) {
  var i;
  for(i = 0; i < PBBIconList.items.length; i++) {
    if((PBBIconList.items[i].type == 'any') && (testString.indexOf(PBBIconList.items[i].match) != -1)) {
      return(PBBIconList.items[i].file);
    } else if((PBBIconList.items[i].type == 'pre') && (testString.indexOf(PBBIconList.items[i].match) == 0)) {
      return(PBBIconList.items[i].file);
    } else if((PBBIconList.items[i].type == 'post') && (testString.indexOf(PBBIconList.items[i].match) != -1)) {
      if((testString.lastIndexOf(PBBIconList.items[i].match) + PBBIconList.items[i].match.length) == testString.length) {
        return(PBBIconList.items[i].file);
      }
    }
  }
return("menu_link_default.gif");
}

function PBBGetPos(myObj) {
  return(myObj.offsetTop + ((myObj.offsetParent) ? PBBGetPos(myObj.offsetParent) : 0));
}

function PBBCheckURL(myURL) {
  var tempString = "";
  if((myURL.indexOf("http://") == 0) || (myURL.indexOf("https://") == 0) || (myURL.indexOf("mailto:") == 0) || (myURL.indexOf("ftp://") == 0) || (myURL.indexOf("telnet:") == 0) || (myURL.indexOf("news:") == 0) || (myURL.indexOf("gopher:") == 0) || (myURL.indexOf("nntp:") == 0) || (myURL.indexOf("javascript:") == 0)) {
    tempString += myURL;
  } else {
    tempString += PBBPreHREF + myURL;
  }
return(tempString);
}

function PBBakeVoid(thisItem, thisCmd, thisText) {
  var tempString = "";
  tempString +=  '<a name="sub' + thisItem.number + '" href="javascript:parent.frames[\'code\'].myVoid();" onclick="' + thisCmd + '" onmouseover="window.status=\'' + thisText + '\';return true;" onmouseout="window.status=\'' + window.defaultStatus + '\';return true;"';
  if(thisItem.number == PBBClickedItem) {
    var tempClass;
    tempClass = thisItem.expanded ? "subexpanded" : "subclosed";
    tempString += ' class="' + tempClass + '"';
  }
  return(tempString + '>');
}

function PBBakeLink(thisItem, addName) {
  var tempString = '<a';

  if(PBBTrackedItem && PBBTrackedItem == thisItem.number) {
    tempString += ' class="tracked"'
  }
  if(addName) {
    tempString += ' name="sub' + thisItem.number + '"';
  }
  tempString += ' href="' + PBBCheckURL(thisItem.url) + '"';
  if(thisItem.target != "") {
    tempString += ' target="' + thisItem.target + '"';
  }
return tempString;
}

function PBBakeImage(thisImage) {
  return('<img src="' + PBBPreHREF + PBBenuImageDirectory + thisImage + '" align="left" border="0" vspace="0" hspace="0" width="18" height="18">');
}

function PBBakeBackImage(thisImage) {
  var tempString = 'transparent url("' + ((PBBPreHREF == "") ? "" : PBBPreHREF);
  tempString += PBBenuImageDirectory + thisImage + '")'
  return(tempString);
}

function PBBakeA(thisType, thisText, thisColor) {
  var tempString = "";
  tempString += 'a' + ((thisType == "pseudo") ? ':' : '.');
  return(tempString + thisText + '{color:' + thisColor + ';background:' + PBBakeBackground() + ';}');
}

function PBBakeBackground() {
  return((PBBBackground == "") ? PBBBGColor : 'transparent');
}

function PBBTrackTarget(thisTarget) {
  if(thisTarget.charAt(0) == "_") {
    return false;
  } else {
    for(i = 0; i < PBBFrameNames.length; i++) {
      if(thisTarget == PBBFrameNames[i]) {
        return true;
      }
    }
  }
  return false;
}

