/**************************************************************************
	Fonction arborescence par
        Copyright (c) 2001 Geir Landrö
  Modifiée par Tonglet Olivier
**************************************************************************/
var nodes		 = new Array();
var openNodes= new Array();
var icons		 = new Array(6);
function preloadIcons() 
{
	icons[0] = new Image();
	icons[0].src = "images/plusbottom.gif";
	icons[1] = new Image();
	icons[1].src = "images/plusbottom.gif";
	icons[2] = new Image();
	icons[2].src = "images/minusbottom.gif";
	icons[3] = new Image();
	icons[3].src = "images/minusbottom.gif";
	icons[4] = new Image();
	icons[4].src = "images/repertoireferme.gif";
	icons[5] = new Image();
	icons[5].src = "images/repertoireouvert.gif";
}
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("|");
			document.write("<A href=\"" + nodeValues[3] + "\" onmouseover=\"window.status='" + nodeValues[2] + "';return true;\" onmouseout=\"window.status=' ';return true;\"><IMG src=\"images/repertoireouvert.gif\" align=\"absbottom\" />" + nodeValues[2] + "</A><BR />");
		} else document.write("<BR />");
	
		var recursedNodes = new Array();
		addNode(startNode, recursedNodes);
	}
}
function getArrayId(node) {
	for (i=0; i<nodes.length; i++) {
		var nodeValues = nodes[i].split("|");
		if (nodeValues[0]==node) return i;
	}
}
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]);
		}
	} 
}
function isNodeOpen(node) {
	for (i=0; i<openNodes.length; i++)
		if (openNodes[i]==node) return true;
	return false;
}
function hasChildNode(parentNode) {
	for (i=0; i< nodes.length; i++) {
		var nodeValues = nodes[i].split("|");
		if (nodeValues[1] == parentNode) return true;
	}
	return false;
}
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;
}
function addNode(parentNode, recursedNodes) {
	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 ino = isNodeOpen(nodeValues[0]);
			for (g=0; g<recursedNodes.length; g++) {
				if (recursedNodes[g] == 1) document.write("<img src=\"images/line.gif\" align=\"absbottom\" />");
				else  document.write("<IMG src=\"images/empty.gif\" align=\"absbottom\" />");
			}
			if (ls) recursedNodes.push(0);
			else recursedNodes.push(1);
			if (hcn) {
				if (ls) {
					document.write("<A href=\"javascript: oc(" + nodeValues[0] + ", 1);\"><IMG id=\"join" + nodeValues[0] + "\" src=\"images/");
					 	if (ino) document.write("minus");
						else document.write("plus");
					document.write("bottom.gif\" align=\"absbottom\" /></A>");
				} else {
					document.write("<A href=\"javascript: oc(" + nodeValues[0] + ", 0);\"><IMG id=\"join" + nodeValues[0] + "\" src=\"images/");
						if (ino) document.write("minus");
						else document.write("plus");
					document.write(".gif\" align=\"absbottom\"  /></A>");
				}
			} else {
				if (ls) document.write("<IMG src=\"images/join.gif\" align=\"absbottom\" />");
				else document.write("<IMG src=\"images/joinbottom.gif\" align=\"absbottom\" />");
			}
			if (hcn) document.write("<A title=\"" + nodeValues[4] + "\" href=\"javascript: oc(" + nodeValues[0] + ", 0);\" onmouseover=\"window.status='" + nodeValues[4] + "';return true;\" onmouseout=\"window.status=' ';return true;\">"); 
      else document.write("<A title=\"" + nodeValues[4] + "\" href=\"" + nodeValues[3] + "\" onmouseover=\"window.status='" + nodeValues[4] + "';return true;\" onmouseout=\"window.status=' ';return true;\">");
			if (hcn) {
				document.write("<IMG id=\"icon" + nodeValues[0] + "\" src=\"images/repertoireferme")
					if (ino) document.write("open");
				document.write(".gif\" align=\"absbottom\" /> ");
			} else document.write("<IMG id=\"icon" + nodeValues[0] + "\" src=\"images/page.gif\" align=\"absbottom\" />");
			document.write(nodeValues[2]);
      document.write("</A><BR />");
			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>");
			}
			recursedNodes.pop();
		}
	}
}
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';
	}
}
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;
}

