/*
-----------------------------------------------------------------------------
	dTree 2.0  |  www.destroydrop.com/javascript/tree/
-----------------------------------------------------------------------------
	Copyright (c) 2002 Geir Landrö

	This script can be used freely as long as all copyright messages are
	intact.
-----------------------------------------------------------------------------
*/


// Node object
function Node(id, pid, name, url, img, target, isopen)
{
	this.id			= id;
	this.pid		= pid;
	this.name		= name;
	this.url		= url;
	this.target		= target;
	this.img		= img;

	this._io		= isopen || false;
	this._ls		= false;
	this._hc		= false;
	this._is		= false;
}


// Tree object
function dTree(objName)
{

// Variables
// ----------------------------------------------------------------------------

	this.arrNodes			= [];
	this.arrRecursed		= [];
	this.arrIcons			= [];
	this.rootNode			= -1;
	this.strOutput			= '';

	this.instanceName 		= objName;
	this.imgFolder			= 'beheer/sjabloom/cms/';
	this.target			= null;

	this.hasLines			= true;
	this.folderLinks		= true;


// Functies
// ----------------------------------------------------------------------------


	// Voegt een nieuwe vertakking toe aan de vertakking array
	this.add = function(id, pid, name, url, img, target, isopen)
	{
		this.arrNodes[this.arrNodes.length] = new Node(id, pid, name, url, img, target, isopen);
	}

	// Plaats boomstructuur op de pagina
	this.draw = function()
	{
		if (document.getElementById)
		{
			this.preloadIcons();
			this.addNode(0);
			document.writeln(this.strOutput);
		}
		else
		{
			document.writeln('Deze JavaScript functie kan niet worden getoond in uw browser. Update uw browser naar IE5 of hoger of NN6 of hoger.');
		}
	}


// Private
// ----------------------------------------------------------------------------

	// Voorladen van gebruikte afbeeldingen
	this.preloadIcons = function()
	{
		this.arrIcons[0] 	= new Image();
		this.arrIcons[0].src 	= this.imgFolder + 'plus.gif';
		this.arrIcons[1] 	= new Image();
		this.arrIcons[1].src 	= this.imgFolder + 'plusbottom.gif';
		this.arrIcons[2] 	= new Image();
		this.arrIcons[2].src 	= this.imgFolder + 'minus.gif';
		this.arrIcons[3] 	= new Image();
		this.arrIcons[3].src 	= this.imgFolder + 'minusbottom.gif';
		this.arrIcons[4] 	= new Image();
		this.arrIcons[4].src 	= this.imgFolder + 'folder.gif';
		this.arrIcons[5] 	= new Image();
		this.arrIcons[5].src 	= this.imgFolder + 'folderopen.gif';
	}

	// recursieve functie die de boomstructuur maakt
	this.addNode = function(pNode)
	{
		for (var n=0; n<this.arrNodes.length; n++)
		{
			if (this.arrNodes[n].pid == pNode)
			{
				var cn = this.arrNodes[n];
				cn._hc = this.hasChildren(cn);
				cn._ls = this.lastSibling(cn);
				
				// Als de huidige element niet de root is dan
				if (this.rootNode != cn.pid)
				{
					// Maak de lijnen
 					for (r=0; r<this.arrRecursed.length; r++)
  						this.strOutput += '<img src="' + this.imgFolder + ( (this.arrRecursed[r] == 1 ) ? 'line' : 'empty' ) + '.gif" alt="" />';

					// lijn en laatse icoon
					(cn._ls) ? this.arrRecursed.push(0) : this.arrRecursed.push(1);

					// Maak de plus en min iconen
					if (cn._hc)
					{
						this.strOutput += '<a href="javascript: ' + this.instanceName + '.o(' + n + ');">'
							+ '<img id="j' + this.instanceName + n + '" src="' + this.imgFolder;

						this.strOutput += ( (cn._io) ? ((cn._ls) ? 'minusbottom' : 'minus') : ((cn._ls) ? 'plusbottom' : 'plus' ) )
							+ '.gif" alt="" /></a>';
					}
					else
						this.strOutput += '<img src="' + this.imgFolder + ( (this.hasLines) ? ((cn._ls) ? 'joinbottom' : 'join' ) : 'empty') + '.gif" alt="" />';
				}

				// Links openen
				if (cn.url)
				{
					this.strOutput += '<a href="' + cn.url + '"';
					if (cn.target) this.strOutput += ' target="' + cn.target + '"';
					if (this.target && !cn.target) this.strOutput += ' target="' + this.target + '"';
					this.strOutput += '>';
				}
				if (!cn.url && cn._hc && cn.pid != this.rootNode)
				{
					this.strOutput += '<a href="javascript: ' + this.instanceName + '.o(' + n + ');">';
				}

				// Folder en menu iconen
				this.strOutput += '<img id="i' + this.instanceName + n + '" src="' + this.imgFolder;
				this.strOutput += (cn.img) ? cn.img : ((this.rootNode == cn.pid) ? 'base' : (cn._hc) ? ((cn._io) ? 'folderopen' : 'folder') : 'page') + '.gif';
				this.strOutput += '" alt="" />';

				// Write out span
				this.strOutput += '<span id="s' + this.instanceName + n + '" class="' + ((this.clickSelect) ? ((cn._is ? 'nodeSel' : 'node')) : 'node') + '">';


				// Write out element naam (de menu tekst dus)
				this.strOutput += cn.name;

					this.strOutput += '</span>';

				// Link sluiten
				if (cn.url || cn._hc) this.strOutput += '</a>';

				this.strOutput += '<br />\n';

				// Als het een map is met kinderen dan diepte ingaan...
				if (cn._hc)
				{
					this.strOutput += '<div id="d' + this.instanceName + n + '" style="display:'
					+ ((this.rootNode == cn.pid || cn._io) ? 'block' : 'none')
					+ ';">\n';
					this.addNode(cn.id);
					this.strOutput += '</div>\n';
				}
				this.arrRecursed.pop();
			}
		}
	}

	// Controleer of er sprake is van het element (node) map
	this.hasChildren = function(node)
	{
		for (n=0; n<this.arrNodes.length; n++)
			if (this.arrNodes[n].pid == node.id) return true;
		return false;
	}

	// Controler of laatste element (node) is
	this.lastSibling = function(node)
	{
		var lastId;
		for (n=0; n< this.arrNodes.length; n++)
			if (this.arrNodes[n].pid == node.pid) lastId = this.arrNodes[n].id;
		if (lastId==node.id) return true;
		return false;
	}


	// Toggle Open or close
	this.o = function(id)
	{
		cn = this.arrNodes[id];

		(cn._io) ? this.nodeClose(id,cn._ls) : this.nodeOpen(id,cn._ls);
		cn._io = !cn._io;

	}

	// Open een map
	this.nodeOpen = function(id, bottom)
	{
		eDiv	= document.getElementById('d' + this.instanceName + id);
		eJoin	= document.getElementById('j' + this.instanceName + id);
		eIcon	= document.getElementById('i' + this.instanceName + id);
		eJoin.src = (bottom) ?  this.arrIcons[3].src : this.arrIcons[2].src;
		if (!this.arrNodes[id].img) eIcon.src = this.arrIcons[5].src;
		eDiv.style.display = 'block';
	}

	// Sluit een map
	this.nodeClose = function(id, bottom)
	{
		eDiv	= document.getElementById('d' + this.instanceName + id);
		eJoin	= document.getElementById('j' + this.instanceName + id);
		eIcon	= document.getElementById('i' + this.instanceName + id);
		eJoin.src = (bottom) ? this.arrIcons[1].src : this.arrIcons[0].src;
		if (!this.arrNodes[id].img) eIcon.src = this.arrIcons[4].src;
		eDiv.style.display = 'none';
	}

}


// Functions used by the dTree object but are not really a part of it
// ------------------------------------------------------------------------------------------------

// Push and pop for arrays is not implemented in Internet Explorer
if (!Array.prototype.push) {
	Array.prototype.push = function array_push() {
		for(var i=0;i<arguments.length;i++)
			this[this.length]=arguments[i];
		return this.length;
	}
}
if (!Array.prototype.pop) {
	Array.prototype.pop = function array_pop() {
		lastElement = this[this.length-1];
		this.length = Math.max(this.length-1,0);
		return lastElement;
	}
}
