var Tree = function(parent, url){
	var self = this;
	this.prefix = 'Tree_';
	this.lastItemClicked =  null; // item is a node or leaf or the tree
	this.lastLeafClicked = null; //leaf only
	this.Parent = parent;
	this.popup = null;
	this.hash = {};
	this.url = url;
	
	this.printTree = function(data,container){		
		var ul = document.createElement('UL');
		
		for(var i = 0 ; i < data.length; i++)
		{
			var li = document.createElement('LI');
			li.id = this.prefix+data[i].id;
			if(data[i].count > 0 || (typeof(data[i].items)== "object" && data[i].items.length > 0))
			{
				li.className = "haveChilds"
			}
			var a = document.createElement('A');
			a.href = "#";
			
			a.innerHTML = data[i].name;
			li.appendChild(a);		
			
			if(i % 2 == 0)
			{
				a.className += " lihgtGray ";
			}
			li.onclick =  function (event){
				self.lastItemClicked = this.id.replace(self.prefix,'');
				if(this.className.indexOf("haveChilds") > -1){
					self.getBranchByOidUrl(this.id.replace(self.prefix,''),self.url, this);	
					self.lastLeafClicked = null;				
				}
				else{
					self.lastLeafClicked = this.id.replace(self.prefix,'');	
					TreeLeafClick();
					//self.togglePopup();											
				}
				cancelBubble(event)
				return false;
			}
			
			if(typeof(data[i].items)== "object" && data[i].items.length > 0)
			{
				this.printTree(data[i].items,li);
				li.className +=" openned "
			}
			ul.appendChild(li);
		}
		container.appendChild(ul);
		toggle(ul)
		this.hash[container.id.replace(this.prefix,'')] = ul
		return ul;
	}	
}

Tree.prototype.initPopup = function(object){
	this.popup = object;
	this.popup.onclick = function(event){
		cancelBubble(event)
	}
}
Tree.prototype.togglePopup = function(){
	if(this.popup.parentNode == document.getElementById(this.prefix+this.lastLeafClicked))
	{
		this.hidePopup()	
	}else{
		this.showPopup()
	}
	return false;
}
Tree.prototype.showPopup = function(){
	document.getElementById(this.prefix+this.lastLeafClicked).appendChild(this.popup);
	return false;
}
Tree.prototype.hidePopup = function(){
	try{
		document.getElementById(this.prefix+this.lastLeafClicked).removeChild(this.popup);
	}
	catch(e){}
	return false;
}
function setActiveClass(object){
	object.className += " active ";
}
function setDeactiveClass(object){
	object.className = object.className.replace("active","");
}
Tree.prototype.getBranchByOidUrl = function(oid,url, sender)
{
	var self = this;
	if('undefined' != typeof(self.hash[oid]))
	{
		if(self.hash[oid].style.display == 'block')
		{
			sender.className = sender.className.replace("openned","");
		}
		else{			
			sender.className += " openned ";
		}
		toggle(self.hash[oid]);
		
	}
	else{
		var requestUrl = "/dm/index/getbranch?oid="+oid+'&url='+url; 
		var c = new IO(requestUrl, 'utf-8', false); 
		c.handle(
			function (result_list) 
			{
				try{
					var  project = eval(result_list);
					sender.className = sender.className.replace("loading","");
					if(sender.getElementsByTagName("UL").length < 1)
					{
					  self.printTree(project,sender)
					}
					sender.className += " openned ";
				}
				catch (e) {
				}	
			
			}, sender.className +=" loading "
		); 
		c.say('get', {});
	}
	
}
Tree.prototype.getMyParents = function()
{
	var object = document.getElementById(this.prefix+this.lastLeafClicked);
	if(object == null) return null;
	var parent = object.parentNode;
	var treePath = "";
	while(parent != this.Parent)
	{
		if(parent.id.indexOf(this.prefix) > -1)
		{
			treePath += parent.id+",";
		}
		parent =  parent.parentNode;
	}
	return treePath+this.prefix+data.id;
}
