/*
 * DON'T REMOVE THE FOLLOWING LICENSE
 *           INFORMATION!
 * ----------------------------------
 * Copyright by
 * Dennis Ritz
 * Author: Dennis Ritz
 * dennis.ritz@gmx.net
 * 2007-2008
 * ----------------------------------
 */
function Tree() {
	this.name = "Tree";
	
	
	this.__toString = function __toString() {
		return this.name;
	}
	
	this.setProp = function setProp(p_node,p_prop,p_value) {
		var i=0;
		var treeID = null;
		var nodeID = null;
		var foundTreeRoot = false;
		var p_parentNode = p_node;
		while (foundTreeRoot == false){
			if(i>500)
				foundTreeRoot = true;
			if(p_parentNode.className == "tree" && p_parentNode.id != "") {
				treeID = p_parentNode.id;
				foundTreeRoot = true;
			}
			if(p_parentNode.className == "treeNode" && nodeID == null && p_parentNode.id != "") {
				nodeID = p_parentNode.id;
			}
			p_parentNode = p_parentNode.parentNode;
			i++;
		}
		if(treeID != null && nodeID != null) {
			Cookie.create(Tree.__toString()+"_"+treeID+"_"+nodeID+"_"+p_prop,p_value);
		}
	}
	
	this.getProp = function getProp(p_node,p_prop) {
		var i=0;
		var treeID = null;
		var nodeID = null;
		var foundTreeRoot = false;
		var p_parentNode = p_node;
		while (foundTreeRoot == false){
			if(i>500)
				foundTreeRoot = true;
			if(p_parentNode.className == "tree" && p_parentNode.id != "") {
				treeID = p_parentNode.id;
				foundTreeRoot = true;
			}
			if((p_parentNode.className == "treeNode" || p_parentNode.className == "treeNodeLeaf") && nodeID == null && p_parentNode.id != "") {
				nodeID = p_parentNode.id;
			}
			p_parentNode = p_parentNode.parentNode;
			i++;
		}
		if(treeID != null && nodeID != null) {
			return Cookie.read(Tree.__toString()+"_"+treeID+"_"+nodeID+"_"+p_prop);
		}else{
			return null;
		}
	}
	
	this.select = function select(evt,p_node) {
		var i=0;
		var obj = null;
		if(!evt) evt = window.event;
		var ctrlPressed=0;
		var selectedArr=new Array();
		var treeID = null;
		var newSelectedArr = new Array();
		var nodeID = null;
		var foundTreeRoot = false;
		var p_parentNode = p_node;
		if(typeof p_parentNode == "undefined") return;
		while (foundTreeRoot == false){
			if(i>500)
				foundTreeRoot = true;
			if(p_parentNode.className == "tree" && p_parentNode.id != "") {
				treeID = p_parentNode.id;
				foundTreeRoot = true;
			}
			if((p_parentNode.className == "treeNode" || p_parentNode.className == "treeNodeLeaf") && nodeID == null && p_parentNode.id != "") {
				nodeID = p_parentNode.id;
			}
			p_parentNode = p_parentNode.parentNode;
			i++;
		}
		if(treeID != null && nodeID != null) {
			var p_selected = Cookie.read(Tree.__toString()+"_"+treeID+"_selected");
			// NEWER BROWSERS [CROSS-PLATFORM]
			if(evt!=null)
				ctrlPressed =evt.ctrlKey;
			if(p_selected != null) {
				selectedArr = p_selected.split("|");
				for(i=0;i<selectedArr.length;i++) {
					if(nodeID == selectedArr[i]) {
						var obj = Browser.getElementById(selectedArr[i]);
					}
				}
			}
			if(evt != null && obj != null && Mouse.checkRClick(evt)){
				newSelectedArr = selectedArr;
			}else if(obj == null && ctrlPressed) {
				newSelectedArr = selectedArr;
				newSelectedArr.push(nodeID);
				Browser.getElementById(nodeID).style.background="url('img/GUI/Tree/active.png') repeat-y";
			}else if(obj != null && ctrlPressed) {
				obj.style.background="transparent";
				for(i=0;i<selectedArr.length;i++) { 
					if(nodeID != selectedArr[i]) {
						newSelectedArr.push(selectedArr[i]);
					}
				}
			}else if((obj != null && !ctrlPressed) || (obj == null && !ctrlPressed)) {
				for(i=0;i<selectedArr.length;i++) {	  
					var obj = Browser.getElementById(selectedArr[i]);
					if(obj != null){
						obj.style.background="transparent";
					}
				}
			  	newSelectedArr.push(nodeID);
			  	Browser.getElementById(nodeID).style.background="url('img/GUI/Tree/active.png') repeat-y";
			}
		}
		Cookie.create(Tree.__toString()+"_"+treeID+"_selected",newSelectedArr.join("|"));

		//alert("select: tree["+treeID+"] node["+nodeID+"]");
		
	}
	
	this.unselect = function unselect(p_node) {
		var i=0;
		var treeID = null;
		var nodeID = null;
		var foundTreeRoot = false;
		var selectedArr=new Array();
		var p_parentNode = p_node;
		while (foundTreeRoot == false){
			if(i>500)
				foundTreeRoot = true;
			if(p_parentNode.className == "tree" && p_parentNode.id != "") {
				treeID = p_parentNode.id;
				foundTreeRoot = true;
			}
			if((p_parentNode.className == "treeNode" || p_parentNode.className == "treeNodeLeaf") && nodeID == null && p_parentNode.id != "") {
				nodeID = p_parentNode.id;
			}
			p_parentNode = p_parentNode.parentNode;
			i++;
		}
		if(treeID != null && nodeID != null) {
			var p_selected = Cookie.read(Tree.__toString()+"_"+treeID+"_selected");
			selectedArr = p_selected.split("|");
			for(i=0;i<selectedArr.length;i++) {
				var obj = Browser.getElementById(selectedArr[i]);
		
				if(obj != null){
					obj.style.background="transparent";
				}
			}
		}

		Cookie.create(Tree.__toString()+"_"+treeID+"_selected",nodeID);
		nodeID = Cookie.read(Tree.__toString()+"_"+treeID+"_selected");
		//alert("select: tree["+treeID+"] node["+nodeID+"]");
		//p_node.style.background="url('img/GUI/Tree/active.png') repeat-y";
	}

	this.expand = function expand(p_node) {
		var elements = p_node.getElementsByTagName("DIV");
		for(var i in elements) {
			if(typeof elements[i] != "undefined"){
				if(elements[i].className == "treeNodeSub") {
					if(Browser.getDisplay(elements[i])) {
						this.setProp(p_node,"expand",false);
						Browser.setDisplay(elements[i],false);
						p_node.firstChild.childNodes[1].src = "img/GUI/Tree/expand.png";
					} else {
						this.setProp(p_node,"expand",true);
						Browser.setDisplay(elements[i],true);
						p_node.firstChild.childNodes[1].src = "img/GUI/Tree/collapse.png";
					}
					break;
				}
			}
		}
	}
	
	this.setToUserView = function setToUserView(p_node) {
		var i=0;
		var treeID = null;
		var nodeID = null;
		var foundTreeRoot = false;
		var selectedArr = new Array();
		var p_parentNode = p_node;
		if(p_node == null) return;
		while (foundTreeRoot == false){
			if(i>500)
				foundTreeRoot = true;
			if(p_parentNode.className == "tree" && p_parentNode.id != "") {
				treeID = p_parentNode.id;
				foundTreeRoot = true;
			}
			p_parentNode = p_parentNode.parentNode;
			i++;
		}
		var p_selected = null;
		
		if(treeID != null) {
			
			var p_selected = Cookie.read(Tree.__toString()+"_"+treeID+"_selected");
			if(p_selected !=null) {
				selectedArr = p_selected.split("|");
				for(i=0;i<selectedArr.length;i++) {
					var obj = Browser.getElementById(selectedArr[i]);
					if(obj != null){
						obj.style.background="url('img/GUI/Tree/active.png') repeat-y";
					}
				}
			}
		}
		
		var elements = p_node.getElementsByTagName("DIV");
		for(var i in elements) {
			if(typeof elements[i] != "undefined"){
				if(elements[i].className == "treeNodeSub") {
					if(this.getProp(elements[i],"expand")=='true'){
						Browser.setDisplay(elements[i],true);
						if(elements[i].firstChild)
						elements[i].previousSibling.childNodes[1].src = "img/GUI/Tree/collapse.png";
					}else{
						Browser.setDisplay(elements[i],false);
						if(elements[i].firstChild)
						elements[i].previousSibling.childNodes[1].src = "img/GUI/Tree/expand.png";
					}
				}

			}
		}
		var p_scroll = Cookie.read(Tree.__toString()+"_"+treeID+"_scroll");
		
		if(p_scroll == null)p_scroll = 0;
		Browser.getElementById(treeID).parentNode.scrollTop = p_scroll;
		//alert(p_scroll);
	}
	
	this.scrolling = function(p_container) {
		//alert(p_container.scrollTop);
		var treeID = p_container.firstChild.id;
		Cookie.create(Tree.__toString()+"_"+treeID+"_scroll",p_container.scrollTop);
	}
}
var Tree = new Tree();