var UseIcons = false;  
var rootId = 'tocroot';

// Initiailizes the document by processing the top level UL tag
function PrepareTree() {
  processUL(document.getElementById(rootId));
}//PrepareTree

// Processes all the LI tags below a UL tag to give them
// an appropriate class name / icon
function processUL(ul) {
  if (!ul) return;
  if (!ul.childNodes || ul.childNodes.length == 0) return;
  for (var itemi=0;itemi<ul.childNodes.length;itemi++) {
    var item = ul.childNodes[itemi];
    if (item.nodeName == "LI") {
      var a;
      var subul;
      subul = "";
      // Look in each LI child node for A or UL tags
      for (var sitemi=0;sitemi<item.childNodes.length;sitemi++) {
        var sitem = item.childNodes[sitemi];
        switch (sitem.nodeName) {
          case "A":
            a = sitem;
            break;
          // Found a UL - this is a book node
          case "UL":
            if (sitem.childNodes.length > 0) {
                if (sitem.childNodes[sitem.childNodes.length-1].nodeType == 1) subul = sitem;
            }//if
            break;
        }//switch
      }//for
      
      if (subul) ConfigureBook(a,ul); // This is a book node
        else ConfigureNode(a);// A page node
    }//if
  }//for
}//processUL


// Standard book node click handler. Toggles the icon / class name
function clickHandler() {
  var LI;
  var A;
  A = this;
  LI = this.parentNode;
  if (LI.className == 'tbo') {
    if (UseIcons) {
      if (LI.getAttribute("icon") <= 0) A.childNodes[0].src = "webimages/1.gif";
        else A.childNodes[0].src = "webimages/" + LI.getAttribute("icon") + ".gif";
    }//if UseIcons
    LI.className = 'tbc';
  } else {
    if (UseIcons) {
      if (LI.getAttribute("icon") <= 0) A.childNodes[0].src = "webimages/0.gif";
        else A.childNodes[0].src = "webimages/" + LI.getAttribute("icon") + ".gif";
    }//if UseIcons
    LI.className = 'tbo';
  }//else
  A.className = LI.className

  // Find the UL tag and synch the class
  for (var sitemi=0;sitemi<LI.childNodes.length;sitemi++) {
    var sitem = LI.childNodes[sitemi];
    switch (sitem.nodeName) {
      // Found a UL - this is a book node
      case "UL":
        sitem.className = LI.className
        break;
    }//switch
  }//for
}//clickHandler


  // Event handler for the first click on a book node
  // Sets up all the child items, adds the standard click handler
  //  and then re-processes the click
  function firstBookClick() {
    var LI;
    LI = this.parentNode;
    // Find the UL tag and process it
    for (var elementi=0;elementi<LI.childNodes.length;elementi++) {
      var element = LI.childNodes[elementi];
      switch (element.nodeName) {
        // Found a UL - this is a book node
        case "UL":
          processUL(element);
          // Re-assign onclick to the routine onclick handler
          this.onclick = clickHandler;
          this.onclick();
          break;
      }
    }
  }

  // Configure a page with the appropriate icon and class name
  function ConfigureNode(a) {
    // Set the className to tp so that the plus / minus symbols aren't rendered
    a.parentNode.className = "tp";
    a.className = "tp"
    if (UseIcons) {
      if (a.parentNode.getAttribute("icon") <= 0)
        a.childNodes[0].src = "webimages/9.gif";
      else
        a.childNodes[0].src = "webimages/" + a.parentNode.getAttribute("icon") + ".gif";
    }//if UseIcons
  }//ConfigureNode


  // Configure a book node with an appropriate icon and class name
  function ConfigureBook(a) {
    a.parentNode.className = "tbc"
    a.className = "tbc"
    // If the icon attribute is set to the default, set it to the default icon for a book
    if (UseIcons) {
      if (a.parentNode.getAttribute("icon") <= 0) {
        a.childNodes[0].src = "webimages/1.gif";
      } else {// Custom icon
        a.childNodes[0].src = "webimages/" + a.parentNode.getAttribute("icon") + ".gif";
      }//else
    }//if UseIcons
    a.onclick = firstBookClick;
  }//ConfigureBook


  function WriteItem(caption, url)
  {
    if (!url) url = 'javascript:void';
    document.write('<li class="tbc" icon="-1"><a href="'+url+'">');
    if (UseIcons) document.write('<img class="icon"/>');
    document.write('<span class="overicon"></span>');
    document.writeln('<span class="nodetext">'+caption+'</span></a>');
  }//WriteItem

  function WriteLevelDown()
  {
    document.writeln("<ul class='tbc'>");
  }//WriteLevelDown

    function WriteLevelUp()
  {
    document.writeln('</ul>');
  }//WriteLevelUp
  function OpenNode(nodeCaption, ul)
  {
    if (!ul) ul = document.getElementById(rootId);
    if (!ul) return;
    if (!ul.childNodes || ul.childNodes.length == 0) return;
    for (var itemi=0;itemi<ul.childNodes.length;itemi++) {
      var item = ul.childNodes[itemi];
      if (item.nodeName == "LI") {
        for (var sitemi=0;sitemi<item.childNodes.length;sitemi++) {
          var sitem = item.childNodes[sitemi];
          if (sitem.nodeName=="A") {
            theSpan = sitem.childNodes[1];
            if (theSpan&&(theSpan.innerHTML==nodeCaption)) {
              sitem.onclick();
              for (var i=0;i<item.childNodes.length;i++) {
                if (item.childNodes[i].nodeName=="UL") return item.childNodes[i];
              }//for
            }//if matches
          }//if
        }//for
      }//if
    }//for
  }//OpenNode
