/*---------------------------------------------------|
| dTree 3 TEST| www.destroydrop.com/javascript/tree/ |
|----------------------------------------------------|
| Copyright (c) 2002-2003 Geir Landrö                |
|                                                    |
| This script can be used freely as long as all      |
| copyright messages are intact.                     |
|                                                    |
|---------------------------------------------------*/

function node(a,i,p,t,u,ti,ta,ic,ico) {
	this.id = i;
	this.pid = p;
	this.text = t;
	this.url = u;
	this.title = ti;
	this.target = ta;
	this.icon = ic;
	this.iconOpen = ico;

	this._ai = a;
	this._io = true;
	this._hc = false;
	this._ls = false;
	this._bd = false;
	this._p = null;
};

var dtreeHandler = {
	_sn : null
};

function dtree(o) {
	this._obj = o;
	this._aNodes = [];
	this._root = new node(-1,-1);
	this._container = null;
	this._rendered = false;
	this._sn = null;
	this._ad = false;

	this.config = {
		target		: '_blank',
		useLines	: true,
		useIcons	: true,
		useSelect	: true,
		imgPath		: '/img/tree/'
	};

	this.icon = {
		root		: 'base.gif',
		folder		: 'folder.gif',
		folderOpen	: 'folderopen.gif',
		node		: 'page.gif',
		empty		: 'empty.gif',
		line		: 'line.gif',
		join		: 'join.gif',
		joinBottom	: 'joinbottom.gif',
		plus		: 'plus.gif',
		plusBottom	: 'plusbottom.gif',
		minus		: 'minus.gif',
		minusBottom	: 'minusbottom.gif',
		nlPlus		: 'nolines_plus.gif',
		nlMinus		: 'nolines_minus.gif'
	};
};

dtree.prototype.add = function(id,pid,text,url,title,target,icon,iconOpen) {
	this._aNodes[this._aNodes.length] = new node(this._aNodes.length,id,pid,text,url,title,target,icon,iconOpen);
};

dtree.prototype.toString = function() {
	if (document.getElementById) {
		if (!this._rendered) {
			document.write('<div id="dtree' + this._obj +'" class="dtree" style="display:none;"></div>');
			this._container = document.getElementById('dtree' + this._obj);
			this.drawRoot();
			this._container.style.display = 'block';
			this._rendered = true;
		} else alert("It is not allowed to draw the same tree instance more than once.");
	} else document.write("Your Browser is not supported");
	return "";
};

dtree.prototype.drawRoot = function() {
	for (var n=0; n<this._aNodes.length; n++) {
		if (this._root.id==this._aNodes[n].pid) {
			this._aNodes[n]._hc = true;
			this.generate(this._aNodes[n]);
			this.drawChildren(this._aNodes[n]);
			break;
		}
	}
};

dtree.prototype.drawChildren = function(node) {
	node._bd = true;
	var test = 0;
	for (var n=0; n<this._aNodes.length; n++) {
		if (this._aNodes[n].pid==node.id) {
			this.setCS(this._aNodes[n]);
			this._aNodes[n]._p = node;
			this.generate(this._aNodes[n]);
			test++;
		}
	}
	return (test>0);
};

dtree.prototype.setCS = function(node) {
	var nLast;
	for (var n=0; n<this._aNodes.length; n++) {
		if (this._aNodes[n].pid == node.id) node._hc = true;
		if (this._aNodes[n].pid == node.pid) nLast = this._aNodes[n].id;
	}
	if (nLast==node.id) node._ls = true;
};

dtree.prototype.generate = function(node) {
	var parent = (node.pid==this._root.id) ? this._container : document.getElementById('d' + this._obj + node.pid);

	var d = document.createElement('div');
	d.id = 'n' + this._obj + node.id;

	var text = document.createTextNode(node.text);
	var url = document.createElement('a');

	/* 16.09. thomas: immer url hinzufügen, da die Folder ebenso anklickbar sind */
	url.className= 'node';
	url.id = 's' + this._obj + node.id;


	if (node.url) {
		url.href = node.url;
		if (node.title) url.title = node.title;
		if (node.target) url.target = node.target;
	}
	else {
		url.href = 'javascript:' + this._obj + '.o(' + node._ai + ');';
	}

	url.appendChild(text);

	var children = d.cloneNode(true);
	if (node.pid != this._root.id) this.indent(node._p, d);

	var icon = document.createElement('img');

	if (node._hc && node.pid != this._root.id) {
		var j = icon.cloneNode(true);
		j.src = (!this.config.useLines) ? this.config.imgPath+this.icon.nlPlus : ((node._ls) ? this.config.imgPath+this.icon.plusBottom : this.config.imgPath+this.icon.plus);
		j.id = 'j' + this._obj + node.id;
		var jurl = document.createElement('a');
		jurl.href = 'javascript:' + this._obj + '.o(' + node._ai + ');';
		jurl.appendChild(j);
		d.appendChild(jurl);
	} else if (node.pid != this._root.id) {
		var j = icon.cloneNode(true);
		j.src = (!this.config.useLines) ? this.config.imgPath+this.icon.empty :
			((node._ls) ? this.config.imgPath+this.icon.joinBottom : this.config.imgPath+this.icon.join);
		d.appendChild(j);
	}

	if (this.config.useIcons) {
		if (!node.icon) node.icon = (this._root.id == node.pid) ? this.config.imgPath+this.icon.root : ((node._hc) ? this.config.imgPath+this.icon.folder : this.config.imgPath+this.icon.node);
		else node.icon = this.config.imgPath+node.icon;
		if (!node.iconOpen) node.iconOpen = (node._hc) ? this.config.imgPath+this.icon.folderOpen : this.config.imgPath+this.icon.node;
		else node.iconOpen = this.config.imgPath+node.iconOpen;
		icon.src = ((node._io) ? node.iconOpen : node.icon)
		icon.id = 'i' + this._obj + node.id;
		d.appendChild(icon);
	}

	(node.pid != this._root.id) ? d.appendChild(url) : d.appendChild(text);
	
	parent.appendChild(d);


	if (node._hc) {
		children.style.display = (node.pid != this._root.id) ? 'none' : 'block';
		children.id = 'd' + this._obj + node.id;
		parent.appendChild(children);
	}

};

dtree.prototype.indent = function(node, div) {
	if (node.pid != this._root.id) {
		var ind = document.createElement('img');
		ind.src = (node._ls || !this.config.useLines) ? this.config.imgPath+this.icon.empty : this.config.imgPath+this.icon.line;
		if (node._p && node._p.pid != this._root.id && node._p._hc) this.indent(node._p, div);
		div.appendChild(ind);
	}
};

dtree.prototype.o = function(e) {
	var cn = this._aNodes[e];
	if (!cn._bd) this.drawChildren(cn);
	cn._io = !cn._io;
	this.nodeStatus(cn);
};

dtree.prototype.nodeStatus = function(node) {
	var d = document.getElementById('d' + this._obj + node.id);
	var j = document.getElementById('j' + this._obj + node.id);
	j.src = (this.config.useLines) ?
	((node._io) ? ((node._ls) ? this.config.imgPath+this.icon.minusBottom:this.config.imgPath+this.icon.minus) : ((node._ls)?this.config.imgPath+this.icon.plusBottom : this.config.imgPath+this.icon.plus)):
	((node._io) ? this.config.imgPath+this.icon.nlMinus : this.config.imgPath+this.icon.nlPlus);
	if (this.config.useIcons) {
		var i = document.getElementById('i' + this._obj + node.id);
		if (node._hc) i.src = (node._io) ? node.iconOpen : node.icon;
	}
	d.style.display = (node._io) ? 'block' : 'none';
};

dtree.prototype.openTo = function(id) {
	for (var n=0; n<this._aNodes.length;n++) {
		if (this._aNodes[n].id == id) {
			this.openTo(this._aNodes[n].pid);
			if (this._aNodes[n]._hc && this._aNodes[n].pid != this._root.id && !this._aNodes[n]._io) this.o(n);
			break;
		}
	}
};

dtree.prototype.openAll = function() {
	if (this._ad) this.oAll();
	else {
		this.drawAll(this._root.id);
		this._ad = true;
	}
};

dtree.prototype.drawAll = function(id) {
	for (var n=0; n<this._aNodes.length;n++) {
		if (this._aNodes[n].pid==id && this._aNodes[n]._hc) {
			if (!this._aNodes[n]._bd)
				if (this.drawChildren(this._aNodes[n])) this.o(this._aNodes[n]._ai);
			this.drawAll(this._aNodes[n].id);
			if (this._aNodes[n]._ls) break;
		}
	}
}

dtree.prototype.oAll = function() {
	for (var n=0; n<this._aNodes.length;n++)
		if (this._aNodes[n]._hc && !this._aNodes[n]._io && this._aNodes[n].pid != this._root.id ) this.o(this._aNodes[n]._ai);
};

dtree.prototype.closeAll = function() {
	for (var n=0; n<this._aNodes.length;n++)
		if (this._aNodes[n]._hc && this._aNodes[n]._io) this.o(this._aNodes[n]._ai);
};


