// JavaScript Document
//=================================================================
// JavaScript Tree #F4F4F
// Version 1.0
//=================================================================
var DIR_IMAGES = "../images/";
var IMG_PLUS = DIR_IMAGES + "l0900tbc.gif";
var IMG_MINUS = DIR_IMAGES + "l0900tbo.gif";
var IMG_BOTTOM = DIR_IMAGES + "l0900tbp.gif";
var IMG_VISITED = DIR_IMAGES + "l0900tbp.gif";

var imgPlus = new Image();
imgPlus.src = IMG_PLUS;
var imgMinus = new Image();
imgMinus.src = IMG_MINUS;
var imgBottom = new Image();
imgBottom.src = IMG_BOTTOM;
var imgVisited = new Image();
imgVisited.src = IMG_VISITED;

var objLocalTree = null;

var INDENT_WIDTH = 7;

//-----------------------------------------------------------------
// Class jsTree
//-----------------------------------------------------------------
// Description
//  The jsTreeNode class encapsulates the functionality of a tree.
//
// Parameters
//  (none)
//-----------------------------------------------------------------
function jsTree() {

    //Public Properties (NCZ, 1/27/02)
    this.root = null;           //the root node of the tree
 
     //Public Collections (NCZ, 1/27/02)
    this.nodes = new Array;     //array for all nodes in the tree
   
    //Constructor
    //assign to local copy of the tree (NCZ, 1/27/02)
    objLocalTree = this;
}

//-----------------------------------------------------------------
// Method jsTree.createRoot()
//-----------------------------------------------------------------
// Description
//  This method creates the root of the tree.
//
// Parameters
//  strIcon (string) - the icon to display for the root.
//  strText (string) - the text to display for the root.
//  strURL (string) - the URL to navigate to when the root is clicked.
//  strTarget (string) - the target for the URL (optional).
//
// Returns
//  The jsTreeNode that was created.
//-----------------------------------------------------------------
jsTree.prototype.createRoot = function(strIcon, strText, strURL, strTarget) {

    //create a new node (NCZ, 1/27/02)
    this.root = new jsTreeNode(strIcon, strText, strURL, strTarget);
    
    //assign an ID for internal tracking (NCZ, 1/27/02)
    this.root.id = "root";
    
    //add it into the array of all nodes (NCZ, 1/27/02)
    this.nodes["root"] = this.root;
    
    //make sure that the root is expanded (NCZ, 1/27/02)
    this.root.expanded = true;
    
    //return the created node (NCZ, 1/27/02)
    return this.root;
}

//-----------------------------------------------------------------
// Method jsTree.buildDOM()
//-----------------------------------------------------------------
// Description
//  This method creates the HTML for the tree.
//
// Parameters
//  (none)
//
// Returns
//  (nothing)
//-----------------------------------------------------------------
jsTree.prototype.buildDOM = function() {

    //call method to add root to document, which will recursively
    //add all other nodes (NCZ, 1/27/02)
    this.root.addToDOM(document.body);
}

//-----------------------------------------------------------------
// Method jsTree.toggleExpand()
//-----------------------------------------------------------------
// Description
//  This toggles the expansion of a node identified by an ID.
//
// Parameters
//  strNodeID (string) - the ID of the node that is being expanded/collapsed.
//
// Returns
//  (nothing)
//-----------------------------------------------------------------
jsTree.prototype.toggleExpand = function(strNodeID) {

    //get the node (NCZ, 1/27/02)
    var objNode = this.nodes[strNodeID];
    
    //determine whether to expand or collapse
    if (objNode.expanded)
        objNode.collapse();
    else
        objNode.expand();
		document.body.scrollTop = document.body.scrollTop+40
}

//*****************************************************************************
// Class jsTreeNode
//*****************************************************************************
// Description
//  The jsTreeNode class encapsulates the basic information for a node
//  in the tree.
//
// Parameters
//  strIcon (string) - the icon to display for this node.
//  strText (string) - the text to display for this node.
//  strURL (string) - the URL to navigate to when this node is clicked.
//  strTarget (string) - the target for the URL (optional).
//-----------------------------------------------------------------
function jsTreeNode(strIcon, strText, strURL, strTarget) {

    //Public Properties (NCZ, 1/27/02)
    this.icon = strIcon;            //the icon to display
    this.text = strText;            //the text to display
    this.url = strURL;              //the URL to link to
    this.target = strTarget;        //the target for the URL
    
    //Private Properties (NCZ, 1/27/02)
    this.indent = 0;                //the indent for the node
    
    //Public States (NCZ, 1/27/02)
    this.expanded = false;          //is this node expanded?
 
    //Public Collections (NCZ, 1/27/02)   
    this.childNodes = new Array;    //the collection of child nodes
}

//-----------------------------------------------------------------
// Method jsTreeNode.addChild()
//-----------------------------------------------------------------
// Description
//  This method adds a child node to the current node.
//
// Parameters
//  strIcon (string) - the icon to display for this node.
//  strText (string) - the text to display for this node.
//  strURL (string) - the URL to navigate to when this node is clicked.
//  strTarget (string) - the target for the URL (optional).
//
// Returns
//  The jsTreeNode that was created.
//-----------------------------------------------------------------
jsTreeNode.prototype.addChild = function (strIcon, strText, strURL, strTarget) {

    //create a new node (NCZ, 1/27/02)
    var objNode = new jsTreeNode(strIcon, strText, strURL, strTarget);
    
    //assign an ID for internal tracking (NCZ, 1/27/02)
    objNode.id = this.id + "_" + this.childNodes.length;
    
    //assign the indent for this node
    objNode.indent = this.indent + 1;
    
    //add into the array of child nodes (NCZ, 1/27/02)
    this.childNodes[this.childNodes.length] = objNode;
    
    //add it into the array of all nodes (NCZ, 1/27/02)
    objLocalTree.nodes[objNode.id] = objNode;
    
    //return the created node (NCZ, 1/27/02)
    return objNode;
}

//-----------------------------------------------------------------
// Method jsTreeNode.addToDOM()
//-----------------------------------------------------------------
// Description
//  This method adds DOM elements to a parent DOM element.
//
// Parameters
//  objDOMParent (HTMLElement) - the parent DOM element to add to.
//
// Returns
//  (nothing)
//-----------------------------------------------------------------
jsTreeNode.prototype.addToDOM = function (objDOMParent) {

    //create the URL (NCZ, 1/27/02)
    var strHTMLLink = "<a href=\"" + this.url + "\"";
    if (this.target)strHTMLLink += " target=\"" + this.target + "\">";
    
    //create the layer for the node (NCZ, 1/27/02)
    var objNodeDiv = document.createElement("div");
    
    //add it to the DOM parent element (NCZ, 1/27/02)
    objDOMParent.appendChild(objNodeDiv);
    
    //create string buffer (NCZ, 1/27/02)
    var d = new jsDocument;
    
    //begin the table (NCZ, 1/27/02)
    d.writeln("<table border=\"0\" cellpadding=\"0\" width=200% cellspacing=\"0\"><tr>");
    
    //no indent needed for root or level under root (NCZ, 1/27/02)
    if (this.indent > 1) {
        d.write("<td width=\"");
        d.write(this.indent * INDENT_WIDTH);
        d.write("\"> </td>");
    }
    
    //there is no plus/minus image for the root (NCZ, 1/27/02)
    if (this.indent > 0) {
    
        d.write("<td width=\"18\" align=\"center\">");
        
        //if there are children, then add a plus/minus image (NCZ, 1/27/02)
        if (this.childNodes.length > 0) {
            d.write("<a href=\"javascript:objLocalTree.toggleExpand('");
            d.write(this.id);
            d.write("')\"><img src=\"");
            d.write(this.expanded ? imgMinus.src : imgPlus.src);
            d.write("\" border=\"0\" hspace=\"1\" id=\"");
            d.write("imgPM_" + this.id);
            d.write("\" /></a>");
//			Added by Bibs
			strHTMLLink = "<a href=\"javascript:objLocalTree.toggleExpand('" + this.id + "')\">"
        }
//			Added by Bibs
        if (this.childNodes.length == 0) {
			d.write(strHTMLLink);
            d.write("<img src=\"");
            d.write(this.expanded ? imgVisited.src : imgBottom.src);
            d.write("\" border=\"0\" hspace=\"1\" id=\"");
            d.write("imgPM_" + this.id);
            d.write("\" /></a>");
        }
        d.write("</td>");
    }
 
    //finish by drawing the icon and text (NCZ, 1/27/02)
//    d.write("<td width=\"22\">" +strHTMLLink + "<img hspace=\"1\" src=\"" + this.icon + "\" border=\"0\" align=\"absmiddle\" /></a></td>");
    d.write("<td nowrap=\"nowrap\">" + strHTMLLink + this.text + "</a></td>");
    d.writeln("</tr></table>");
        
    //assign the HTML to the layer (NCZ, 1/27/02)
    objNodeDiv.innerHTML = d;

    //create the layer for the children (NCZ, 1/27/02)
    var objChildNodesLayer = document.createElement("div");
    objChildNodesLayer.setAttribute("id", "divChildren_" + this.id);
    objChildNodesLayer.style.position = "relative";
    objChildNodesLayer.style.display = (this.expanded ? "block" : "none");
    objNodeDiv.appendChild(objChildNodesLayer);
    
    //call for all children (NCZ, 1/27/02)
    for (var i=0; i < this.childNodes.length; i++)
        this.childNodes[i].addToDOM(objChildNodesLayer);
}

//-----------------------------------------------------------------
// Method jsTreeNode.collapse()
//-----------------------------------------------------------------
// Description
//  This method expands the jsTreeNode's children to be hidden.
//
// Parameters
//  (none)
//
// Returns
//  (nothing)
//-----------------------------------------------------------------
jsTreeNode.prototype.collapse = function () {

    //check to see if the node is already collapsed (NCZ, 1/27/02)
    if (!this.expanded) {
    
        //throw an error (NCZ, 1/27/02)
        throw "Node is already collapsed"

    } else {
    
        //change the state of the node (NCZ, 1/27/02)
        this.expanded = false;
        
        //change the plus/minus image to be plus (NCZ, 1/27/02)
        document.images["imgPM_" + this.id].src = imgPlus.src;
        
        //hide the child nodes (NCZ, 1/27/02)
        document.getElementById("divChildren_" + this.id).style.display = "none";
    }
}


//-----------------------------------------------------------------
// Method jsTreeNode.expand()
//-----------------------------------------------------------------
// Description
//  This method expands the jsTreeNode's children to be displayed.
//
// Parameters
//  (none)
//
// Returns
//  (nothing)
//-----------------------------------------------------------------
jsTreeNode.prototype.expand = function () {

    //check to see if the node is already expanded (NCZ, 1/27/02)
    if (this.expanded) {
    
        //throw an error (NCZ, 1/27/02)
        throw "Node is already expanded"
    
    } else {
    
        //change the state of the node (NCZ, 1/27/02)
        this.expanded = true;
        
        //change the plus/minus image to be minus (NCZ, 1/27/02)
        document.images["imgPM_" + this.id].src = imgMinus.src;
        
        //show the child nodes (NCZ, 1/27/02)
        document.getElementById("divChildren_" + this.id).style.display = "block";
    }
}
