function buildTextNodeTree(element, categories, type, divId) 
{
  	//instantiate the tree:
    tree = new YAHOO.widget.TreeView(element);
	for (var i=0, j=categories.length; i<j; i++) {
  		var nodeObj = new NodeObj();
  		nodeObj.label 		= categories[i][1];
  		nodeObj.identifier 	= categories[i][0];
  		nodeObj.type		= type;
  		nodeObj.divId		= divId;
  		var tempNode = new YAHOO.widget.TextNode(nodeObj, tree.getRoot(), false);
  		tempNode.labelStyle = "";
  		tempNode.setDynamicLoad(loadNodeData, 0);
	}

     // Expand and collapse happen prior to the actual expand/collapse,
     // and can be used to cancel the operation
     tree.subscribe("expand", function(node) {
     		
            YAHOO.log(node.index + " was expanded", "info", "example");
            node.labelStyle = "selectNormal";
            tree.draw();
            // return false; // return false to cancel the expand
         });

     tree.subscribe("collapse", function(node) {
            YAHOO.log(node.index + " was collapsed", "info", "example");
     		node.labelStyle = "";
     		selectedCategory[node.data.divId] = -1;
     		//alert("Colapse");
     		//alert(node.data.divId);
     		tree.draw();
         });

     // Trees with TextNodes will fire an event for when the label is clicked:
     tree.subscribe("labelClick", function(node) {
            YAHOO.log(node.index + " label was clicked", "info", "example");
            //alert("Click");
            //alert(node.data.divId);
            if(node.data.parent == -1){
            	selectedCategory[node.data.divId] = node;
            	//alert(dump(node.data));
            	//alert("click pe categ din div" + node.data.divId);
            } else {
            	selectedCategory[node.data.divId] = node;
            	
            }           
         });

	//The tree is not created in the DOM until this method is called:
      tree.draw();
}

function startSelectedCategory(divId)
{
	//alert(dump(divId));
	if(selectedCategory[divId] != -1){
		//YAHOO.log("Pornesc exercitiile cu " + dump(selectedCategory), "info", "exerciseCategory.js");
		refreshContent("firstExercise", selectedCategory[divId]);
	} else {
		alert(pleaseSelectCategory);
	}
}
							
function NodeObj() {
	this.label 		= "";
	this.identifier = 0;
	this.parent 	= -1;
	this.type 		= "";
	this.divId 		= -1;
}
function loadNodeData(node, fnLoadComplete)  {
	//We'll load node data based on what we get back when we
	//use Connection Manager topass the text label of the 
	//expanding node to the Yahoo!
	//Search "related suggestions" API.  Here, we're at the 
	//first part of the request -- we'll make the request to the
	//server.  In our success handler, we'll build our new children
	//and then return fnLoadComplete back to the tree.
	

	//prepare URL for XHR request:
	if(node.data.type == "category") {
		sUrl = "ajax/getExercisesByCategory.php?categoryId=" + node.data.identifier;
	} else if(node.data.type == "type"){
		sUrl = "ajax/getExercisesByType.php?typeId=" + node.data.identifier;
	}
	//prepare our callback object
	var callback = {
	
		//if our XHR call is successful, we want to make use
		//of the returned data and create child nodes.
		success: function(oResponse) {
			YAHOO.log("XHR transaction was successful.", "info", "example");
			YAHOO.log(oResponse.responseText);
			var oResults = eval("(" + oResponse.responseText + ")");
			//alert(dump(oResults));
			//alert( oResults );
			if(YAHOO.lang.isArray(oResults)) {
				for (var i=0, j=oResults.length; i<j; i++) {
			   		var nodeObj = new NodeObj();
			   		nodeObj.label 		= oResults[i]["name"];
			   		nodeObj.identifier 	= oResults[i]["exerciseId"];
			   		nodeObj.parent 		= oResults[i]["categoryId"];
			   		nodeObj.type		= "exercise";
			   		nodeObj.divId		= node.data.divId;											   																
					var tempNode = new YAHOO.widget.TextNode(nodeObj, node, false);
				}
			}												
			
			//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):
			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) {
			YAHOO.log("Failed to process XHR transaction.", "info", "example");
			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: 7000
	};
	
	//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);								
}							    										
