// TreeView Script
// (c) 2004-2005, El Camino Real - Yannick Gabard.
//--------------------------------------------------------------
// File: treeview.js
// Revision: 1.1
//--------------------------------------------------------------
// Auteur          : Yannick Gabard (yannick.gabard@el-camino-real.org)
//--------------------------------------------------------------

// Credits : Adaptation du script Aurigma DeepTree

//Cache images
var oImg1 = new Image(); 
oImg1.src = "img/treeview/icon-item.gif"; 
var oImg2 = new Image(); 
oImg2.src = "img/treeview/icon-folder.gif"; 
var oImg3 = new Image(); 
oImg3.src = "img/treeview/node-none.gif"; 
var oImg4 = new Image(); 
oImg4.src = "img/treeview/node-plus.gif"; 
var oImg5 = new Image(); 
oImg5.src = "img/treeview/node-minus.gif"; 

//Images
var sImagesPath = "img/treeview/";
var sImageNone = sImagesPath + "node-none.gif";
var sImagePlus = sImagesPath + "node-plus.gif";
var sImageMinus = sImagesPath + "node-minus.gif";

//Styles
var sNodeLoadingClass = "NodeLoading";
var sNodeClass = "Node";

// Localisation
var sLoading = "Chargement en cours...";
var sLoadingTimeOut = "Impossible de charger l'arbre";

//Timeout during which node will be loaded
var timeOut = 5000;

//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(URLRootTree){
	oSelNode = null;
	//set beingLoadedNode to the tree container
	beingLoadedNode = document.getElementById("Tree");
	processLoading(URLRootTree);		
}

//Load subtree into hidden frame
function processLoading(sTreeURL){	
	window.tocLoader.location.href = sTreeURL;
	//window.frames["tocLoader"].document.location.href = sTreeURL;
	if (window.frames["tocLoader"].document!=null) {
    window.frames["tocLoader"].document.location.href = sTreeURL; }
  else {
   var a
   a = document.getElementById("tocLoader");
   a.location.href = sTreeURL;
	 //window.frames["main"].document.frames["tocLoader"].document.location.href = sTreeURL;
  };
	
	//window.tocLoader.location.replace(sTreeURL);
	
	//frames['tocLoader'].location.href = 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(sTitle){
	var oDIV = beingLoadedNode.appendChild(document.createElement("div"));
	oDIV.className = sNodeLoadingClass;
	var oA = oDIV.appendChild(document.createElement("a"));
	oA.href = "#";
	// oA.title = "Click to cancel..."
	oA.title = beingLoadedNode.getAttribute("src");
	oA.onclick = cancelLoad;	
	var	oText = oA.appendChild(document.createTextNode(sLoading));
}

//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(sLoadingTimeOut);			
	}
}

//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(beingLoadedNode.getAttribute("src"));		
			//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(oNodes){
	//Check whether loading was canceled
	if (beingLoadedNode!=null){
		hideBeingLoadedNode();
		//Get list of nodes				
		buildSubtree(oNodes, beingLoadedNode)
		beingLoadedNode = null;
	}
}		

function showAlert(){
	alert(777);
}

function buildSubtree(oNodes, oContainer, show){
	if (oNodes.length==0){
		if (oContainer.childNodes.length>0){
			oContainer.childNodes[0].childNodes[0].src = sImageNone;
			oContainer.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
		
		var iDepth = 0; 
		var oParent = oContainer.parentNode;
		while(oParent){
			oParent = oParent.parentNode;
			iDepth++;
		}
		
		oUL = oContainer.appendChild(document.createElement("div"));			
		oUL.style.borderTopWidth = "0px;"			
		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("div"));	
			//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"));
			oNOBR.style.display = "block";	
			oNOBR.style.marginLeft = iDepth * 8 - 16 + "px";
			//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!="") || (oNodes[i].children)){
				//Sub nodes was not loaded and so not expanded
				oLI.setAttribute("LoadedChildren", "false");
				//Check is exists subtree
				if (oNodes[i].children){
					oLI.setAttribute("Expanded", "true");
					oIMG.src = sImageMinus;
				}
				else{
					oLI.setAttribute("Expanded", "false");
					oIMG.src = sImagePlus;
				}
				//Set action image event handler - dynamicaly assembly handler for
				//It should be like actionOnClick('tnXXXX')	
				//patch for konqueror	
				oIMG.onclick = new Function("if (typeof(actionOnClick)!='undefined') {window.actionOnClick('tn" + nodeId + "');} else {window.parent.actionOnClick('tn" + nodeId + "');}");
			}		
			else{
				oIMG.src = sImageNone;
				oIMG.alt = "";
			}
			//Create icon image
			oIMG.width = 13;
			oIMG.height = 16;
			oIMG = oNOBR.appendChild(oIMG);		
			//Creation of icon image
			oIMG = document.createElement("img");
			oIMG.border = 0;
			oIMG.src = sImagesPath + "icon-"+ sIcon + ".gif";
			//oIMG.alt = "";	
			oIMG.width = 16;
			oIMG.height = 16;		
			oIMG = oNOBR.appendChild(oIMG);
			//Create link
			if (sHref!=""){
				oA = document.createElement("a");
				oA.title = sName;		
				//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 = "/Support/" + sHref;
					oA.href = sHref;
				//}
				if (sTarget!=""){
					oA.target = sTarget;		
				}
				// YG - Begin
				// Ajout du lien sur les libellés des noeuds
				if ((sSrc!="") || (oNodes[i].children)) {
					oA.onclick = new Function("if (typeof(actionOnClick)!='undefined') {window.actionOnClick('tn" + nodeId + "');} else {window.parent.actionOnClick('tn" + nodeId + "');}");
				}	
				// YG -End
			}
			else{

				// YG
				// Il n'y a pas de document associé
				oA = document.createElement("span");
				oA.title = sName;
				// oA.style.cursor = "hand";
  			oA.onclick = new Function("if (typeof(actionOnClick)!='undefined') {window.actionOnClick('tn" + nodeId + "');} else {window.parent.actionOnClick('tn" + nodeId + "');}");
				
			}
			//oA.title = sName;		
			if (oNodes[i].selected){		
				oA.className = "TreeNodeSel";
			}
			else{
				oA.className = "TreeNode";
			}						
			oA = oNOBR.appendChild(oA);
			oText = oA.appendChild(document.createTextNode(sName));
			
			//Check is exists subtree and process for subnodes
			if (oNodes[i].children){
				buildSubtree(oNodes[i].children, oLI);

				// YG 
				// Referme le niveau recemment chargé
				actionOnClick(oLI.id)
			}			
		}
		//Add custom attributes to resultNode which specifies whether node loaded children or not and 
		//whether is was expanded or not
		oContainer.setAttribute("Expanded", "true");	
	}
	oContainer.setAttribute("LoadedChildren", "true")	

}

//Sync toc		
function tocSync(){
	var p = window.main.location.pathname;
	var s = window.main.location.search;
	if (s!=''){
		p = p + '%3F' + s.substr(1, p.length-2);}
	window.content.location = "/toc.html" + p;
	return false;
}		

function tocHide(){
	document.getElementById('tocPanel').style.display = "none";
	document.getElementById('tocShowButton').style.display = "block";	
	return false;
}

function tocShow(){
	document.getElementById('tocPanel').style.display = "block";
	document.getElementById('tocShowButton').style.display = "none";	
	return false;
}

//Node class
function node(name, href, icon, target, src, selected, children){
	this.name = name;
	this.href = href;
	this.icon = icon;
	this.target = target;
	this.src = src;
	this.selected = selected;
	this.children = children;
}


