/*--------------------------------------------------|

| dTree 2.05 | 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.                    |

|                                                   |

| Updated: 17.04.2003                               |

|---------------------------------------------------|
|25.08.2005      GIGI79       www.gigi79.com        |
|                                                   |
|Modificato in modo da permettere che una stessa    | 
|cartella possa essere contenuta in due cartelle    | 
|--------------------------------------------------*/



// Node object

function Node(id, pid, name, url, title, target, icon, iconOpen, open) {

	this.id = id;

	this.pid = pid;

	this.name = name;

	this.url = url;

	this.title = title;

	this.target = target;

	this.icon = icon;

	this.iconOpen = iconOpen;

	this.ai = 0;
	
	this.Div=[]; //CONTIENE I RIFERIMENTI AI DIV
//	QUESTE PROPRIETA SONO STATE MESSE DENTRO DIV 
	this._io = open || false;

	this._is = false;

	this._ls = false;

	this._hc = false;
};

function DIV(id,io,is,ls,hc,p,div){
	this.id=id;
	this.isOpen=io;
	this.isSelected=is;
	this.last=ls;
	this.children=hc;
	this.pNode=p;
	this.pDiv=div;
}


// Tree object

function dTree(objName) {

	this.config = {

		target					: null,

		folderLinks			: true,

		useSelection		: true,

		useCookies			: false,

		useLines				: false,

		useIcons				: false,

		useStatusText		: false,

		closeSameLevel	: false,

		inOrder					: false

	}
	pathToSrc='/';
	this.icon = {

		root				:'',// pathToSrc+'img/homepage.png',//pathToSrc+'img/globe.gif',

		folder			: pathToSrc+'gfx/freccia.png',

		folderOpen	: pathToSrc+'gfx/freccia.png',

		node				: pathToSrc+'img/page.gif',

		empty				: pathToSrc+'gfx/empty.gif',

		line				: pathToSrc+'img/line.gif',

		join				: pathToSrc+'img/join.gif',

		joinBottom	: '',//pathToSrc+'img/joinbottom.gif',

		plus				: '',//pathToSrc+'img/plus.gif',

		plusBottom	: '/gfx/freccia_sub.png',

		minus		: '/gfx/freccia.png',

		minusBottom	:'/gfx/freccia.png',

		nlPlus		:'/gfx/freccia_sub.png',

		nlMinus		: '/gfx/freccia.png'

	};

	this.obj = objName;

	this.aNodes = [];

	this.aIndent = [];

	this.root = new Node(-1);

	this.selectedNode = null;

	this.selectedFound = false;

	this.completed = false;

};



// Adds a new node to the node array

dTree.prototype.add = function(id, pid, name, url, title, target, icon, iconOpen, open) {

	this.aNodes[this.aNodes.length] = new Node(id, pid, name, url, title, target, icon, iconOpen, open);

};



// Open/close all nodes

dTree.prototype.openAll = function() {

	this.oAll(true);

};

dTree.prototype.closeAll = function() {

	this.oAll(false);

};



// Outputs the tree to the page

dTree.prototype.toString = function() {

	var str = '<div class="dtree">\n';

	if (document.getElementById) {

		if (this.config.useCookies) this.selectedNode = this.getSelected();
		
		str += this.addNode(this.root,0);

	} else str += 'Browser not supported.';

	str += '</div>';

	if (!this.selectedFound) this.selectedNode = null;

	this.completed = true;

	return str;

};



// Creates the tree structure

	//modifica di Gigi79 23/07/2005
	//in questo modo si rende unico ogni nodo e si possono avere nodi con piu di 1 padre
	//viene utlilizzato per creare id unici dei DIV anche se sono di oggetti esistenti
	var progressivo=0;	

dTree.prototype.addNode = function(pNode,pDivSel) {

	var str = '';

	var n=0;

	if (this.config.inOrder) n = pNode.ai;

	for (n; n<this.aNodes.length; n++) {

		if (this.aNodes[n].pid == pNode.id) {

			var cn = this.aNodes[n];
			var DivSel=cn.Div.length;
			
			cn.Div[DivSel] = new DIV(progressivo++,cn._io,cn._is,cn._ls,cn._hc,pNode,pDivSel);

			cn.ai = n;

			this.setCS(cn,DivSel);
			
			
			if (!cn.target && this.config.target) cn.target = this.config.target;

			if (cn.Div[DivSel].children && !cn.Div[DivSel].isopen && this.config.useCookies) cn.Div[DivSel].isOpen = this.isOpen(cn.Div[DivSel].id);

			if (!this.config.folderLinks && cn.Div[DivSel].children) cn.url = null;

			if (this.config.useSelection && cn.Div[DivSel].id== this.selectedNode && !this.selectedFound) {
					
					cn.Div[DivSel].isSelected = true;

					this.selectedNode = cn.Div[DivSel].id;

					this.selectedFound = true;

			}

			str += this.node(cn,DivSel);  

			if (cn.Div[DivSel].last) break;

		}

	}

	return str;

};



// Creates the node icon, url and text

dTree.prototype.node = function(node, DivSel) {
	
	nodeId=node.Div[DivSel].id;
	
	var str = '<div class="dTreeNode">' + this.indent(node, DivSel);

	if (this.config.useIcons) {

		if (!node.icon) node.icon = (this.root.id == node.pid) ? this.icon.root : ((node.Div[DivSel].children) ? this.icon.folder : this.icon.node);

		if (!node.iconOpen) node.iconOpen = (node.Div[DivSel].children) ? this.icon.folderOpen : this.icon.node;

		if (this.root.id == node.pid) {

			node.icon = this.icon.root;

			node.iconOpen = this.icon.root;

		}

		if (((node.Div[DivSel].isOpen)?node.iconOpen:node.icon)!='')
			str += '<img id="i' + this.obj + nodeId + '" src="'+((node.Div[DivSel].isOpen)? node.iconOpen : node.icon) + '" alt="" border="0" />';

	}

	if (node.url) {

		str += '<a id="s' + this.obj + nodeId + '" class="' + ((this.config.useSelection) ? ((node.Div[DivSel].isOpen ? 'nodeSel' : ((node.Div[DivSel].children)?'node':'node_prod'))) : 'node') + '" href="' + node.url + '"';

		if (node.title) str += ' title="' + node.title + '"';

		if (node.target) str += ' target="' + node.target + '"';

		if (this.config.useStatusText) str += ' onmouseover="window.status=\'' + node.name + '\';return true;" onmouseout="window.status=\'\';return true;" ';

		if (this.config.useSelection && ((node.Div[DivSel].children && this.config.folderLinks) || !node.Div[DivSel].children))

			str += " onclick='javascript: " + this.obj + ".s(" + node.ai + ","+DivSel+");'";

		str += '>';

	}

	else if ((!this.config.folderLinks || !node.url) && node.Div[DivSel].children && node.pid != this.root.id)

		str += '<a href="javascript: ' + this.obj + '.o(' + node.ai + ','+DivSel+');" class="'+((node.Div[DivSel].children)?'node':'node_prod')+'">';

	str += node.name;

	if (node.url || ((!this.config.folderLinks || !node.url) && node.Div[DivSel].children)) str += '</a>';

	str += '</div>';

	if (node.Div[DivSel].children) {

		str += '<div id="d' + this.obj + nodeId + '" class="clip" style="display:' + ((this.root.id == node.pid || node.Div[DivSel].isOpen) ? 'block' : 'none') + ';">';

		str += this.addNode(node,DivSel);

		str += '</div>';

	}

	this.aIndent.pop();

	return str;

};



// Adds the empty and line icons

dTree.prototype.indent = function(node, DivSel) {

	nodeId=node.Div[DivSel].id;
	
	var str = '';

	if (this.root.id != node.pid) {

		for (var n=0; n<this.aIndent.length; n++)

			if(((this.aIndent[n]==1 && this.config.useLines)?this.icon.line:this.icon.empty)!='')
				str += '<img src="' + ((this.aIndent[n]==1 && this.config.useLines)?this.icon.line:this.icon.empty) + '" alt="" border="0" />';

		(node.Div[DivSel].last) ? this.aIndent.push(0) : this.aIndent.push(1);
		

		if (node.Div[DivSel].children) {
			
			str += '<a href="javascript: ' + this.obj + '.o(' + node.ai + ','+ DivSel +');"><img id="j' + this.obj + nodeId + '" src="';

			if (!this.config.useLines) str += (node.Div[DivSel].isOpen) ? this.icon.nlMinus : this.icon.nlPlus;

			else str +=( (node.Div[DivSel].isOpen) ? ((node.Div[DivSel].last && this.config.useLines) ? this.icon.minusBottom : this.icon.minus) : ((node.Div[DivSel].last && this.config.useLines) ? this.icon.plusBottom : this.icon.plus ) );

			str += '" alt="" border="0" /></a>';

		} //else str +='<img src="' + ( (this.config.useLines) ? ((node.Div[DivSel].last) ? this.icon.joinBottom : this.icon.join ) : this.icon.empty) + '" alt="" border="0" />';

	}

	return str;

};



// Checks if a node has any children and if it is the last sibling

dTree.prototype.setCS = function(node,DivSel) {

	var lastId;

	for (var n=0; n<this.aNodes.length; n++) {

		if (this.aNodes[n].pid == node.id) node.Div[DivSel].children = true;

		if (this.aNodes[n].pid == node.pid) lastId = this.aNodes[n].id;

	}

	if (lastId==node.id) node.Div[DivSel].last = true;

};



// Returns the selected node

dTree.prototype.getSelected = function() {

	var sn = this.getCookie('cs' + this.obj);

	return (sn) ? sn : null;

};



// Highlights the selected node

dTree.prototype.s = function(id,DivSel) {
	
	if (!this.config.useSelection) return;

	var cn = this.aNodes[id];
	
	nodeId=cn.Div[DivSel].id;

	if (cn.Div[DivSel].children && !this.config.folderLinks) return;

	if (this.selectedNode != nodeId) {

		if (this.selectedNode || this.selectedNode==0) {
			if(eOld = document.getElementById("s" + this.obj + this.selectedNode)) eOld.className = "node";
		}

		if(eNew = document.getElementById("s" + this.obj + nodeId)) eNew.className = "nodeSel";

		this.selectedNode = nodeId;

		if (this.config.useCookies) this.setCookie('cs' + this.obj, nodeId);

	}

};



// Toggle Open or close

dTree.prototype.o = function(id,DivSel) {
	
	var cn = this.aNodes[id];

	var IO=!cn.Div[DivSel].isOpen;

	this.nodeStatus(IO,id,DivSel, cn.Div[DivSel].last);

	cn.Div[DivSel].isOpen=IO;
	
	if (this.config.closeSameLevel) this.closeLevel(cn,DivSel);

	if (this.config.useCookies) this.updateCookie();

};



// Open or close all nodes

dTree.prototype.oAll = function(status) {

	for (var n=0; n<this.aNodes.length; n++) {
		for (var i=0; i<this.aNodes[n].Div.length; i++) {
			if (this.aNodes[n].Div[i].children && this.aNodes[n].pid != this.root.id) {
	
				this.nodeStatus(status, n,i, this.aNodes[n].Div[i].last)
	
				this.aNodes[n].Div[i].isOpen = status;

			}
		}
	}

	if (this.config.useCookies) this.updateCookie();

};



// Opens the tree to a specific node

dTree.prototype.openTo = function(nId, bSelect, bFirst) {

	if (!bFirst) {

		for (var n=0; n<this.aNodes.length; n++) {
			
			if (this.aNodes[n].id == nId) {

				nId=n;

				break;

			}

		}

	}

	var cn=this.aNodes[nId];
	for (var i=0;i<cn.Div.length;i++){
		DivSel=i;
		if (cn.Div[DivSel].pNode.id==this.root.id || !cn.Div[DivSel].pNode) return;
	
		cn.Div[DivSel].isOpen = true;
	
		cn.Div[DivSel].isSelect = bSelect;
	
		if (this.completed && cn.Div[DivSel].children) this.nodeStatus(true, cn.ai,DivSel, cn.Div[DivSel].last);
	
		if (this.completed && bSelect) this.s(cn.ai,DivSel);
	
		else if (bSelect) this.selectNode=cn.ai;
	
		
		this.openTo(cn.Div[i].pNode.ai, false, true);
	}
};



// Closes all nodes on the same level as certain node

dTree.prototype.closeLevel = function(node,DivSel) {

	for (var n=0; n<this.aNodes.length; n++) {

		for (var i=0; i<this.aNodes[n].Div.length; i++) {

			if (this.aNodes[n].Div[i].p.id == node.Div[DivSel].p.id && this.aNodes[n].id != node.id && this.aNodes[n].Div[i].children) {

				this.nodeStatus(false, n,i, this.aNodes[n].Div[i].last);

				this.aNodes[n].Div[i].isOpen = false;

				this.closeAllChildren(this.aNodes[n],i);

			}

		}

	}

}



// Closes all children of a node

dTree.prototype.closeAllChildren = function(node,DivSel) {

	for (var n=0; n<this.aNodes.length; n++) {
		for (var i=0; i<this.aNodes[n].Div.length; i++) {
		
			if (this.aNodes[n].pid == node.id && this.aNodes[n].Div[i].children) {
	
				if (this.aNodes[n].Div[i].isOpen) this.nodeStatus(false, n,i, this.aNodes[n].Div[i].last);
	
				this.aNodes[n].Div[i].isOpen = false;
	
				this.closeAllChildren(this.aNodes[n],i);		
	
			}
		}

	}

}



// Change the status of a node(open or closed)

dTree.prototype.nodeStatus = function(status,id,DivSel, bottom) {
	
	nodeId=this.aNodes[id].Div[DivSel].id;

	eDiv	= document.getElementById('d' + this.obj + nodeId);

	eJoin	= document.getElementById('j' + this.obj + nodeId);
	
	if (this.config.useIcons) {

		eIcon	= document.getElementById('i' + this.obj + nodeId);

		eIcon.src = (status) ? this.aNodes[id].iconOpen : this.aNodes[id].icon;

	}

	eJoin.src = (this.config.useLines)?

	((status)?((bottom)?this.icon.minusBottom:this.icon.minus):((bottom)?this.icon.plusBottom:this.icon.plus)):

	((status)?this.icon.nlMinus:this.icon.nlPlus);

	eDiv.style.display = (status) ? 'block': 'none';

};





// [Cookie] Clears a cookie

dTree.prototype.clearCookie = function() {

	var now = new Date();

	var yesterday = new Date(now.getTime() - 1000 * 60 * 60 * 24);

	this.setCookie('co'+this.obj, 'cookieValue', yesterday);

	this.setCookie('cs'+this.obj, 'cookieValue', yesterday);

};



// [Cookie] Sets value in a cookie

dTree.prototype.setCookie = function(cookieName, cookieValue, expires, path, domain, secure) {

	document.cookie =

		escape(cookieName) + '=' + escape(cookieValue)

		+ (expires ? '; expires=' + expires.toGMTString() : '')

		+ (path ? '; path=' + path : '')

		+ (domain ? '; domain=' + domain : '')

		+ (secure ? '; secure' : '');

};



// [Cookie] Gets a value from a cookie

dTree.prototype.getCookie = function(cookieName) {

	var cookieValue = '';

	var posName = document.cookie.indexOf(escape(cookieName) + '=');

	if (posName != -1) {

		var posValue = posName + (escape(cookieName) + '=').length;

		var endPos = document.cookie.indexOf(';', posValue);

		if (endPos != -1) cookieValue = unescape(document.cookie.substring(posValue, endPos));

		else cookieValue = unescape(document.cookie.substring(posValue));

	}

	return (cookieValue);

};



// [Cookie] Returns ids of open nodes as a string

dTree.prototype.updateCookie = function() {

	var str = '';

	for (var n=0; n<this.aNodes.length; n++) {
		for (var i=0; i<this.aNodes[n].Div.length; i++) {
			if (this.aNodes[n].Div[i].isOpen && this.aNodes[n].pid != this.root.id) {
	
				if (str) str += '.';
	
				str += this.aNodes[n].id;
	
			}
		}
	}

	this.setCookie('co' + this.obj, str);

};



// [Cookie] Checks if a node id is in a cookie

dTree.prototype.isOpen = function(id) {

	var aOpen = this.getCookie('co' + this.obj).split('.');

	for (var n=0; n<aOpen.length; n++)

		if (aOpen[n] == id) return true;

	return false;

};



// If Push and pop is not implemented by the browser

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;

	}

};