tree = new treeObject();
//window.onload = new Function("tree.setEvents('#','return false;');");

function treeObject() {

	this.showTab = function(tabName) {
		if(tabName == 'example') 		this.showExample();
		else if(tabName == 'taxinomy') 	this.showTaxinomy();
		else if(tabName == 'search') 	this.showSearch();
		else 							this.showDir();
	}

	this.showTransparentTab = function(divID) {
		if(isMSIE || isAOL || isKmeleon) divID = "tree"; //isMSIE, isAOL, isKmeleon sont définis dans default.js. Je force à tree car les filters ne fonctionnent pas sur les sous div.
		var div = document.getElementById(divID);

/* Pas super en IE
		if(!div.style.opacity || div.style.opacity == 1) div.style.opacity = 0;
		if(!div.style.filter || div.style.filter == "alpha(opacity=100)") div.style.filter = "alpha(opacity=0)";

		this.progessTransparentTab(div.id);
*/	}

	this.progessTransparentTab = function(divID) {
		var div = document.getElementById(divID);

		var opacity = Math.round(parseFloat(div.style.opacity)*100)/100;
		if(opacity < 1) {
			opacity += 0.05;
			div.style.opacity = opacity;
			div.style.filter = "alpha(opacity=" + Math.round(opacity*100) + ")";
			window.setTimeout('tree.progessTransparentTab("' + divID + '")',50);
		}
	}

	this.showDir = function() {
		if(document.getElementById("tabMenu").className != "selected") {
			this.showTransparentTab("equMenu");

			document.getElementById("equMenu").style.display = "block";
			document.getElementById("tabMenu").className = "selected";
			document.getElementById("equSearch").style.display = "none";
			document.getElementById("tabSearch").className = "";
			document.getElementById("equTaxinomy").style.display = "none";
			document.getElementById("tabTaxinomy").className = "";
			document.getElementById("equExample").style.display = "none";
			document.getElementById("tabExample").className = "";
		}
	}

	this.showSearch = function() {
		if(document.getElementById("tabSearch").className != "selected") {
			this.showTransparentTab("equSearch");

			document.getElementById("equMenu").style.display = "none";
			document.getElementById("tabMenu").className = "";
			document.getElementById("equSearch").style.display = "block";
			document.getElementById("tabSearch").className = "selected";
			document.getElementById("equTaxinomy").style.display = "none";
			document.getElementById("tabTaxinomy").className = "";
			document.getElementById("equExample").style.display = "none";
			document.getElementById("tabExample").className = "";
			document.getElementById("fieldSearchTitre").focus();
		}
	}

	this.showTaxinomy = function() {
		if(document.getElementById("tabTaxinomy").className != "selected") {
			this.showTransparentTab("equTaxinomy");

			document.getElementById("equMenu").style.display = "none";
			document.getElementById("tabMenu").className = "";
			document.getElementById("equSearch").style.display = "none";
			document.getElementById("tabSearch").className = "";
			document.getElementById("equTaxinomy").style.display = "block";
			document.getElementById("tabTaxinomy").className = "selected";
			document.getElementById("equExample").style.display = "none";
			document.getElementById("tabExample").className = "";
		}
	}

	this.showExample = function() {
		if(document.getElementById("tabExample").className != "selected") {
			this.showTransparentTab("equExample");

			document.getElementById("equMenu").style.display = "none";
			document.getElementById("tabMenu").className = "";
			document.getElementById("equSearch").style.display = "none";
			document.getElementById("tabSearch").className = "";
			document.getElementById("equTaxinomy").style.display = "none";
			document.getElementById("tabTaxinomy").className = "";
			document.getElementById("equExample").style.display = "block";
			document.getElementById("tabExample").className = "selected";
		}
	}

	this.getChildNode = function(node) {
		if(node.id) {
			return document.getElementById("u" + node.id);
		} else {
			return null;
		}
	}

	this.display = function(node) {
		var childNode = this.getChildNode(node);

		if(childNode) {
			if(!childNode.style.display || childNode.style.display == "none") this.expand(node);
			else this.collapse(node);
		}

		this.cancelEvent();
	}

	this.collapse = function(node) {
		var childNode = this.getChildNode(node);

		if(childNode) {
			childNode.style.display = "none";
			node.className = "Texpand";
		}
	}

	this.expand = function(node) {
		var childNode = this.getChildNode(node);

		if(childNode) {
			childNode.style.display = "block";
			node.className = "Tcollapse";
		}
	}

	this.cancelEvent = function() { //for IE
		if(window.event) {
			window.event.cancelBubble = tree;
		}
	}

	this.collapseAll = function() {
		var liTags = document.getElementsByTagName('li');
		
		for (var i=0;i<liTags.length;i++) {
			this.collapse(liTags[i]);
		}
	}

	this.expandAll = function() {
		var liTags = document.getElementsByTagName('li');
		
		for (var i=0;i<liTags.length;i++) {
			var node = liTags[i];

			this.expand(node);
		}
	}

	this.expandTo = function(lastId) {
		if(lastId != 0) {
			var liTags = document.getElementsByTagName('li');
			lastId = "un" + lastId;
			finalNode = null;

			for (var i=0;i<liTags.length;i++) {
				var node = liTags[i];

				if(node.id) {
					if(lastId.indexOf(node.id) >= 0) {
						this.expand(node);
						finalNode = node;
					}
				}
			}

			if(finalNode) {
				if(finalNode.id.indexOf("u") == 0) {
					finalNode = document.getElementById(finalNode.id.substr(1,finalNode.id.length));
				}
				finalNode.className = finalNode.className + "Current";
			}
		}
	}

	this.setEvents = function(href, onclick) {
		var liTags = document.getElementsByTagName('li');

		for (var i=0;i<liTags.length;i++) {
			var node = liTags[i];

			if(node.className == "Tcollapse" || node.className == "TcollapseCurrent" || node.className == "Texpand" || node.className == "TexpandCurrent") {
				node.onclick = new Function("tree.display(this)");

				var label = node.innerHTML;
				var ref = node.id.substr(1);
				node.innerHTML = '<a href="' + href + ref + '" onclick="' + onclick + '">' + label + '</a>';

			} else if(liTags[i].className == "Tnode" || liTags[i].className == "TnodeCurrent") {
				node.onclick = new Function("tree.cancelEvent()");

				var label = node.innerHTML;
				var ref = node.id.substr(1);
				node.innerHTML = '<a href="' + href + ref + '" onclick="' + onclick + '">' + label + '</a>';
			}
		}
	}

	/*this.setRefInInput = function(href, inputID) {
		inputID = this.getInput();
		if(href && inputID) {
		var atribute = 'paramDiscipline=';
			var pos = href.indexOf(atribute);
			if(pos > -1) {
				var ref = href.substr(pos+atribute.length, href.length);
				if(window.opener && !window.opener.closed) {
					var inputDiscipline = window.opener.document.getElementById(inputID);
					if(inputDiscipline) {
						inputDiscipline.value = ref;
						inputDiscipline.focus();
						self.close();
					}
				} 
			}
		}
	}*/

	this.setRefInInput = function(href) {
		if(window.opener && !window.opener.closed) {
			var ref = this.getURLParamValue(href,"paramDiscipline");
			var inputID = this.getURLParamValue(document.location.href,"inputDiscipline");
			var inputDiscipline = window.opener.document.getElementById(inputID);
			if(ref && inputDiscipline) {
				inputDiscipline.value = ref;
				inputDiscipline.focus();
			}
		}
		self.close();
	}

	this.getURLParamValue = function(url,paramName) {
		var paramValue = null;
		var querystring = this.getURLQuerystring(url);

		if (querystring) {
			var arrayOfParameters = querystring.split("&");
			var tempParamName;
			var tempParamValue;
			var parameter;

			for (var i = 0; i < arrayOfParameters.length; i++) {
				parameter = arrayOfParameters[i];
				posEqual = parameter.indexOf("=");

				if(posEqual > 0) {
					tempParamName = parameter.substring(0,posEqual);
					tempParamValue = parameter.substring(posEqual+1,parameter.length);
				} else {
					tempParamName = parameter;
					tempParamValue = parameter;
				}

				if(tempParamName == paramName) {
					paramValue = tempParamValue;
					break;
				}
			}
		}
		return paramValue;
	}

	this.getURLQuerystring = function(url) {
		var querystring = null;

		if ((url != null) && (url != "")) {
			querystring = unescape(url);
			
			var posQuerystring = querystring.indexOf("?");
			if (posQuerystring > 0)	{
				var posAnchor = querystring.indexOf("#");
				if (posAnchor > 0) {
					querystring = querystring.substring(posQuerystring+1, posAnchor);
				} else {
					querystring = querystring.substring(posQuerystring+1);
				}
			} else {
				querystring = null;
			}
		}

		return querystring;
	}

}