/**************************************************************************
	Copyright (c) 2001-2003 Geir Landr� (drop@destroydrop.com)
	JavaScript Tree - www.destroydrop.com/hjavascripts/tree/
	Version 0.96	

	This script can be used freely as long as all copyright messages are
	intact.

	March 09. Modified by B Spencer to enable regenerating new trees 
	within the application
**************************************************************************/
// Arrays for nodes and icons
var nodes			= new Array();
var openNodes		= new Array();
var icons			= new Array(6);

// Loads all icons that are used in the tree
function preloadIcons() {
	icons[0] = new Image();
	icons[0].src = "images/treegen_img/plus.gif";
	icons[1] = new Image();
	icons[1].src = "images/treegen_img/plusbottom.gif";
	icons[2] = new Image();
	icons[2].src = "images/treegen_img/minus.gif";
	icons[3] = new Image();
	icons[3].src = "images/treegen_img/minusbottom.gif";
	icons[4] = new Image();
	icons[4].src = "images/treegen_img/folder.gif";
	icons[5] = new Image();
	icons[5].src = "images/treegen_img/folderopen.gif";
	icons[6] = new Image();
	icons[6].src = "images/treegen_img/edit_file.gif";
}

// Create the tree
function createTree(targetId, arrName, startNode, openNode, treeTitle, iconInd) 
{

	
	
	//treeHolder.visibility = 'hidden';
	// kill off any existing tree and create a new one
	checkRemoveObject('menuTree');
	var treeDiv = document.createElement('div');
	treeDiv.id = 'menuTree';
	treeDiv.setAttribute = ('id','menuTree');
	//treeDiv.style.display = 'none';

	
	//if no title supplied the defaultb it to Website
	if (treeTitle == ''){treeTitle='Website';}
	nodes = arrName;
	if (nodes.length > 0) 
	{
		preloadIcons();							// preloads the tree icons
		if (startNode == null) {startNode = 0;}
		if (openNode != 0 || openNode != null) 
		{	setOpenNodes(openNode);	}
	
		// if the startnode is the top of the tree then generate the title node
		if (startNode == 0) 
		{
			editable = false;
			if (iconInd)
			{	genImageNode(treeDiv, 'images/treegen_img/base.gif', '', '');		}
			
			// add the Title if you have one
			if (treeTitle != '')
			{
				// this places the title at top of tree
				var span = document.createElement('span');
				var text = document.createTextNode(treeTitle);
				span.appendChild(text);
				span.style.fontWeight = 'bold';
				//span.style.paddingLeft='20px';
				treeDiv.appendChild(span);
			}
		}
		else
		{	
			editable = false;
			// extract the nodes into an array
			var nodeValues = nodes[getArrayId(startNode)].split("|");
			alert("here "+nodeValues[2]+" >> "+nodeValues[5]);
			editable = nodeValues[5];
			
			// create a tree node
			var link = document.createElement('a');
			link.setAttribute('href', nodeValues[3]);
			var span = document.createElement('span');
			var text = document.createTextNode(nodeValues[4]);
			span.appendChild(text);
			link.appendChild(span);
			link.title = nodeValues[4];

			// insert the appropriate image
			genImageNode(link, 'images/treegen_img/folderopen.gif', '', '');
			
			// generate the node 's text
			var span = document.createElement('span');
			var text = document.createTextNode(nodeValues[2]);
			span.appendChild(text);
			link.appendChild(span);
			treeDiv.appendChild(link);
		}
		// add the line space here
		var br = document.createElement('br');
		treeDiv.appendChild(br);
		
		// now recursively process all nodes in the array
		var recursedNodes = new Array();
		addNode(startNode, recursedNodes, treeDiv);
	}
	// append the tree to the holder
	//alert('tree done')
	var treeHolder = document.getElementById(targetId);
	treeHolder.appendChild(treeDiv);
	if(treeDiv)treeDiv.style.display='block';
}


// generic fuction to add an image node to tree
function genImageNode(treeDiv, img, id, alt)
{
	// this puts an image wherever
	var image = document.createElement('img');
	image.src = img;
	image.setAttribute('align','absbottom');
	if(id != '')
	{
		image.id = id;
		image.setAttribute('id',id);
	}
	//image.setAttribute('alt',alt);
	image.title=alt;
	treeDiv.appendChild(image);
	
}

// Returns the position of a node in the array
function getArrayId(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) 
{
	for (i=0; i<nodes.length; i++) {
		var nodeValues = nodes[i].split("|");
		if (nodeValues[0] == openNode) 
		{
			openNodes.push(nodeValues[0]);
			setOpenNodes(nodeValues[1]);
		}
	} 
}
// 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;
}


// Adds a new node to the tree
function addNode(parentNode, recursedNodes, treeDiv) 
{
	for (var i = 0; i < nodes.length; i++) 
	{
		// sparate the node detail information
		var nodeValues = nodes[i].split("|");
		if (nodeValues[1] == parentNode) 
		{
			// get node pointers into variables
			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) 
				{	genImageNode(treeDiv, 'images/treegen_img/line.gif', '', '');	}
				else  
				{	genImageNode(treeDiv, 'images/treegen_img/empty.gif', '', '');	}
			}

			// put in array line & empty icons
			if (ls) recursedNodes.push(0);
			else recursedNodes.push(1);

			// Write out joinline icons
			if (hcn) // has child node
			{
				if (ls) // last sibling
				{
					var node = document.createElement('a');
					node.setAttribute('href','javascript: oc(' + nodeValues[0] + ', 1);');
					if (ino) 
					{	genImageNode(node, 'images/treegen_img/minusbottom.gif', 'join' + nodeValues[0], 'Open/Close node');	}
					else 
					{	genImageNode(node, 'images/treegen_img/plusbottom.gif', 'join' + nodeValues[0], 'Open/Close node');	}
				} 
				else 
				{
					var node = document.createElement('a');
					node.setAttribute('href','javascript: oc(' + nodeValues[0] + ', 0);');
					if (ino) 
					{	genImageNode(node, 'images/treegen_img/minus.gif', 'join' + nodeValues[0], 'Open/Close node');		}
					else 
					{	genImageNode(node, 'images/treegen_img/plus.gif', 'join' + nodeValues[0], 'Open/Close node');		}
				}
				treeDiv.appendChild(node);
			} 
			else 
			{
				if (ls) // last sibling
				{	genImageNode(treeDiv, 'images/treegen_img/joinbottom.gif', '', '');	}
				else 
				{	genImageNode(treeDiv, 'images/treegen_img/join.gif', '', '');		}
			}
			
			// create the Start link
			var link = document.createElement('a');
			link.setAttribute('href', nodeValues[3]);
												
			// Write out the folder icons
			if (hcn)	//has child node
			{
				if (ino) // is node open
				{	genImageNode(link, 'images/treegen_img/folderopen.gif', 'icon' + nodeValues[0], 'Folder');	}
				else 
				{	genImageNode(link, 'images/treegen_img/folder.gif', 'icon' + nodeValues[0], 'Folder');	}
			}
	
			//-------------- create the clickable tree level --------------//
			// if there is no subordinate tree then put the page image out
			if (!hcn)	
			{
				var sourceImg = 'images/treegen_img/page.gif';
				var ttip = 'Read Only DDrape';
				if(nodeValues[5] == true || nodeValues[5] == 'true') 
				{
					sourceImg = 'images/treegen_img/edit_file.gif'; 
					ttip = 'Editable DDrape';
				}
				genImageNode(link, sourceImg, 'icon' + nodeValues[5], ttip);
			}
			
			// Create the text element of the tree
			var classname = 'numaDrapesTips';
			//if (nodeValues[5] == 'false') {classname='unavailable';}
			
			link.className = classname;
			link.rel = nodeValues[4];
			link.setAttribute('rel',nodeValues[4]);
			link.title = nodeValues[2];

			// set up the name line on the tree
			var span = document.createElement('span');
				var bigId = nodeValues[2]+"|"+nodeValues[4];	// use pipe as separator - text could hold , and :
				span.id = 'treeLeaf:'+i;
				span.setAttribute('id', 'treeLeaf:'+i);
				span.nodeValue = nodeValues[4];
				span.className="treeNodeText";

				bindEvent(span, 'click', highlightSelection, 'false');
				//bindEvent(span, 'mouseover', showMetaData, 'false');	// show metadata
				//bindEvent(span, 'mouseout', hideMetaData, 'false');		// hide metadata
							
				// set up node name text
				var text = document.createTextNode(nodeValues[2]);
				span.appendChild(text);
			link.appendChild(span);
			treeDiv.appendChild(link);

			// add the line space here
			var br = document.createElement('br');
			treeDiv.appendChild(br);

			// If node has children write out divs and go deeper
			if (hcn) // has child node
			{
				var divNode = document.createElement('div');
				divNode.id = 'div'+nodeValues[0];
				divNode.setAttribute('id', 'div'+nodeValues[0]);
				if(!ino) {divNode.style.display = 'none';}

				// recursively step through the tree
				divNode.nodeValue = addNode(nodeValues[0], recursedNodes, divNode);
				treeDiv.appendChild(divNode);
			}
			
			// remove last line or empty icon 
			recursedNodes.pop();
		}
	}
}

// -------------------- function to bind an event to an object ------------------------
function bindEvent(object, event, fnct, status)
{
	if (status) status=false;
	if (object.addEventListener)
	{	object.addEventListener(event, fnct, status);  }// for FF 
	else 
	{	object.attachEvent("on"+event, fnct); }// for IE
}
// ------------------ function to check if object exists and if so remove it --------------------
function checkRemoveObject(oId)
{
	if (document.getElementById(oId))
	{	
		var obj = document.getElementById(oId);
		obj.parentNode.removeChild(obj);
		obj.value=null;
		return true;
	}
	return false;
}
function highlightSelection(evt)
{
	if(previousId != null){document.getElementById(previousId).className="trNormal";}
	var thisId = getThisId(evt);
	var treeLeaf = document.getElementById(thisId);
	treeLeaf.className= 'trSelected';
	previousId = thisId;
	lastSearchDDrapeId = null;
	//return previousId;
	//new Event.stop(evt);
}
function showMetaData(evt)
{
	var e_out = getThisId(evt);
	var bits = e_out.split('|');
	showData(bits[0], bits[1]);
	Event.stop(evt);
}
function hideMetaData(evt)
{
	hideData();
	Event.stop(evt);
}

// Opens or closes a node
function oc(node, bottom) {
	var theDiv = document.getElementById("div" + node);
	var theJoin	= document.getElementById("join" + node);
	var theIcon = document.getElementById("icon" + node);
	
	if (theDiv.style.display == 'none') {
		if (bottom==1) theJoin.src = icons[3].src;
		else theJoin.src = icons[2].src;
		theIcon.src = icons[5].src;
		theDiv.style.display = '';
	} else {
		if (bottom==1) theJoin.src = icons[1].src;
		else theJoin.src = icons[0].src;
		theIcon.src = icons[4].src;
		theDiv.style.display = 'none';
	}
}
// Push and pop not implemented in IE
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;
}
