//-------------------------------------------
//-------------------------------------------

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/tree_icons/plus.gif";
	icons[1] = new Image();
	icons[1].src = "images/tree_icons/plusbottom.gif";
	icons[2] = new Image();
	icons[2].src = "images/tree_icons/minus.gif";
	icons[3] = new Image();
	icons[3].src = "images/tree_icons/minusbottom.gif";
	icons[4] = new Image();
	icons[4].src = "images/tree_icons/folder.gif";
	icons[5] = new Image();
	icons[5].src = "images/tree_icons/folderopen.gif";
}
//-------------------------------------------
// Create the tree
//-------------------------------------------
function createTree(arrName, startNode, openNode) 
{
	nodes = arrName;
	if (nodes.length > 0) 
	{
		preloadIcons();
		if (startNode == null) 
			startNode = 0;
		if (openNode != 0 || openNode != null) 
		{
			setOpenNodes(openNode);
		}
		if (startNode !=0) 
		{
			var nodeValues = nodes[getArrayId(startNode)].split("|");

			var lStyle = " style='cursor:default;' ";
			if(nodeValues[3].length>0)
			{
				lStyle = " style='cursor:hand;' ";
			}
			
			var lTarget = "";
			var lHref = "";
			if(nodeValues[5].length>0)
			{
				lTarget = " target='" + nodeValues[5] + "' ";
				lHref = " href='" + nodeValues[3] + "' ";
			}
			else
			{
				lHref = " onClick=\"doNavigate('" + nodeValues[3] + "');\" ";
			}

			document.write("<a " + lTarget + lStyle + lHref + " onmouseover=\"window.status='" + nodeValues[2] + "';return true;\" onmouseout=\"window.status=' ';return true;\"><img src=\"images/tree_icons/folderopen.gif\" align=\"absmiddle\" alt=\"\" />" + nodeValues[2] + "</a><br />");
		} 
	
		var recursedNodes = new Array();
		addNode(startNode, recursedNodes);
	}
}

//-------------------------------------------
// 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) 
{
	var lStr = "";
	for (var i = 0; i < nodes.length; i++) 
	{
		var nodeValues = nodes[i].split("|");
		if (nodeValues[1] == parentNode) 
		{
			var ls	= lastSibling(nodeValues[0], nodeValues[1]);
			var hcn	= hasChildNode(nodeValues[0]);
			var lTarget = "";
			var lHref = "";
			
			if(nodeValues[5].length>0)
			{
				lTarget = " target='" + nodeValues[5] + "' ";
				lHref = " href='" + nodeValues[3] + "' ";
				lHref = lHref + " onClick=\" oc(" + nodeValues[0] + ", 0);\" ";
				
			}
			else
			{
				 lHref = " onClick=\"doNavigate('" + nodeValues[3] + "');oc(" + nodeValues[0] + ", 0);\" ";
			}




			var ino = false;				
			if(nodeValues[4]==1)
			{
				ino = true;
			}

			// Write out line & empty icons
			for (g=0; g<recursedNodes.length; g++) 
			{
				if (recursedNodes[g] == 1) 
					document.write("<img src=\"images/tree_icons/line.gif\" align=\"absmiddle\" alt=\"\" />");
				else  
					document.write("<img src=\"images/tree_icons/empty.gif\" align=\"absmiddle\" alt=\"\" />");
			}

			// put in array line & empty icons
			if (ls) recursedNodes.push(0);
			else recursedNodes.push(1);

			// Write out join icons
			if (hcn) 
			{
				if(nodeValues[1] != 0)
				{				
					if (ls) 
					{
						document.write("<a " +lTarget +" href=\"javascript: oc(" + nodeValues[0] + ", 1);\"><img id=\"join" + nodeValues[0] + "\" src=\"images/tree_icons/");
						if (ino)
						{ document.write("minus");
						}
						else
						{ document.write("plus");
						}
						document.write("bottom.gif\" align=\"absmiddle\" alt=\"Öppna/Stänga\" /></a>");
					} 
					else 
					{
						document.write("<a " +lTarget +" href=\"javascript: oc(" + nodeValues[0] + ", 0);\"><img id=\"join" + nodeValues[0] + "\" src=\"images/tree_icons/");
						if (ino)
						{ document.write("minus");
						}
						else
						{ document.write("plus");
						}
						document.write(".gif\" align=\"absmiddle\" alt=\"Öppna/Stänga\" /></a>");
					}
				}
			} 
			else 
			{
				if (ls) document.write("<img src=\"images/tree_icons/joinbottom.gif\" align=\"absmiddle\" alt=\"\" />");
				else document.write("<img src=\"images/tree_icons/join.gif\" align=\"absmiddle\" alt=\"\" />");
			}

			// Start link
			var lStyle = " style='cursor:default;' ";
			if(nodeValues[3].length>0 || hcn )
			{
				lStyle = " style='cursor:hand;' ";
			}
			lStr = "";
			
			lStr = lStr + "<a " + lTarget + lStyle + lHref + " onmouseover=\"window.status='" + nodeValues[2] + "';return true;\" onmouseout=\"window.status='';return true;\">";
			
			// Write out folder & page icons
			if (hcn) 
			{
				if( nodeValues[1] == 0)
				{
					//Projekt folder
					lStr = lStr + "<img id=\"icon" + nodeValues[0] + "\" src=\"images/tree_icons/project.gif\" align=\"absmiddle\" />";
				}
				else
				{ //Överordnade uppgift (folder)
					lStr = lStr + "<img id=\"icon" + nodeValues[0] + "\" src=\"images/tree_icons/folder.gif\" align=\"absmiddle\" />";
				}
			} 
			else 
			{
				if( nodeValues[1] == 0)
				{
					//Projekt folder
					lStr = lStr + "<img id=\"icon" + nodeValues[0] + "\" src=\"images/tree_icons/project.gif\" align=\"absmiddle\" />";
				}
				else
				{
					lStr = lStr + "<img id=\"icon" + nodeValues[0] + "\" src=\"images/tree_icons/page.gif\" align=\"absmiddle\" />";
				}
			}

			// Write out node name
			lStr = lStr + nodeValues[2] +"</a><br/>";
			document.write(lStr );
			// If node has children write out divs and go deeper
			if (hcn) 
			{
				document.write("<div id=\"div" + nodeValues[0] + "\"");
				if (!ino) 
					document.write(" style=\"display: none;\"");
				document.write(">");
				addNode(nodeValues[0], recursedNodes);
				document.write("</div>");
			}
			
			// remove last line or empty icon 
			recursedNodes.pop();
		}
	}
}

//-------------------------------------------
// 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 != null)
{	
	
	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 = '';

	//Kamil
	//	doSubmitTree(node, false);
	} 
	else 
	{
		if (bottom==1) theJoin.src = icons[1].src;
		else theJoin.src = icons[0].src;
		theIcon.src = icons[4].src;
		theDiv.style.display = 'none';

	//Kamil
//		doSubmitTree(node, true);
	}
}	

}

//-------------------------------------------
// 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;
}

//-------------------------------------------
//-------------------------------------------
//-------------------------------------------
//-------------------------------------------

