
//General configuration

var sImageNone = sImagesPath + "none.gif";
var sImagePlus = sImagesPath + "plus.gif";
var sImageMinus = sImagesPath + "minus.gif";

//Styles
var sNodeLoadingClass = "BeingLoadedNode";
var sNodeClass = "TreeNode";

//Text
var sCancelLoading = "يک لحظه لطفا ...";

//Timeout during which node will be loaded
var timeOut = 60000;

//Selected node
var oSelNode;

//Curent loading node. Only one node can be being loading at one moment!*/
var beingLoadedNode;

//Current node counter. We use it for generation unique ID
var nodeId = 0;

//Init data and start loading root tree
function initPage(){
	oSelNode = null;
	//set beingLoadedNode to the tree container
	beingLoadedNode = document.getElementById("Tree");
	processLoading(sRootTree);	
}

//Load subtree into hidden frame
function processLoading(sTreeURL){	
	window.tocLoader.location = sTreeURL;
	//We will cancel loading of tree if it willn't have success during timeout
	//window.setTimeout("cancelLoadByTimeout('" + beingLoadedNode.id + "');", timeOut);
	
}

//Show hint [Loading...]
function showBeingLoadedNode(){
	var oDIV = beingLoadedNode.appendChild(document.createElement("div"));
	oDIV.className = sNodeLoadingClass;
	var oA = oDIV.appendChild(document.createElement("a"));
	oA.href = "#";
	oA.title = "انصراف ..."
	oA.onclick = cancelLoad;	
	var	oText = oA.appendChild(document.createTextNode(sCancelLoading));
}

//Hide hint [Loading...]
function hideBeingLoadedNode(){	
	if (beingLoadedNode.childNodes.length>1){
		var oDIV = beingLoadedNode.childNodes[1];			
		beingLoadedNode.removeChild(oDIV); 
	}
}

//Cancel loading of node
function cancelLoadByTimeout(id){
	//Check if node is still  loaded
	if (beingLoadedNode!=null && beingLoadedNode.id==id){
		hideBeingLoadedNode();
		//Check whether its top container node
		if (beingLoadedNode.childNodes.length>0){
			beingLoadedNode.childNodes[0].childNodes[0].src = sImageNone;
			beingLoadedNode.childNodes[0].childNodes[0].onclick = null;
		}
		beingLoadedNode = null;	
		alert("عدم موفقيت در بارگذارِی ساختار درختي");			
	}
}

//This function handles OnClick event on hint [Loading...]
function cancelLoad(){
	hideBeingLoadedNode();
	//Check whether its top container node
	if (beingLoadedNode.childNodes.length>0){
		beingLoadedNode.childNodes[0].childNodes[0].src = sImagePlus;
	}
	beingLoadedNode = null;	
	return false;
}

//This function handle OnClick event on action image ([+] or [-]). 
//As parameter we pass id of associated node
function actionOnClick(id){
	var oNode = document.getElementById(id);
	
	//If node is already expanded we collapse it
	if (oNode.getAttribute("Expanded")=="true"){
		oNode.childNodes[1].style.display = "none";
		oNode.childNodes[0].childNodes[0].src = sImagePlus;
		oNode.setAttribute ("Expanded", "false")
	}
	//If node is collapsed we expand it
	else{
		//Show minus icon
		oNode.childNodes[0].childNodes[0].src = sImageMinus;		
		//If children nodes are already loaded, just expand it
		if (oNode.getAttribute("LoadedChildren")=="true"){
			oNode.childNodes[1].style.display = "block";
			oNode.setAttribute ("Expanded", "true")
		}
		 //Children nodes are not loaded yet, we need to load it
		else{
			//If the other nodes are loaded at the current time, we should stop it
			if (beingLoadedNode!=null){
				hideBeingLoadedNode();
			}
			//Set new current being loaded node
			beingLoadedNode = oNode;
			//Show loading node
			showBeingLoadedNode();		
			//loading subtree
			processLoading(beingLoadedNode.getAttribute("src"));
		}
	}
}

//These functions handle onMouseOver and onMouseOut events on the links of tree menu 
var sNormalColor = "#000000";
var sOverColor = "red";

function actionOnMouseOver(){
	var oSrc = window.event.srcElement;
	oSrc.style.color = sOverColor ;
	oSrc.style.cursor = "hand";
		
 }

function actionOnMouseOut(){
	var oSrc = window.event.srcElement;
	
	oSrc.style.color = sNormalColor;
	oSrc.style.cursor = "default";

}

//Build tree. This function is called from hidden frame with new downloaded data,
//which are passed in oData parameter

function buildTree(oData){
//Check whether loading was canceled
	if (beingLoadedNode!=null){
		hideBeingLoadedNode();
		//Get list of nodes		
		var oNodes = oData.nodes;
		if (oNodes.length==0){
			if (beingLoadedNode.childNodes.length>0){
				beingLoadedNode.childNodes[0].childNodes[0].src = sImageNone;
				beingLoadedNode.childNodes[0].childNodes[0].onclick = null;
				
			}
		}
		else{
			var oLI, oUL, oIMG, oA, oText, oNOBR, i;
			var sIcon, sName, sHref, sTarget, sSrc, sId;
			
			//Create container for child nodes
			oUL = beingLoadedNode.appendChild(document.createElement("ul"));	
			oUL.style.display="block";	
			//Run over nodes
			
			for (i=0;i<oNodes.length;i++){
				//Set obligatory values
				sIcon = oNodes[i].icon;
				sName =	oNodes[i].name;	

				sId = oNodes[i].id;
				if (sId == cId){
					sName = sName + " »" ;
					}
				
				//Set optional values
				if (oNodes[i].href!= null){
					sHref=oNodes[i].href;			
				}
				else{
					sHref="";					
				}
				if (oNodes[i].target!=null){
					sTarget=oNodes[i].target;
				}
				else{
					sTarget=""
				}
				//Create node
				oLI = oUL.appendChild(document.createElement("li"));	
				//Incerement unique node ID
				nodeId++;
				oLI.id = 'tn' + nodeId;

				//If src attribute is not empty, add custom attribute to the result node
				if (oNodes[i].src!=null){
					sSrc = oNodes[i].src;
				}
				else{
					sSrc = "";
				}
				oLI.setAttribute("src", sSrc);
				oNOBR = oLI.appendChild(document.createElement("nobr"));
				//Create action image [+], [-] [ ]
				oIMG = document.createElement("img");
				oIMG.border = 0;
				//If src attribute is not empty or amount of the child nodes is not equals zero
				if (sSrc!=""){
					//Sub nodes was not loaded and so not expanded
					oLI.setAttribute("LoadedChildren", "false");
					oLI.setAttribute("Expanded", "false");
					oIMG.src = sImagePlus;
					//Set action image event handler - dynamicaly assembly handler for
					//It should be like actionOnClick('tnXXXX')		
					oIMG.onclick = new Function("actionOnClick('tn" + nodeId + "')");	
				}		
				else{
					oIMG.src = sImageNone;
				}
				//Create icon image
				oIMG.width = 16;
				oIMG.height = 16;
				oIMG = oNOBR.appendChild(oIMG);		
				//Creation of icon image
				oIMG = document.createElement("img");
				//oIMG.border = 0;
				//oIMG.src = sImagesPath + sIcon + ".gif";
				//oIMG.width = 16;
				//oIMG.height = 16;		
				//oIMG = oNOBR.appendChild(oIMG);
				//Create link
								
				oA = document.createElement("a");
					oA.onclick = new Function("actionOnClick('tn" + nodeId + "')");
					oA.onmouseover = new Function("actionOnMouseOver()");
					oA.onmouseout =  new Function("actionOnMouseOut()");
				if (sHref!=""){
					//Combine full path from baseHref and relative path
					//If you want to use full pathes (not relative) just
					//assing to baseHref empty string
														
					oA.href = oData.baseHref + sHref;
				}
				if (sTarget!=""){
					oA.target = sTarget;		
				}
				oA.title = sName;		
				oA.className = "treenode";
				oA = oNOBR.appendChild(oA);

				oText = oA.appendChild(document.createTextNode(sName));
				
			}
			//Add custom attributes to resultNode which specifies whether node loaded children or not and 
			//whether is was expanded or not
			beingLoadedNode.setAttribute("Expanded", "true");	
		}
		beingLoadedNode.setAttribute("LoadedChildren", "true");
		beingLoadedNode = null;
	}
	
	if (nID > 0){
		beingLoadedNode = document.getElementById("tn" + nID);
		beingLoadedNode.setAttribute("Expanded", "false");
		actionOnClick("tn" + nID);
		}	

}		
