/*
Treeview Javascript Web Menu (C) 2000 Chris F Wood 
-------------------------------------------------- 

This script may be used in the manner discribed on www.thetoxiczone.com,
plaese also note that this notice should not be removed from this file. 

If you wish to contact the author please e-mail chris@thetoxiczone.com 
*/

function InitTreeView() 
{
   bgColour = '#F5f5f5';
   lColour = '#0000AA';
   alColour = '#FF0000';
   vlColour = '#880088';
   tColour = '#000000';
   bgImage = '';
   TreeViewFont = 'MS Sans Serif,Arial,Helvetica'
   TreeViewFontSize = '10pt'; 
   lineWidth = 19;
   lineHeight = 16;
   imageRoot = 'images/';
   defaultRoot = 'tv-root.gif';
   defaultLink = 'tv-link.gif';
   defaultFolderOpen = 'tv-folder-open.gif';
   defaultFolderClosed = 'tv-folder-closed.gif';
   linkCont = 'tv-link-cont.gif'; 
   linkEnd = 'tv-link-end.gif'; 
   closedCont = 'tv-closed-cont.gif'; 
   closedEnd = 'tv-closed-end.gif'; 
   openCont = 'tv-open-cont.gif'; 
   openEnd = 'tv-open-end.gif'; 
   vline = 'tv-line.gif';
   blank = 'tv-blank.gif';
   oneBranch = false;
   topHTML = '';
   topHeight = 10;
   bottomHTML ='bottomframe.html';
   bottomHeight = 100;
   defaultTarget = 'mainFrame';
   iExplorer = (navigator.appName.indexOf("Internet Explorer") != -1) 
   TreeViewData = new Collection();
   return;
}

function drawTreeView() 
{
   TreeViewOutput = self.navFrame.window.document;
   TreeViewOutput.open("text/html");
   TreeViewOutput.write("<HTML><HEAD><TITLE>NavFrame</TITLE>\n");
   TreeViewOutput.write("<style type=\"text/css\">\n");
   TreeViewOutput.write("<!--\n");
   TreeViewOutput.write("p { font-family: " + TreeViewFont + ";\n");
   TreeViewOutput.write("font-size: " + TreeViewFontSize + ";\n");
   TreeViewOutput.write("color: " + tColour + ";\n");
   TreeViewOutput.write("background-color: " + bgColour + " }\n");
   TreeViewOutput.write("a { color: " + lColour + " }\n");
   TreeViewOutput.write("a:active { color: " + alColour + " }\n");
   TreeViewOutput.write("a:visited { color: " + vlColour + " }\n");
   TreeViewOutput.write("-->\n");
   TreeViewOutput.write("</style></HEAD>\n");
   TreeViewOutput.write("<BODY BGCOLOR='" + bgColour + "' BACKGROUND='" + bgImage + "'>\n");
      if (topHTML != "")
   {
      if (iExplorer)
      {
         TreeViewOutput.write("<IFRAME BORDER = \"0\" MARGINWIDTH = \"0\" MARGINHEIGHT = \"0\" HSPACE = \"0\" VSPACE = \"0\" FRAMEBORDER = \"0\" FRAMESPACING = \"0\" SCROLLING=\"NO\" WIDTH=\"100%\" HEIGHT=\"" + topHeight + "\" SRC='" + topHTML +"'>");
         TreeViewOutput.write("</IFRAME>");
      }
   }
   TreeViewOutput.write("<P>");
   if (TreeViewData.length > 0)
   { 
      TreeViewOutput.write("<IMG SRC='" + imageRoot + TreeViewData[1].iconClosed + "' WIDTH =" + TreeViewData[1].iconWidth + " HEIGHT=" + TreeViewData[1].iconHeight + " ALIGN=TEXTTOP BORDER=0>&nbsp");
      if (TreeViewData[1].url != '')
         TreeViewOutput.write("<A HREF='" + TreeViewData[1].url + "' TARGET='" + TreeViewData[1].target + "' onMouseOver=\"window.status='" + TreeViewData[1].url + "'; return true\" onMouseOut=\"window.status=''; return true\"><B>" + TreeViewData[1].name + "</B></A><BR>\n");
      else
         TreeViewOutput.write("<B>" + TreeViewData[1].name + "<B>\n"); 
      drawBranchNodes("root","");
   }
   TreeViewOutput.write("</P>");

   if (bottomHTML != "")
   {
      if (iExplorer)
      {
         TreeViewOutput.write("<IFRAME BORDER = \"0\" MARGINWIDTH = \"0\" MARGINHEIGHT = \"0\" HSPACE = \"0\" VSPACE = \"0\" FRAMEBORDER = \"0\" FRAMESPACING = \"0\" SCROLLING=\"NO\" WIDTH=\"100%\" HEIGHT=\"" + bottomHeight + "\" SRC='" + bottomHTML +"'>");
         TreeViewOutput.write("</IFRAME>");
      }
   }
   TreeViewOutput.write("</BODY>\n");
   TreeViewOutput.write("</HTML>");
   TreeViewOutput.close();

   return;
}

function drawBranchNodes(parentNode, imageString) 
{
   var childnodes = extractChildNodes(parentNode);
   var currentNode = 0;
   while (currentNode < childnodes.length) 
   {
      var newImageString = imageString;
      currentNode++;
      TreeViewOutput.write(imageString);
      if (currentNode != childnodes.length) 
         switch (childnodes[currentNode].state)
         {
          case -1: 
            TreeViewOutput.write("<IMG SRC='" + imageRoot + linkCont + "' WIDTH =" + lineWidth + " HEIGHT=" + lineHeight + " ALIGN=TEXTTOP>");
            break;
          case 0: 
            TreeViewOutput.write("<A HREF='' onClick=\"self.parent.toggle('" + childnodes[currentNode].id + "'); return false\"><IMG SRC='" + imageRoot + closedCont + "' WIDTH =" + lineWidth + " HEIGHT=" + lineHeight + " ALT='[Expand]' ALIGN=TEXTTOP BORDER=0 onMouseOver=\"window.status='[Expand]'; return true\" onMouseOut=\"window.status=''; return true\"></A>");
            break;
          default: 
             TreeViewOutput.write("<A HREF='' onClick=\"self.parent.toggle('" + childnodes[currentNode].id + "'); return false\"><IMG SRC='" + imageRoot + openCont + "' WIDTH =" + lineWidth + " HEIGHT=" + lineHeight + " ALT='[Collapse]' ALIGN=TEXTTOP BORDER=0 onMouseOver=\"window.status='[Collapse]'; return true\" onMouseOut=\"window.status=''; return true\"></A>");
             newImageString = newImageString + "<IMG SRC='" + imageRoot + vline + "' WIDTH =" + lineWidth + " HEIGHT=" + lineHeight + " ALIGN=TEXTTOP>";
         }
      else 
         switch (childnodes[currentNode].state)
         {
          case -1: 
            TreeViewOutput.write("<IMG SRC='" + imageRoot + linkEnd + "' WIDTH =" + lineWidth + " HEIGHT=" + lineHeight + " ALIGN=TEXTTOP>");
            break;
          case 0: 
            TreeViewOutput.write("<A HREF='' onClick=\"self.parent.toggle('" + childnodes[currentNode].id + "'); return false\"><IMG SRC='" + imageRoot + closedEnd + "' WIDTH =" + lineWidth + " HEIGHT=" + lineHeight + " ALT='[Expand]' ALIGN=TEXTTOP BORDER=0 onMouseOver=\"window.status='[Expand]'; return true\" onMouseOut=\"window.status=''; return true\"></A>");
            break;
          default: 
            TreeViewOutput.write("<A HREF='' onClick=\"self.parent.toggle('" + childnodes[currentNode].id + "'); return false\"><IMG SRC='" + imageRoot + openEnd + "' WIDTH =" + lineWidth + " HEIGHT=" + lineHeight + " ALT='[Collapse]' ALIGN=TEXTTOP BORDER=0 onMouseOver=\"window.status='[Collapse]'; return true\" onMouseOut=\"window.status=''; return true\"></A>");
            newImageString = newImageString + "<IMG SRC='" + imageRoot + blank + "' WIDTH =" + lineWidth + " HEIGHT=" + lineHeight + " ALIGN=TEXTTOP>";
         }
      if (childnodes[currentNode].state == 1) 
         TreeViewOutput.write("<IMG SRC='" + imageRoot + childnodes[currentNode].iconOpen + "' WIDTH =" + childnodes[currentNode].iconWidth + " HEIGHT=" + childnodes[currentNode].iconHeight + " ALIGN=TEXTTOP BORDER=0>&nbsp;");
      else
         TreeViewOutput.write("<IMG SRC='" + imageRoot + childnodes[currentNode].iconClosed + "' WIDTH =" + childnodes[currentNode].iconWidth + " HEIGHT=" + childnodes[currentNode].iconHeight + " ALIGN=TEXTTOP BORDER=0>&nbsp;"); 
      if (childnodes[currentNode].url == "")
         TreeViewOutput.write(childnodes[currentNode].name + "<BR>\n");
      else
         TreeViewOutput.write("<A HREF='" + childnodes[currentNode].url + "' TARGET='" + childnodes[currentNode].target + "' onMouseOver=\"window.status='" + childnodes[currentNode].url + "'; return true\" onMouseOut=\"window.status=''; return true\">" + childnodes[currentNode].name + "</A><BR>\n");
      if (childnodes[currentNode].state == 1) 
         drawBranchNodes(childnodes[currentNode].id, newImageString); 
   }
   return;
}

function toggle(id) 
{
   var nodeIndex = findNodeIndex(id);
   var currentState = TreeViewData[nodeIndex].state; 
   if (oneBranch)
      closeAllBranchNodes();   
   if (currentState == 1)  
      TreeViewData[nodeIndex].state = 0; 
   else
      TreeViewData[nodeIndex].state = 1; 
   if (oneBranch)
      openAllUnderNode(TreeViewData[nodeIndex].parent);   
   timeOut = setTimeout("drawTreeView()",100);
   return false;
}

function closeAllBranchNodes() 
{
   var currentNode = 0;
   while (currentNode < TreeViewData.length) 
   {
      currentNode++;
      if (TreeViewData[currentNode].state == 1)  
         TreeViewData[currentNode].state = 0; 
   }
   return;
}

function openAllUnderNode(id)
{
   var nodeIndex = findNodeIndex(id);
   if (id != 'root')
   {
      TreeViewData[nodeIndex].state = 1;
      openAllUnderNode(TreeViewData[nodeIndex].parent);
   }
   return;   
}

function findNodeIndex(id) 
{
   var currentNode = 0;
   while (currentNode < TreeViewData.length) 
   {
      currentNode++;
      if (TreeViewData[currentNode].state != -1) 
         if (TreeViewData[currentNode].id == id) 
            return currentNode;
   } 
   return -1;
}

function extractChildNodes(parentNode) 
{
   var childNodes = new Collection();
   var currentNode = 1; 
   while (currentNode < TreeViewData.length) 
   {
      currentNode++;
      if (TreeViewData[currentNode].parent == parentNode) 
         childNodes.add(TreeViewData[currentNode]);
   } 
   return childNodes;
}

function Collection() 
{
   this.length = 0; 
   this.add = add; 
   return this;
}

function add(object) 
{
  this.length++; 
  this[this.length] = object;
  return;
}

function Node(id,parent,iconClosed,iconOpen,state,name,url,target,width,height) 
{
   this.id = id;
   this.parent = parent;
   if (iconClosed != "")
      this.iconClosed = iconClosed;
   else
      if (state != -1) 
         this.iconClosed = defaultFolderClosed;
      else
         if (parent != "")
            this.iconClosed = defaultLink;
         else
            this.iconClosed = defaultRoot;
   if (iconOpen != "")
      this.iconOpen = iconOpen;
   else
      this.iconOpen = defaultFolderOpen;
   this.state = state;
   this.name = name;
   this.url = url;
   if (target != "") 
      this.target = target;
   else
      this.target = defaultTarget;
   this.iconWidth = width;
   this.iconHeight = height;
   return this;
}
