// Arrays for nodes and icons

var nodes = new Array();
var nodeObjects = new Array();
var openNodes = new Array();
var icons = new Array(14);

// Loads all icons that are used in the tree

function preloadIcons(strSkin) {
	icons[0] = new Image();
	icons[0].src = "img/" + strSkin + "/menu/mp.gif";
	icons[1] = new Image();
	icons[1].src = "img/" + strSkin + "/menu/mp.gif";
	icons[2] = new Image();
	icons[2].src = "img/" + strSkin + "/menu/mp.gif";
	icons[3] = new Image();
	icons[3].src = "img/" + strSkin + "/menu/mp.gif";
	icons[4] = new Image();
	icons[4].src = "img/" + strSkin + "/menu/mm.gif";
	icons[5] = new Image();
	icons[5].src = "img/" + strSkin + "/menu/mm.gif";
	icons[6] = new Image();
	icons[6].src = "img/" + strSkin + "/menu/mm.gif";
	icons[7] = new Image();
	icons[7].src = "img/" + strSkin + "/menu/mm.gif";
	icons[8] = new Image();
	icons[8].src = "img/" + strSkin + "/menu/trans.gif";
	icons[9] = new Image();
	icons[9].src = "img/" + strSkin + "/menu/trans.gif";
	icons[10] = new Image();
	icons[10].src = "img/" + strSkin + "/menu/trans.gif";
	icons[11] = new Image();
	icons[11].src = "img/" + strSkin + "/menu/folder.gif";
	icons[12] = new Image();
	icons[12].src = "img/" + strSkin + "/menu/folder.gif";
	icons[13] = new Image();
	icons[13].src = "img/" + strSkin + "/menu/trans.gif";
}

function NodeItem(obj, idx)
{
	var vals = obj.split('|');

	this.id = idx;
	this.catid = vals[0];
	this.parentid = vals[1];
	this.name = vals[2];
	this.path = vals[3];
	this.isOpen = false;
	this.hasChildNode = Boolean(parseInt(vals[4]));
	this.lastSibling = Boolean(parseInt(vals[5]));
	this.level = parseInt(vals[6]);
	
	this.joinString = function(cssClass)
	{
		var css = cssClass[this.level];
		var cssLength = css.length;
		var result = "";
		
		// Write out line & empty icons
		
	
		result += "\n\t<div class=\"" + cssClass[this.level] + "\">\n\t";
		
		result += "\t<table cellpadding=1 cellspacing=0 border=0 width=\"100%\">\n\t\t\t<tr>\n\t\t\t\t<td class=\"menuTd1Lvl" + this.level + "\">";	
		
		for (jdx = 2; jdx <= this.level; jdx++)
		{
			if (jdx == this.level)
			{
				result += "<img src=\"" + icons[13].src + "\" align=\"absbottom\" alt=\"\" />";
			}
			else if (jdx >= 1)
			{
				result += "<img src=\"" + icons[9].src + "\" align=\"absbottom\" alt=\"\" />";
			}
		}
			
		// Write out join icons
		if (this.hasChildNode) 
		{
			if (this.isOpen) 
			{
				if (this.lastSibling)
				{
					result += "<img id=\"join" + this.catid + "\" onclick=\"javascript:oc(" + this.catid + ", -1);\" src=\"" + icons[6].src + "\"";
				}
				else
				{
					result += "<img id=\"join" + this.catid + "\" onClick=\"javascript: oc(" + this.catid + ", -1);\" src=\"" + icons[4].src + "\"";
				}
			}
			else 
			{
				if (this.lastSibling)
				{
					result += "<img id=\"join" + this.catid + "\" onclick=\"javascript: oc(" + this.catid + ", -1);\" src=\"" + icons[2].src + "\"";
				}
				else
				{
					result += "<img id=\"join" + this.catid + "\" onclick=\"javascript: oc(" + this.catid + ", -1);\" src=\"" + icons[0].src + "\"";
				}
			}
			result += " align=\"absbottom\" style=\"cursor:pointer;\" alt=\"Open/Close node\" /></a>";
		}
		else 
		{
			if (this.lastSibling)
			{
				result += "<img src=\"" + icons[8].src + "\" align=\"absbottom\" />";
			}
			else
			{
				result += "<img src=\"" + icons[10].src + "\" align=\"absbottom\" alt=\"\" />";				
			}
		}
		
		// Start link
		// Write out folder & page icons
		result += "</td>\n\t\t\t\t<td class=\"menuTd2Lvl" + this.level + "\">";
		if (this.hasChildNode) 
		{
			if (this.path.length > 0)
			{
				result += "<img id=\"icon" + this.catid + "\" ";
				if (this.isOpen) 
				{
					result += "src=\"" + icons[12].src + "\" align=\"absbottom\" alt=\"Folder\" width=\"0\" />";
				}
				else
				{
					result += "src=\"" + icons[11].src + "\" align=\"absbottom\" alt=\"Folder\" width=\"0\" />";				
				}
				result += "</td><td width=\"100%\">";
										
				if (cssLength > 0)
				{
					result += "<a href=\"" + this.path + "\" class=\"" + css + "\" target=\"main\" onclick=\"javascript: oc(" + this.catid + ", -1);\" onmouseover=\"window.status='" + this.name + "';return true;\" onmouseout=\"window.status=' ';return true;\">";
				}
				else
				{
					result += "<a href=\"" + this.path + "\" target=\"main\" onclick=\"javascript: oc(" + this.catid + ", -1);\" onmouseover=\"window.status='" + this.name + "';return true;\" onmouseout=\"window.status=' ';return true;\">";					
				}
			}
			else
			{
				result += "<img id=\"icon" + this.catid + "\" ";
				if (this.isOpen) 
				{
					result += "src=\"" + icons[12].src + "\" align=\"absbottom\" alt=\"Folder\" width=\"0\" />";
				}
				else
				{
					result += "src=\"" + icons[11].src + "\" align=\"absbottom\" alt=\"Folder\" width=\"0\" />";				
				}
				result += "</td><td width=\"100%\">";
				
				if (cssLength > 0)
				{
					result += "<a href=\"javascript:;\" class=\"" + css + "\" target=\"main\" onclick=\"javascript: oc(" + this.catid + ", -1);\" onmouseover=\"window.status='" + this.name + "';return true;\" onmouseout=\"window.status=' ';return true;\">";
				}
				else
				{
					result += "<a href=\"javascript:;\" target=\"main\" onclick=\"javascript: oc(" + this.catid + ", -1);\" onmouseover=\"window.status='" + this.name + "';return true;\" onmouseout=\"window.status=' ';return true;\">";
				}
			}
		}
		else 
		{
			if (this.path.length > 0)
			{
				result += "<img id=\"icon" + this.catid + "\" src=\"" + icons[11].src + "\" align=\"absbottom\" width=\"0\" alt=\"" + this.catid + "\" /></td><td width=\"100%\">";
				
				if (cssLength > 0)
				{		
					result += "<a href=\"" + this.path + "\"  class=\"" + css + "\" target=\"main\">";
				}
				else
				{
					result += "<a href=\"" + this.path + "\" target=\"main\">";				
				}
			}
			else
			{
				result += "<img id=\"icon" + this.catid + "\" src=\"" + icons[11].src + "\" align=\"absbottom\" width=\"0\" alt=\"" + this.name + "\" /></td><td width=\"100%\">";
				
				if (cssLength > 0)
				{
					result += "<a href=\"javascript:;\" class=\"" + css + "\" style=\"cursor:default;\">";
				}
				else
				{
					result += "<a href=\"javascript:;\" style=\"cursor:default;\">";
				}
			}
		}
			
		// Write out node name
		result += this.name;
		// End link
		
		result += "</a>";
			
		result += "</td>\n\t\t</tr>\n\t</table>\n";
		
		result += "</div>\n";

		
		return result;
	};
}

function InitNodeObjects(arrName)
{
	for (idx=0; idx < arrName.length; idx++)
	{
		var item = new NodeItem(arrName[idx], idx);

		nodeObjects.push(item);
	}
}	

// Create the tree

function createTree(arrName, startNode, openNode, strSkin) 
{
	nodes = arrName;
	if (nodes.length > 0) 
	{
		preloadIcons(strSkin);
		if (startNode == null)
		{
			startNode = 0;
		}
		
		document.write("<div class=\"menuTree\">");	
		var recursedNodes = new Array();
		
		var currentIdx = 0;
		
		InitNodeObjects(arrName);
		
		while (currentIdx < nodeObjects.length)
		{
			currentIdx = addNode(nodeObjects[currentIdx], nodeObjects[currentIdx].level);
		}
		
		document.write("</div >");
	}
}

// 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;
}

// Adds a new node in the tree

function addNode(parentNode, currentLevel) 
{
	while (parentNode.level == currentLevel)
	{
	
		
			var currentIdx = parentNode.id;
			
			document.write(parentNode.joinString(cssClass));
			
			// If node has children write out divs and go deeper
			if (parentNode.hasChildNode) 
			{
				//document.write("<div id=\"div" + parentNode.catid + "\" class=\"GroupLvl" + parentNode.level + "\"");
				document.write("<div id=\"div" + parentNode.catid + "\"");
				if (!parentNode.isOpen)
				{
					document.write(" style=\"display: none;\"");
				}
			
				document.write(">");
				
				
				currentIdx = addNode(nodeObjects[currentIdx + 1], nodeObjects[currentIdx + 1].level);
				
				document.write("</div>");
			}
			// remove last line or empty icon
			
			parentNode = nodeObjects[currentIdx + 1];
			
			if (parentNode != null)
			{
				if (parentNode.level != currentLevel)
				{
					return currentIdx;
				}
			}
			else
			{
				return currentIdx + 1;
			}
		}
	
	return currentIdx + 1;
}


// Opens or closes a node

function oc(node, type)
{
	oc2(node, type);
}

function oc2(node, type) 
{
	var theDiv = document.getElementById("div" + node);
	var theJoin = document.getElementById("join" + node);
	var theIcon = document.getElementById("icon" + node);
	if (theDiv.style.display == 'none') 
	{
		if (type == 0)
		{
			theJoin.src = icons[8].src;
		}
		else if (type == 1)
		{
			theJoin.src = icons[9].src;
		}
		else if (type == 2)
		{
			theJoin.src = icons[6].src;
		}
		else
		{
			theJoin.src = icons[4].src;
		}
		
		theIcon.src = icons[12].src;
		theDiv.style.display = '';
	}
	else 
	{
		if (type == 0)
		{
			theJoin.src = icons[8].src;
		}
		else if (type == 1)
		{
			theJoin.src = icons[9].src;
		}
		else if (type == 2)
		{
			theJoin.src = icons[2].src;
		}
		else
		{
			theJoin.src = icons[0].src;
		}
		
		theIcon.src = icons[11].src;
		theDiv.style.display = 'none';
	}
	
	return true;
}

