/**********************************************************************
* Configuration variables.
************************************************************************/

// Should the rootNode be displayed.
var showRootNode = true;

// Should the dashed lines between nodes be shown.
var showLines = true;

// RootNode of the tree.
var rootNode;

// Container to display the Tree in.
var container;

// Shows/Hides subnodes on startup
var showAllNodesOnStartup = false;

/************************************************************************
* The following is just instancevariables.
************************************************************************/
var href = '';

/************************************************************************
* Function to recursive get list items
************************************************************************/

i=1;
function getChilds(el)
{
	var childs = new Array();
  for(var o=0;o<el.getElementsByTagName("li").length;o++){
  	//foreach element
  	var node = null;
  	// Check if has Childs

  	if(el.getElementsByTagName("li")[o].getElementsByTagName("ul").length>0)
  	{
  		// recursive call
  		recurChilds = getChilds(el.getElementsByTagName("li")[o].getElementsByTagName("ul")[0]);
			el.getElementsByTagName("li")[o].removeChild(el.getElementsByTagName("li")[o].getElementsByTagName("ul")[0]);
			i++;
			node = new TreeNode(i,el.getElementsByTagName("li")[o].innerHTML);
			for(num=0; num<recurChilds.length;num++)
  		{
  			node.addChild(recurChilds[num]);
  		}
  		childs.push(node);
  	}
  	else
  		{
  			// has no childs
				i++;
				childs.push(new TreeNode(i,el.getElementsByTagName("li")[o].innerHTML));
  		}
  	}
  return childs;
}

/************************************************************************
* Function init
************************************************************************/

function init(root,containerul,imgdir) {
	rootNode = getChilds(document.getElementById(containerul));
	// remove all remaining elements
	var d = document.getElementById(root);
  var olddiv = document.getElementById(containerul);
  d.removeChild(olddiv);
	rootNode = rootNode[0];
	container = document.getElementById(root);
	showTree(imgdir);
}

/**
* The TreeNode Object
* @param id unique id of this treenode
* @param name The title of this node
*/
function TreeNode(id,name,param) {
	this.id = id;
	this.childs = new Array();
	this.name = (name == null ? 'unset name' : name);
	this.parent = null;
	this.handler = null;
	
	this.openeventlisteners = new Array();
	this.haschilds = false;
	this.linestring = '';
	
	this.nextSibling = null;
	this.prevSibling = null;
	

	this.getID = function() {
		return this.id;
	}
	this.setName = function(newname) {
		this.name = newname;
	}
	this.getName = function() {
		return this.name;
	}
	this.addOpenEventListener = function(event) {
		this.openeventlisteners[this.openeventlisteners.length] = event;
	}
	this.gotOpenEventListeners = function() {
		return (this.openeventlisteners.length > 0);
	}
	
	this.addChild = function(childNode) {
		var possiblePrevNode = this.childs[this.childs.length - 1]
		if (possiblePrevNode) {
			possiblePrevNode.nextSibling = childNode;
			childNode.prevSibling = possiblePrevNode;
			// alert(childNode.prevSibling);
		}

		this.childs[this.childs.length] = childNode;
		childNode.setParent(this);

	}
	this.removeChild = function(childNode) {
		var found = false;
		for (var i=0;i<this.childs.length;i++) {
			if (found) {
				this.childs[i] = this.childs[i + 1];
			}
			if (this.childs[i] == childNode) {
				if (i == (this.childs.length - 1)) {
					this.childs[i] = null;
				}
				else {
					this.childs[i] = this.childs[i + 1];
				}
				found = true;
			}
		}
		if (found) {
			this.childs.length = this.childs.length-1;
		}
	}
	this.resetChilds = function() {
		this.childs = new Array();
	}
	this.setHasChilds = function(hasChilds) {
		this.haschilds = hasChilds;
	}
	this.hasChilds = function() {
		if (this.haschilds == true) {
			return true;
		}
		return (this.childs.length > 0);
	}
	this.getChildCount = function() {
		return this.childs.length;
	}
	this.getFirstChild = function() {
		if (this.hasChilds()) {
			return this.childs[0];
		}
		return null;
	}
	this.gotHandler = function() {
		return this.handler != null;
	}
	this.setHandler = function(handler) {
		this.handler = handler;
	}
	this.getHandler = function() {
		return this.handler;
	}
	this.setParent = function(parent) {
		this.parent = parent;
	}
	this.getParent = function() {
		return this.parent;
	}
	this.getLineString = function() {
		return this.linestring;
	}
	this.setLineString = function(string) {
		this.linestring = string;
	}
	
}
function getTreeNode(nodeID) {
	return findNodeWithID(rootNode,nodeID);
}
function findNodeWithID(node,nodeID) {
	if (node.getID() == nodeID) {
		return node;
	}
	else {
		if (node.hasChilds()) {
			for(var i=0;i<node.getChildCount();i++) {
				var value = findNodeWithID(node.childs[i],nodeID);
				if (value != false) {
					return value;
				}
			}
		}
		return false;
	}
}

function showTree(imagesrc) {
	href = imagesrc;
	window.focus();
	var str = '';
	str = '<div id="node' + rootNode.getID() + '" class="sitemapmain" style="display:' + (showRootNode == true ? 'block' : 'none') + ';">';
	str += '<nobr><div class="imgitem"></div>';
	str += '' + rootNode.getName() + '';
	str += '</nobr></div>';
	
	if (rootNode.hasChilds()) {
		for(i=0;i<rootNode.childs.length;i++) {
			nodeContents = showNode(rootNode.childs[i],(i == (rootNode.getChildCount() -1)));
			str = str + nodeContents;
		}
	}
	container.innerHTML = str;
	if (window.finishedLoading) {
		finishedLoading();
	}
}
/**
* Shows the given node, and subnodes.
*/
function showNode(treeNode,lastNode) {
	linestring = treeNode.getLineString();
	var str;
	str = '<div class="sitemapmain" id="node' + treeNode.getID() + '">';
	str += '<nobr><div class="imgitem">';
	for(var y=0;y<linestring.length;y++) {
		if (linestring.charAt(y) == 'I') {
			str += '<img src="' + href + '/' + (showLines ? 'line' : 'white') + '.gif" style="width:19px;height:20px;vertical-align:middle;">';
		}
		else if (linestring.charAt(y) == 'B') {
			str += '<img src="' + href + '/white.gif" style="width:19px;height:20px;vertical-align:middle;">';
		}
	}
	if (treeNode.hasChilds()) {
		// If this is the first child of the rootNode, and showRootNode is false, we want to display a different icon.
		if (!showRootNode && (treeNode.getParent() == rootNode) && (treeNode.getParent().getFirstChild() == treeNode)) {
			if (!lastNode) {
				str += '<img id="handler' + treeNode.getID() + '" src="' + href + '/' + (showLines ? 'plus_no_root' : 'plus_nolines') + '.gif" style="width:19px;height:20px;vertical-align:middle;" OnClick="handleNode(' + treeNode.getID() + ');">';
			}
			else {
				str += '<img id="handler' + treeNode.getID() + '" src="' + href + '/' + 'plus_last' + '_no_root.gif" style="width:19px;height:20px;vertical-align:middle;" OnClick="handleNode(' + treeNode.getID() + ');">';
			}
		}
		else {
			if (!lastNode) {
				str += '<img id="handler' + treeNode.getID() + '" src="' + href + '/' + (showLines ? 'plus' : 'plus_nolines') + '.gif" style="width:19px;height:20px;vertical-align:middle;" OnClick="handleNode(' + treeNode.getID() + ');">';
			}
			else {
				str += '<img id="handler' + treeNode.getID() + '" src="' + href + '/' + (showLines ? 'plus_last' : 'plus_nolines') + '.gif" style="width:19px;height:20px;vertical-align:middle;" OnClick="handleNode(' + treeNode.getID() + ');">';
			}
		}
	}
	else {
		// If this is the first child of the rootNode, and showRootNode is false, we want to display a different icon.
		if (!showRootNode && (treeNode.getParent() == rootNode) && (treeNode.getParent().getFirstChild() == treeNode)) {
			if (!lastNode) {
				str += '<img id="handler' + treeNode.getID() + '" src="' + href + '/' + (showLines ? 't_no_root' : 'white') + '.gif" style="width:19px;height:20px;vertical-align:middle;">';
			}
			else {
				str += '<img id="handler' + treeNode.getID() + '" src="' + href + '/white.gif" style="width:19px;height:20px;vertical-align:middle;">';
			}
		}
		else {
			if (!lastNode) {
				str += '<img id="handler' + treeNode.getID() + '" src="' + href + '/' + (showLines ? 't' : 'white') + '.gif" style="width:19px;height:20px;vertical-align:middle;">';
			}
			else {
				str += '<img id="handler' + treeNode.getID() + '" src="' + href + '/' + (showLines ? 'lastnode' : 'white') + '.gif" style="width:19px;height:20px;vertical-align:middle;">';
			}
		}
	}
		
	str += '</div>';
	str += treeNode.getName();
	//str += '</span>';
	str += '</nobr>';
	str += '</div>';

	if (treeNode.hasChilds()) {
		
			str += '<div id="node' + treeNode.getID() + 'sub" style="display:' + (showAllNodesOnStartup == true ? 'block;' : 'none;') + ';">';
		var subgroupstr = '';
		var newChar = '';
		if (!lastNode) {
			newChar = 'I';
		}
		else {
			newChar = 'B';
		}
		for(var z=0;z<treeNode.getChildCount();z++) {
			treeNode.childs[z].setLineString(linestring + newChar);
		}

		for(var z=0;z<treeNode.getChildCount();z++) {
			subgroupstr += showNode(treeNode.childs[z],(z == (treeNode.getChildCount() -1)));
		}
		str += subgroupstr;
		str += '</div>';
	}
	else {
		str += '<div id="node' + treeNode.getID() + 'sub" style="display:none;">';
		str += '</div>';
	}
	return str;
}
function handleNode(nodeID) {
	var treeNode = getTreeNode(nodeID);	
	if (!treeNode.hasChilds()) { // No reason to handle a node without childs.
		return;
	}
	
	var submenu = document.getElementById('node' + nodeID + 'sub');
	
	
	var actionimage = document.getElementById('handler' + nodeID);
	// This will be used if showRootNode is set to false.
	var firstChildOfRoot = false;
	if (actionimage.src.indexOf('_no_root') != -1) {
		firstChildOfRoot = true;
	}
	
	if (submenu.style.display == 'none') {
		fireOpenEvent(treeNode);
		submenu.style.display = 'block';

		if (actionimage.src.indexOf('last') == -1) {
			actionimage.src = href + '/' + ((firstChildOfRoot) ? 'minus_no_root' : (showLines ? 'minus' : 'minus_nolines')) + '.gif';
		}
		else {
			actionimage.src = href + '/' + ((firstChildOfRoot) ? 'minus_last_no_root' : (showLines ? 'minus_last' : 'minus_nolines')) + '.gif';
		}
	}
	else {
		submenu.style.display = 'none';
		
		if (actionimage.src.indexOf('last') == -1) {
			actionimage.src = href + '/' + ((firstChildOfRoot) ? 'plus_no_root' : (showLines ? 'plus' : 'plus_nolines')) + '.gif';
		}
		else {
			actionimage.src = href + '/' + ((firstChildOfRoot) ? 'plus_last_no_root' : (showLines ? 'plus_last' : 'plus_nolines')) + '.gif';
		}
	}
}
function fireOpenEvent(treeNode) {
	if (treeNode.gotOpenEventListeners()) {
		for(var i=0;i<treeNode.openeventlisteners.length;i++) {
			eval(treeNode.openeventlisteners[i] + '(' + treeNode.getID() + ');');
		}
	}
}