/**************************************************************************
	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.
**************************************************************************/

// Tableau des noeuds et des icones
var nodes			= new Array();;
var openNodes		= new Array();
var icons			= new Array();
preloadIcons();

// Pour précharger toutes les icones
function preloadIcons() 
	{
	icons[0] = new Image();
	icons[0].src = "img/pucem.gif";
	icons[1] = new Image();
	icons[1].src = "img/pucem.gif";
	icons[2] = new Image();
	icons[2].src = "img/puceoff.gif";
	icons[3] = new Image();
	icons[3].src = "img/puceoff.gif";
	icons[4] = new Image();
	icons[4].src = "img/pixel.gif";
	icons[5] = new Image();
	icons[5].src = "img/pixel.gif";
	}

// Crée l'arbre
function createTree(arrName, nom, startNode, openNode) 
	{
	nodes = arrName;
	//document.write(nom);
	if (nodes.length > 0) 
		{
		if (startNode == null) startNode = 0;
		if (openNode != 0 || openNode != null) setOpenNodes(openNode);
	
		var recursedNodes = new Array();
		addNode(startNode, recursedNodes, nom);
		}
	}

// Retourne la position d'un noeud dans le tableau
function getArrayId(node) 
	{
	for (i=0; i<nodes.length; i++) 
		{
		var nodeValues = nodes[i].split("|");
		if (nodeValues[0]==node) return i;
		}
	}

// Met dans le tableau les noeuds qui vont être ouverts
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]);
			}
		} 
	}

// Vérifie si un noeud est ouvert
function isNodeOpen(node) 
	{
	for (i=0; i<openNodes.length; i++)
		{if (openNodes[i]==node) return true;}
	return false;
	}

// Vérifie si un noeud a des sous-noeuds
function hasChildNode(parentNode) 
	{
	for (i=0; i< nodes.length; i++) 
		{
		var nodeValues = nodes[i].split("|");
		if (nodeValues[1] == parentNode) return true;
		}
	return false;
	}

// Vérifie si un noeud est le dernier de la série
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;
	}

// ajoute un noeud à l'arbre
function addNode(parentNode, recursedNodes, nom) 
	{
	for (var i = 0; i < nodes.length; i++) 
		{
		if (nom == "Office") {var puce = "puceoff";}
		else {var puce = "pucem";}
		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]);

			// Write out line & empty icons
			for (g=0; g<recursedNodes.length; g++) 
				{
				document.write("<img src=\"img/empty.gif\" align=\"absbottom\" alt=\"\"  border=0/>");
				}

			// put in array line & empty icons
			if (ls) recursedNodes.push(0);
			else recursedNodes.push(1);

			// Si le noeud a des enfants
			if (hcn) 
				{
					document.write("<a class=tree href=\"javascript:oc(" + nodeValues[0] + ", 0,'"+ nom+"');\">");
					document.write("<img id=\"join"+ nom + nodeValues[0] + "\" src=\"img/");
					document.write(puce+".gif\" align=\"absbottom\" alt=\"Ouvrir / Fermer\"  border=0/></a>");
				} 
			else 
				{
				if (ls) document.write("<img src=\"img/joinbottom.gif\" align=\"absbottom\" alt=\"\" border=0/>");
				else 
					{
					if (nodeValues[1] != 0)
						{
						document.write("<img src=\"img/join.gif\" align=\"absbottom\" alt=\"\"  border=0/>");
						}
					else
						{
						document.write("<img id=\"icon"+ nom + nodeValues[0] + "\" src=\"img/pixel")
						document.write(".gif\"  width=10 height=1 align=\"absbottom\" border=0/>");
						document.write("<img src=\"img/"+puce+".gif\" align=\"absbottom\"  border=0/>");
						document.write("<img id=\"icon"+ nom + nodeValues[0] + "\" src=\"img/pixel")
						document.write(".gif\"  width=10 height=1 align=\"absbottom\" border=0/>");
						}
					}
				}

			// Start link
			if (hcn) 
				{
				document.write("<a class=tree href=\"javascript: oc(" + nodeValues[0] + ", 0,'"+ nom+"');\" ");
				document.write("onmouseover=\"window.status='" + nodeValues[2] + "';return true;\" ");
				document.write("onmouseout=\"window.status=' ';return true;\">");
				}
			else
				{
				document.write("<a class=tree href=\"" + nodeValues[3] + "\" ");
				document.write("onmouseover=\"window.status='" + nodeValues[2] + "';return true;\" ");
				document.write("onmouseout=\"window.status=' ';return true;\">");
				}
			// Write out folder & page icons
			if (hcn) 
				{
				document.write("<img id=\"icon"+ nom + nodeValues[0] + "\" src=\"img/pixel")
				document.write(".gif\"  width=1 height=1 align=\"absbottom\" border=0/>");
				} 
			//else document.write("<img id=\"icon" + nodeValues[0] + "\" src=\"img/page.gif\"  border=0 align=\"absbottom\" alt=\"Page\" />");
			
			// Write out node name
			document.write(nodeValues[2]);

			// End link
			document.write("</a><br />");
			
			// If node has children write out divs and go deeper
			if (hcn) 
				{
				document.write("<div id=\"div"+ nom + nodeValues[0] + "\"");
					if (!ino) document.write(" style=\"display: none;\"");
				document.write(">");
				addNode(nodeValues[0], recursedNodes, nom);
				document.write("</div>");
				}
			
			// remove last line or empty icon 
			recursedNodes.pop();
			}
		}
	}

// Opens or closes a node
function oc(node, bottom, nom) 
	{
	var theDiv = document.getElementById("div"+ nom + node);
	var theJoin	= document.getElementById("join"+ nom + node);
	var theIcon = document.getElementById("icon"+ nom + node);
	
	if (theDiv.style.display == 'none') 
		{
		if (bottom==1) 
			{
			if (nom == 'Office'){theJoin.src = icons[3].src;}
			else {theJoin.src = icons[1].src;}
			}
		else 
			{
			if (nom == 'Office'){theJoin.src = icons[2].src;}
			else {theJoin.src = icons[0].src;}
			}
		theIcon.src = icons[5].src;
		theDiv.style.display = '';
		} 
	else 
		{
		if (bottom==1) 
			{
			if (nom == 'Office'){theJoin.src = icons[3].src;}
			else {theJoin.src = icons[1].src;}
			}
		else 
			{
			if (nom == 'Office'){theJoin.src = icons[2].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;
	}
