var path_url = "index.php";
var backgroundColor = new Array("#ffe499", "#ffecb7", "#fff5da");
var styleColor = new Array("menustyle", "menustyle", "menustyle");
var path_spacer = 'images/spacer.gif';
var path_freccia = 'images/freccia_menu.gif';
var borderCol = "#FFFFFF";
var width = 207; //larghezza compresi i bordini
var height = 17;
var style = 'horizontal'; //valori possibili -> vertical, horizontal, select, table
var dMap = false;

function getAbsX(elt){
 return (elt.x) ? elt.x : getAbsPos(elt,"Left");
}

function getAbsY(elt){
 return (elt.y) ? elt.y : getAbsPos(elt,"Top");
}

function getAbsPos(elt,which){
	iPos = 0;
	while (elt != null){
		iPos += elt["offset" + which];
		elt = elt.offsetParent;
	} 
	
	return iPos;
}

function Is(){
	var agent = navigator.userAgent.toLowerCase();
	this.major = parseInt(navigator.appVersion);
	this.minor = parseFloat(navigator.appVersion);
	this.ns = ((agent.indexOf('mozilla')!= -1) && ((agent.indexOf('spoofer')== -1) && (agent.indexOf('compatible')== -1)));
	this.ns4 = (this.ns && (this.major == 4));
	this.ns6 = (this.ns && (this.major >= 5));
	this.ie = (agent.indexOf("msie")!= -1);
	this.ie4 = (this.ie && (this.major == 4) && (agent.indexOf("msie 5.")== -1));
	this.ie5 = (this.ie && (this.major >= 4)&& (agent.indexOf("msie 5.")!=-1));
}

function aaa(anim){ 
	var is = new Is();
	if(is.ns6){
		xPos = document.images[anim].offsetLeft
	}
	
	else if(document.getElementById){
			myObject = document.getElementById(anim)
			xPos = getAbsX(myObject)
		}else if(document.layers){
					var doc =eval('document.'+anim)
					myObject = doc
					xPos = getAbsX(myObject)
				}else if (document.all) {
							var doc =eval('document.all.'+anim)
							myObject=doc
							xPos = getAbsX(myObject)
						}  

	return(xPos);
}

function clTimeout(){
	if(timerID) clearTimeout(timerID);
}

function timeout(){
	if(timerID) clearTimeout(timerID);
	timerID = setTimeout("d.closeAll()",3000);
}

function str_replace(what, wwhat, where){
	re = eval("/"+ what + "/gi")
	return where.replace(re, wwhat);
}

function WoR(){
	var top = getAbsY(document.layerMenu);
	var left = getAbsX(document.layerMenu);
	
	for(i=0;i<d.principalNode.length;i++){
		document.getElementById(d.principalNode[i]).style.top = top;
		document.getElementById(d.principalNode[i]).style.left = (left + (width * i)) - i;
	}
}



var timerID;

// Node object
function Node(id, fatherId, name, url, other, isOpen) {
	this.id = id;
	this.fatherId = fatherId;
	this.name = name;
	this.url = url;
	this.other = other;
	this.level = 0;
	this.isOpen = isOpen || false;
	this.lastChild = false;
	this.hasChildren = false;
};

//Tree object
function Tree(objName, width, height, style, indent, home, overEffects) {
	this.config = {
		viewHome		: home || false,
		useCookies		: true,
		useStatusText	: true,
		closeSameLevel	: true,
		width			: width || 200,
		height			: height || 25,
		indent			: indent || false,
		style			: style || 'vertical',
		top				: top || 0,
		left			: left || 0,
		overEffects		: overEffects				
	}
	
	this.obj = objName;
	this.Nodes = [];
	this.Indent = [];
	this.principalNode = [];
};

// Outputs the tree to the page
Tree.prototype.toString = function() {
	for (n=0; n<this.Nodes.length; n++) this.findChildren(this.Nodes[n]);
	var str = '';
	
	//if(this.config.style == 'select') str += "<select>";
	if(this.config.viewHome == true){
		if(this.config.style == 'select') str += this.firstNodeSelect(this.Nodes[0],this.Nodes[0].id);
		if(this.config.style == 'table') str += this.firstNodeTable(this.Nodes[0],this.Nodes[0].id);
	}
	
	if (document.getElementById) {
		str += this.addNode(this.Nodes[0]);
	} else str += 'Browser not supported.';
	
	//if(this.config.style == 'select') str += "</select>";
	return str;
};

// Creates the tree structure
Tree.prototype.addNode = function(pNode) {
	var str = '';
	var n=0;
	
	for (n; n<this.Nodes.length; n++) {
		if (this.Nodes[n].fatherId == pNode.id) {
			var cn = this.Nodes[n];
			if (cn.hasChildren && !cn.isOpen && this.config.useCookies) cn.isOpen = this.isOp(cn.id);
			if(this.config.style == 'select') str += this.nodeSelect(cn,n);
				else  if(this.config.style == 'table') str += this.nodeTable(cn,n);
					else str += this.node(cn,n);
			if (cn.lastChild) break;
		}
	}
	return str;
};

// Creates the node icon, url and text
Tree.prototype.node = function(node, nodeId) {
	var str = '';
	var bgcolor;
	this.findLevel(node);
	bgcolor = backgroundColor[node.level];
	css = styleColor[node.level];
	
	if(node.level == 0){
		this.principalNode.push(this.obj + nodeId);
		left = left + this.config.width - 1;
	}
	
	var positioning = '';
	if(this.config.style == 'horizontal' && node.level == 0) positioning = 'position:absolute; top:' + top + 'px; left:' + left + 'px;';
	
	if(node.level == 0) str += '<div id="' + this.obj + nodeId + '" style="' +positioning+ 'width:' + this.config.width + 'px;  background-color: '+bgcolor +'">';
	
	str += '<table width="' + this.config.width + '" cellspacing="0" cellpadding="0" border="0" bgcolor="'+bgcolor +'">';
	
	//if(node.level == 0) str += '<tr><td colspan="5" height="1" bgcolor="' +borderCol+ '"><img src="' +path_spacer+ '" height="1"></td></tr>';
	
	str += '<tr><td width="1" bgcolor="' +borderCol+ '"><img src="' +path_spacer+ '" height="1"></td><td width="5"><img src="' +path_spacer+ '" height="1"></td>';
	
	
	
	str += '<td width="' + (this.config.width - 16) + '" height="' + this.config.height + '"';
	if(node.level == 0) str += ' align="center"';
	str += '>';
	if(this.config.indent) str += this.indent(node,'&nbsp;&nbsp;');	
	
	
	if(node.hasChildren){
		if(this.config.overEffects){
			if(node.level == 0) link = ' href="javascript:;"'// onmouseout="' + this.obj + '.o(' + nodeId + ');"';
				else link = ' href="javascript:' + this.obj + '.o(' + nodeId + ');"'// onmouseout="' + this.obj + '.o(' + nodeId + ');"';
		}else{
			link = ' href="javascript:;"'
		}
	}else{
		link = ' href="' + path_url + '?sid=' + node.url + '"';
	}
	
	str += '<a';
	if(this.config.overEffects){
		if(node.level == 0) str += ' onmouseover="' + this.obj + '.o(' + nodeId + ');clTimeout()" onmouseout="timeout()"';
			else str += ' onmouseover="clTimeout()" onmouseout="timeout()"';
	}
	str +=  ' class="' + css + '" ' + link + '>' + node.name + '</a>';
	str += '</td>';
	
	if(node.hasChildren && node.level != 0) str += '<td width="9" align="center" valign="bottom"><img src="' +path_freccia+ '"></td>';
		else str += '<td width="5"><img src="' +path_spacer+ '" height="1"></td>';
	
	str += '<td width="1" bgcolor="' +borderCol+ '"><img src="' +path_spacer+ '" height="1"></td></tr>';
	str += '<tr><td colspan="5" height="1" bgcolor="' +borderCol+ '"><img src="' +path_spacer+ '" height="1"></td></tr>';
	str += '</table>';
	
	if (node.hasChildren) {
		str += '<div id="d' + this.obj + nodeId + '" style="display:' + ((node.isOpen) ? 'block' : 'none') + ';  width:' + this.config.width + 'px">';
		str += this.addNode(node);
		str += '</div>\n';
	}
	if(node.level == 0) str += '</div>\n';
	this.Indent.pop();
	return str;
};

// Creates the node icon, url and text
Tree.prototype.firstNodeSelect = function(node, nodeId) {
	var str = '';
	var bgcolor;
	this.findLevel(node);
	bgcolor = backgroundColor[node.level];
	css = styleColor[node.level];
	
	str += '<option class="' + css + '" style="background-color:' + bgcolor + '" value="' + node.id + '">' + node.name + '\n';
	
	this.Indent.push();
	return str;
};

// Creates the node icon, url and text
Tree.prototype.nodeSelect = function(node, nodeId) {
	var str = '';
	var bgcolor;
	this.findLevel(node);
	bgcolor = backgroundColor[node.level];
	css = styleColor[node.level];
	
	str += '<option class="' + css + '" style="background-color:' + bgcolor + '"';
	//if(!node.hasChildren)  
	str+= ' value="' + node.id + '"';
	str += '>'
	if(this.config.indent) str += this.indent(node,'&nbsp;&nbsp;&nbsp;&nbsp;');
	str += node.name + '\n';
	
	if (node.hasChildren) {
		str += this.addNode(node);
	}
	
	this.Indent.pop();
	return str;
};


// Creates the node icon, url and text
Tree.prototype.firstNodeTable = function(node, nodeId) {
	var str = '';
	var bgcolor;
	this.findLevel(node);
	bgcolor = backgroundColor[node.level];
	css = styleColor[node.level];
	
	str += '<tr><td align="right" height="25"><a class="red" href="javascript:updateOBJ(' +node.id+ ',document.updateOBJ);"><img src="' +image_path+ 'modify.gif" width="16" height="16" border="0" alt="MODIFICA"></a></td>\n';
	str += '<td bgcolor="' + bgcolor + '">';
	if(this.config.indent) str += this.indent(node,'&nbsp;&nbsp;&nbsp;&nbsp;');
	str += node.name + '</td>\n';
	str += '<td>&nbsp;</td>';
	
	str += '</tr>\n';
	
	this.Indent.push();
	return str;
};

// Creates the node icon, url and text
Tree.prototype.nodeTable = function(node, nodeId) {
	var str = '';
	var bgcolor;
	this.findLevel(node);
	bgcolor = backgroundColor[node.level];
	css = styleColor[node.level];
	
	if(this.config.viewHome) levelMax = 2;
		else levelMax = 1;
	
	
	str += '<tr><td>';
	if(this.config.indent) str += this.indent(node,'&nbsp;&nbsp;');
	str += node.name + '</td>\n';
	str += '</tr>\n';
	
	if (node.hasChildren) {
		str += this.addNode(node);
	}
	
	this.Indent.pop();
	return str;
};


// [Cookie]
Tree.prototype.isOp = function(id) {
	var io = this.getCookie('co' + this.obj).split('.');
	for (var n=0; n<io.length; n++)
		if (io[n] == id) return true;
	return false;
};

// [Cookie] Clears a cookie
Tree.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
Tree.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
Tree.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
Tree.prototype.updateCookie = function() {
	var str = '';
	for (var n=0; n<this.Nodes.length; n++) {
		if (this.Nodes[n].isOpen) {
			if (str) str += '.';
			str += this.Nodes[n].id;
		}
	}
	this.setCookie('co' + this.obj, str);
};

// Adds a new node
Tree.prototype.add = function(id, fatherId, name, url, isOpen) {
	this.Nodes[this.Nodes.length] = new Node(id, fatherId, name, url, isOpen);
};

// Checks if a node has any children and if it is the last sibling
Tree.prototype.findChildren = function(node) {
	var lastId;
	for (var n=0; n<this.Nodes.length; n++) {
		if (this.Nodes[n].fatherId == node.id) node.hasChildren = true;
		if (this.Nodes[n].fatherId == node.fatherId) lastId = this.Nodes[n].id;
	}
	if(lastId==node.id) node.lastChild = true;
};

Tree.prototype.findLevel = function(node) {
	node.level = this.Indent.length;
	(node.lastChild) ? this.Indent.push(0) : this.Indent.push(1);
};

Tree.prototype.indent = function(node,spacer) {
	var str = '';
	for (var n=0; n<node.level; n++) str += spacer;
	return str;
};

// Toggle Open
Tree.prototype.o = function(id) {
	var cn = this.Nodes[id];
	this.closeLevel(this.Nodes[id]);
	this.nodeStatus(!cn.isOpen, id);
	cn.isOpen = !cn.isOpen;
	if(!cn.isOpen) this.closeAllChildren(this.Nodes[id]);
	//timeout();
};

// Open or close all nodes
Tree.prototype.oAll = function(status) {
	for (var n=0; n<this.Nodes.length; n++) {
		if (this.Nodes[n].hasChildren && this.Nodes[n].fatherId != 0) {
			this.nodeStatus(status, n)
			this.Nodes[n].isOpen = status;
		}
	}
};

// Open/close all nodes
Tree.prototype.openAll = function() {
	this.oAll(true);
};
Tree.prototype.closeAll = function() {
	this.oAll(false);
};

// Change the status of a node(open or closed)
Tree.prototype.nodeStatus = function(status, id) {
	eDiv = document.getElementById('d' + this.obj + id);
	//alert(eDiv.style.display);
	eDiv.style.display = (status) ? 'block': 'none';
};

// Closes all nodes on the same level as certain node
Tree.prototype.closeLevel = function(node) {
	for (var n=0; n<this.Nodes.length; n++) {
		if (this.Nodes[n].fatherId == node.fatherId && this.Nodes[n].id != node.id && this.Nodes[n].hasChildren) {
			this.nodeStatus(false, n);
			this.Nodes[n].isOpen = false;
			this.closeAllChildren(this.Nodes[n]);
		}
	}
}

Tree.prototype.closeAllChildren = function(node) {
	for (var n=0; n<this.Nodes.length; n++) {
		if (this.Nodes[n].fatherId == node.id && this.Nodes[n].hasChildren) {
			if (this.Nodes[n].isOpen) this.nodeStatus(false, n);
			this.Nodes[n].isOpen = false;
			this.closeAllChildren(this.Nodes[n]);
		}
	}
}

// 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;
	}
};
