//*********************************************************************************
// Aurigma Deep Tree 2.0 (c), Fedor Skvortsov, Aurigma Inc. 2001-2003
// Mailto: support@aurigma.com
// WWW: http://www.aurigma.com
//
// This script and its components are free, but must be
// maintained this text and the copyright to use it.
//
//*********************************************************************************
//
//General configuration
//Root Tree
var sRootTree = "struct.php?[var._mpid]";

//Images
var sImagesPath = "images/DeepTree/";
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 = "Loading...";

//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("galleryMenuroot");
	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("span"));
	oA.href = "#";
	oA.title = "Click to cancel..."
	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("Couldn't load subtree.");
	}
}

//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"));
		}
	}
}

//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;
			//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;
				//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"));
				oLI.className = "galleryMenuitem";
				//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 = 13;
				oIMG.height = 13;
				oIMG = oNOBR.appendChild(oIMG);
				//Creation of icon image
				oIMG = document.createElement("img");
				oIMG.border = 0;
				oIMG.src = sImagesPath + sIcon + ".gif";
				oIMG.width = 13;
				oIMG.height = 13;
				if ( sIcon != '' )
					oIMG = oNOBR.appendChild(oIMG);
				//Create link
				oA = document.createElement("a");
				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;
				}
				else
				{
					oA.href = "javascript: actionOnClick('tn" + nodeId + "');";
				}
				if (sTarget!="")
				{
					oA.target = sTarget;
				}
				oA.title = sName;
				oA.className = "menu_links";
				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;
	}
}