//das Objekt fŸr einen Eintrag
function treeEntry(lable,id,chardatree)
{
	this.label=lable;
	this.id=id;
	
	this.chardatree = chardatree; 
	// href wird nicht benštigt in diesem Fall aber von YUI 
	// vorrausgesetzt
	this.href;
}
function chardatree(htlmen,treeuri,currentident,usecheckbox,clickfun,filtertext) {
	this.current = currentident;
	this.treeurl = treeuri;
	this.checkbox = usecheckbox;
	this.tree = new YAHOO.widget.TreeView(htlmen);
	this.filter="";
	if(filtertext!=undefined && filtertext!=null)this.filter=filtertext;
	
	fun= clickfun;

  this.onNodeClick = function(oNode) {
	
	oNode.propagateHighlightUp=true;
	if(typeof(fun)=="function")fun(oNode.data.id);
	//onTreeItemClicked();
  }

   this.tree.subscribe('dblClickEvent', function(oArgs) {
	//alert(oArgs);
	/*
	onTreeItemClicked(oNode.data.id);
	
	this.tree.removeChildren(oNode);
	 
	oNode.expandAll;	*/
});
   
   //tree.setNodesProperty('propagateHighlightUp',true);
   //tree.setNodesProperty('propagateHighlightDown',true);
   if (this.checkbox != undefined && this.checkbox == 1 ) {
	this.tree.subscribe('clickEvent',this.tree.onEventToggleHighlight);	
   }else{
	this.tree.subscribe("labelClick", this.onNodeClick);	
   }
  
   //turn dynamic loading on for entire tree:
   this.tree.setDynamicLoad(this.loadNodes);
   //get root node for tree:
   var node = this.tree.getRoot();
     node.data.id="0000";
     node.data.chardatree =this;
     this.loadNodes(node);  	
   //render tree with these toplevel nodes; all descendants of these nodes
   //will be generated as needed by the dynamic loader.
   this.tree.draw();
   this.filtertree=function(text){
 		this.filter=text;
		this.tree.removeChildren(this.tree.getRoot());
		this.loadNodes(this.tree.getRoot()); 
	}
   return this;
}




chardatree.prototype.loadNodes = function(node, fnLoadComplete)  {
	//alert(this.current);
	//prepare URL for XHR request:
	var nodeId = encodeURI(node.data.id);
	//if (nodeId == undefined) nodeId = "0000";
	var sUrl = node.data.chardatree.treeurl+"/?node="+nodeId+"&current="+node.data.chardatree.current+"&filtertext="+node.data.chardatree.filter;
        node.focus();
	var callback = {	
		success: function(oResponse) {		
			//var oResults = eval("(" + oResponse.responseText + ")");
			var oResults = YAHOO.lang.JSON.parse(oResponse.responseText);
			if((oResults) && (oResults.length)) {
				//Result is an array if more than one result, string otherwise
				if(YAHOO.lang.isArray(oResults)) {
					for (var i=0, j=oResults.length; i<j; i++) {
 						var newNode = new treeEntry(oResults[i].text,oResults[i].id,node.data.chardatree);
						var tempNode = new YAHOO.widget.TextNode(newNode, node, oResults[i].expanded);
						tempNode.dblcick=function(oArgs){}
						
					}
				} else {
					
					var newNode = new treeEntry(oResults.text,oResults.id,node.data.chardatree);
					var tempNode = new YAHOO.widget.TextNode(newNode, node,  oResults.expanded)
				}
			}
								
			//When we're done creating child nodes, we execute the node's
			//loadComplete callback method which comes in via the argument
			//in the response object (we could also access it at node.loadComplete,
			//if necessary):
			
			node.loadComplete();
			//oResponse.argument.fnLoadComplete();
		},
		
		//if our XHR call is not successful, we want to
		//fire the TreeView callback and let the Tree
		//proceed with its business.
		failure: function(oResponse) {
			oResponse.argument.fnLoadComplete();
		},
		
		//our handlers for the XHR response will need the same
		//argument information we got to loadNodeData, so
		//we'll pass those along:
		argument: {
			"node": node,
			"fnLoadComplete": fnLoadComplete
			
		},
		
		//timeout -- if more than 7 seconds go by, we'll abort
		//the transaction and assume there are no children:
		timeout: 15000
	};
	
	//With our callback object ready, it's now time to 
	//make our XHR call using Connection Manager's
	//asyncRequest method:
	YAHOO.util.Connect.asyncRequest('get', sUrl, callback);
}



