/**************************************************************************
	Copyright (c) 2001 Geir Landrö (drop@destroydrop.com)
	JavaScript Tree - www.destroydrop.com/hugi/javascript/tree/
	Version 0.96	

	This script can be used freely as long as all copyright messages are
	intact.
**************************************************************************/

function debug(message) {
    doAlert = 0;

    if( doAlert ) {
        alert(message);
    }
}

function displayNode(prefix, nodeId)
{
    var nodeValues = nodes[getArrayIndex(nodeId)];
    debug(prefix + " " + nodeValues);

}

// Arrays for nodes and icons
var nodes		= new Array();;
var openNodes	= new Array();
var icons		= new Array(6);
var containerSkin = "Arizona";


// Loads all icons that are used in the tree
function preloadIcons() {
	icons[0] = new Image();
	icons[0].src = "skins/" + containerSkin + "/images/CONT/MenuTable/javascript/plus.gif";
	icons[1] = new Image();
	icons[1].src = "skins/" + containerSkin + "/images/CONT/MenuTable/javascript/plus.gif"; //bottom
	icons[2] = new Image();
	icons[2].src = "skins/" + containerSkin + "/images/CONT/MenuTable/javascript/minus.gif";
	icons[3] = new Image();
	icons[3].src = "skins/" + containerSkin + "/images/CONT/MenuTable/javascript/minus.gif"; //bottom
	icons[4] = new Image();
	//icons[4].src = "skins/" + containerSkin + "/images/CONT/MenuTable/javascript/folder.gif";
	icons[5] = new Image();
	//icons[5].src = "skins/" + containerSkin + "/images/CONT/MenuTable/javascript/folderopen.gif";
	icons[6] = new Image();
	icons[6].src = "skins/" + containerSkin + "/images/CONT/MenuTable/javascript/child.gif";
}

//var DEFAULT_START_NODE = 0;
var DEFAULT_START_NODE = 0;

// Create the tree
function createTree(arrName, startNode, openNode, skinName) {
	nodes = arrName;
    containerSkin = skinName;
	if (nodes.length > 0) {
		preloadIcons();
		if (startNode == null) startNode = DEFAULT_START_NODE;

		if ((openNode != 0) || (openNode != null)){
            var lobbyNode = 1;
            setOpenNodes(openNode, lobbyNode);
        }

        var tmp = "";
        for (i=0; i<openNodes.length; i++) {
            tmp += openNodes[i] + ",";
        }

		if (startNode !=0) {
			//document.write("<a href=\"" + nodeValues[3] + "\" onmouseover=\"window.status='" + nodeValues[2] + "';return true;\" onmouseout=\"window.status=' ';return true;\"><img src=\"images/javascript/folderopen.gif\" align=\"absbottom\" alt=\"\" />" + nodeValues[2] + "</a><br />");
		} //else document.write("<img src=\"images/javascript/base.gif\" align=\"absbottom\" alt=\"\" /><a href=\"http://stacyk.dra.com:8088/rooms/\">Lobby</a><br />");
	
		var recursedNodes = new Array();

		addNode(startNode, recursedNodes,0);
	}

}
// Returns the position of a node in the array
function getArrayIndex(node) {
	for (i=0; i<nodes.length; i++) {
		var nodeValues = nodes[i].split("|");
		if (nodeValues[0]==node) return i;
	}
}
// Puts in array nodes that will be open
function setOpenNodes(openNode, rootNode) {
	for (i=0; i<nodes.length; i++) {
		var nodeValues = nodes[i].split("|");

		if (nodeValues[0]==openNode) {
            // Uncomment "if" if we don't want the root node to open
//            if(nodeValues[0] != rootNode) {
                openNodes.push(nodeValues[0]);
                setOpenNodes(nodeValues[1], rootNode);
//            }
		}
	} 
}
// Checks if a node is open
function isNodeOpen(node) {
	for (i=0; i<openNodes.length; i++) {
		if (openNodes[i]==node) return true;
    }
	return false;
}
// Checks if a node has any children
function hasChildNode(parentNode) {
	for (i=0; i< nodes.length; i++) {
		var nodeValues = nodes[i].split("|");
		if (nodeValues[1] == parentNode) return true;
	}
	return false;
}
// Checks if a node is the last sibling
function lastSibling (node, parentNode) {
	var lastChild = 0;
	for (i=0; i< nodes.length; i++) {
		var nodeValues = nodes[i].split("|");
		if (nodeValues[1] == parentNode)
			lastChild = nodeValues[0];
	}
	if (lastChild==node) return true;
	return false;
}

// Checks if a nodeId is the last sibling

function lastSiblingById(nodeId, parentNodeId) {
	var lastChildNodeId = 0;

	for (i=0; i< nodes.length; i++) {
		var nodeValues = nodes[i].split("|");

		if (nodeValues[1] == parentNodeId)
        	{
			lastChildNodeId = nodeValues[0];
        	}
	}

    debug("node Id = " + nodeId + " ; parentNodeId = " + parentNodeId + " ; lastC = " + lastChildNodeId);

	if (lastChildNodeId == nodeId) return true;
	return false;
}

// returns siblings of a node 

function getSiblings(nodeId, parentId)
{
    var siblings = new Array();

	for (var i = 0; i < nodes.length; i++) {
        var currentNode = nodes[i];
		var currentNodeValues = currentNode.split("|");
        var currentParentId = currentNodeValues[1];
        var currentNodeId = currentNodeValues[0];
        
        if( currentParentId == parentId ) {
            if( currentNodeId == nodeId ) {
                debug("getSiblings. same. skipping " + currentNodeValues);
            } else {
                debug("getSiblings. pushing: " + currentNodeValues);
                siblings.push(currentNode);
            }
        }
    }

    return siblings;
}

// Adds a new node in the tree
function addNode(parentNode, recursedNodes, level) {
    if( (level == null) || (level < 0) ) { level = 0; }

    
	for (var i = 0; i < nodes.length; i++) {
		var nodeValues = nodes[i].split("|");

        // Additional commands to handle apostrophes in room names
        if (nodeValues[2].indexOf("'") != -1) {
            var statbar = nodeValues[2].replace(/'/, "\\'");  //"
        } else {
            var statbar = nodeValues[2];
        }
        // end of additional commands to handle apostrophes
        // Kevin H. (Sirsi Corporation)
        
        var hasInstance = (nodeValues[4] == "true");
        
		if (nodeValues[1] == parentNode) {
            
			var ls	= lastSibling(nodeValues[0], nodeValues[1]);
			var hcn	= hasChildNode(nodeValues[0]);
			var ino = isNodeOpen(nodeValues[0]);

			// Write out line & empty icons
			for (g=0; g<recursedNodes.length; g++) {
				//if (recursedNodes[g] == 1) document.write("<img src=\"images/javascript/line.gif\" align=\"absbottom\" alt=\"\" />");
				//else  document.write("<img src=\"images/javascript/empty.gif\" align=\"absbottom\" alt=\"\" />");
				//document.write("<span style=\"color:#669999\">");
			}
			
			// put in array line & empty icons
			if (ls) recursedNodes.push(0);
			else recursedNodes.push(1);

            if(currentOpenNode == nodeValues[0]) {
                var cssClass = "" + containerSkin + "-Menu-ActiveNode";
            } else {
                var cssClass = "" + containerSkin + "-Menu-InactiveNode";
            }

            document.write("<div class=\"" + cssClass + "\">");

            
			// Write out join icons

			if (hcn) {
				if (ls) {
                    if (ino) {
                        var imgSrc = icons[3].src;
                    } else {
                        var imgSrc = icons[1].src;
                    }
					document.write("<a href=\"javascript: oc(" + nodeValues[0] + ", 1);\">" + 
                                   "<img id=\"join" + nodeValues[0] + "\" src=\"");
                    document.write(imgSrc);
					document.write("\" border=\"0\" align=\"absbottom\" alt=\"" + ROOMS_JS_OPEN_CLOSE_NODE + "\" /></a>");
				} else {
                    if (ino) {
                        var imgSrc = icons[2].src;
                    } else {
                        var imgSrc = icons[0].src;
                    }
					document.write("<a href=\"javascript: oc(" + nodeValues[0] + ", 0);\">" + 
                                   "<img id=\"join" + nodeValues[0] + "\" src=\"");
                    document.write(imgSrc);
					document.write("\" border=\"0\" align=\"absbottom\" alt=\"" + ROOMS_JS_OPEN_CLOSE_NODE + "\" /></a>");
				}


			} else {
                document.write("<img src=\"" + icons[6].src + "\" align=\"absbottom\" alt=\"" + nodeValues[2] + "\" />");
			}

			// Start link
			if(hcn) {
                if(!hasInstance) {
                    document.write(nodeValues[2]);
                } else {
                    document.write(
                        "<a href=\"" + 
                        nodeValues[3] + 
                        "\" onmouseover=\"window.status='" + 
                        statbar + 
                        "';return true;\" onmouseout=\"window.status=' ';return true;\"" + 
                        "class=\"" + cssClass + "\"" +
                        ">" + 
                        //nodeValues[0] +  "-" +
                        nodeValues[2] + 
                        "</a>"
                        );
                }
			} else {
                if(!hasInstance) {
                    document.write(nodeValues[2]);
                } else {
                    document.write(
                                   "<a href=\"" + 
                                   nodeValues[3] + 
                                   "\" onmouseover=\"window.status='" + 
                                   statbar +
                                   "';return true;\" onmouseout=\"window.status=' ';return true;\"" + 
                                   "class=\"" + cssClass + "\"" +
                                   ">" +
                                   //nodeValues[0] +  "-" +
                                   nodeValues[2] + 
                                   "</a>"
                                   );
                }
            }
            document.write("</div>");
            document.write("<div class=\"" + containerSkin + "-Menu-ChildNode\">");
            
			// If node has children write out divs and go deeper
			if (hcn) {
//            document.write("---");                

				document.write("<div id=\"div" + nodeValues[0] + "\"");
                if (!ino) document.write(" style=\"display: none;\"");
				document.write(">");                

                var childLevel = level + 1;
				addNode(nodeValues[0], recursedNodes, childLevel);

				document.write("</div>");
			} else {
                //Display nothing
			}
            document.write("</div>");
        
            
			
			// remove last line or empty icon 
			recursedNodes.pop();
		}
   }
}

function closeSiblings(nodeId) {
    //debug("close Siblings. nodeId = " + nodeId);
    var nodeIndex = getArrayIndex(nodeId);
    //debug("close Siblings. nodeIndex = " + nodeIndex);
	var nodeValues = nodes[nodeIndex].split("|");
    debug("close Siblings. nodeValues = " + nodeValues);
    var parentId = nodeValues[1];
    debug("parent Id = " + parentId);

    var siblings = getSiblings(nodeId, parentId);
    debug("closeSiblings. siblings.length = " + siblings.length);

	for (var i = 0; i < siblings.length; i++) {
        debug("closeSiblings. i = " + i);
        var siblingNode = siblings[i];
        var siblingValues = siblingNode.split("|");
        var siblingId = siblingValues[0];

        debug("closeSiblings. sibling = " + siblingValues);
        closeSingleNode(siblingId);
    }
}

function closeSingleNode(nodeId)
{

	var theDiv = new getObj("div" + nodeId).obj;
    
    var nodeIndex = getArrayIndex(nodeId);
	var nodeValues = nodes[nodeIndex].split("|");
    var parentId = nodeValues[1];

    bottom = lastSiblingById(nodeId, parentId);

    debug("closeSingleNode. closing: " + nodeValues + " ; bottom = " + bottom);

    // check to ensure that node is closed

	if ((theDiv != null) && (theDiv.style.display != 'none') ) {
	    theDiv.style.display = 'none';
    }


    var joinId = "join" + nodeId;
	var theJoin	= new getObj(joinId).obj;
    if(theJoin != null) {
        var bottom	= lastSibling(nodeValues[0], nodeValues[1]);    
        if (bottom==1) {
            setImage(joinId, icons[1]);
        } else {
            setImage(joinId, icons[0]);
        }
    }    
}


    
// Opens or closes a node
function oc(nodeId, bottom) {
	var theDiv = new getObj("div" + nodeId).obj;

    var joinId = "join" + nodeId;
	var theJoin	= new getObj(joinId).obj;    
    //var theIcon = new getObj("icon" + nodeId).obj;
    //var theContent = new getObj(containerSkin + "-contentDiv");
    
    displayNode("oc", nodeId);

	if (theDiv != null)
	{
        if (theDiv.style.display == 'none') {
            if (bottom==1) {
                setImage(joinId, icons[3]);
            } else {
                setImage(joinId, icons[2]);
            }
            //if(theIcon != null) {
            //  theIcon.src = icons[5].src;
            //}
            theDiv.style.display = '';
            // close the others
            //closeSiblings(nodeId);

            //hide content to prevent Form Elements from Overlapping the Menu Layer 
            if(nodeId == 1) {
                //hideObject(theContent);
            }
        } else {
            if (bottom==1) {
                setImage(joinId, icons[1]);
            } else {
                setImage(joinId, icons[0]);
            }
            //if(theIcon != null) {
            //    theIcon.src = icons[4].src;
            //}
            theDiv.style.display = 'none';

            //hide content to prevent Form Elements from Overlapping the Menu Layer 
            if(nodeId == 1) {
                //showObject(theContent);
            }            
        }        
	}
}

// Push and pop not implemented in IE (don´t know about NS though)
if(!Array.prototype.push) {
	function array_push() {
		for(var i=0;i<arguments.length;i++)
			this[this.length]=arguments[i];
		return this.length;
	}
	Array.prototype.push = array_push;
}
if(!Array.prototype.pop) {
	function array_pop(){
		lastElement = this[this.length-1];
		this.length = Math.max(this.length-1,0);
		return lastElement;
	}
	Array.prototype.pop = array_pop;
}

