// Gestionnaire du contrle
TreeGridCatalogueHandler = 
{
	trees : new Array(),
	xhrStateChangeGetXML : function(treeID_P,refToSee_P) { this.trees[treeID_P].getXmlFromXHR(refToSee_P); },
	toggleTree : function(treeID_P,ref_P) { this.trees[treeID_P].toggleTree(ref_P,true); }
}

// Dfinition du TreeGrid
function TreeGridCatalogue(name_P,
													 width_P,
													 frmRefBdD_P,
													 xmlScriptPath_P)
{
	// attributs
	this.divId = name_P;
	TreeGridCatalogueHandler.trees[this.divId] = this;
	// Id du tableau HTML englobant l'arbre
	this.tableId = 'tree_' + name_P;
	// Largeur du contrle
	this.width = width_P;
	// Champs de formulaire pour le ref BdD
	this.frmRefBdD = frmRefBdD_P;
	// Chemin du flux XML
	this.xmlScriptPath = xmlScriptPath_P;
	
	// Rpertoire des images
	this.imgDirectory = '../../imagesTreeGrid/';
	this.imgWhite = 'white.gif';
	this.imgDot = 'puce.gif';
	this.imgExpand = 'plus.gif';
	this.imgCollapse = 'minus.gif';
	this.imgPDF = 'ico-pdf.gif';
	this.srcImageWhite = this.imgDirectory + this.imgWhite;
	this.srcImageDot = this.imgDirectory + this.imgDot;
	this.srcImageExpand = this.imgDirectory + this.imgExpand;
	this.srcImageCollapse = this.imgDirectory + this.imgCollapse;
	this.srcImagePDF = this.imgDirectory + this.imgPDF;
	
	this.patternNodeId = 'treeNode_';
	
	// Paramtre d'URL supplmentaire pour le flux XML
	this.paramUrlXML = '';
	
	//Code de la langue d'affichage
	this.refLangue = '';
	
	// Paramtre d'URL pour le télchargment du fichier
	this.paramUrlFileDownload = '';
	
	// Message linguistique de chargment
	this.msgLoading = '';
	
	// Message linguistique indiquant que l'arbre est vide
	this.msgNoRessource = '';
	
	// Titre pour le lien de téléchargement du fichier
	this.titleFileDownload = '';
}
	
	
/*
 *
 *	Mthodes prives
 *
 */
 
// Initialisation
function PRV__Initialise_DEF()
{
	// XML permettant d'alimenter l'arbre
	this.xml = null;
	// Code HTML du contrle
	this.strHTML = '';
	// Boolen indique qu'on a cliqu sur le bouton expand/collapse d'un noeud
	this.nodeToggled = false;
	// Boolen indique qu'on a cliqu sur une action par rapport  un noeud
	this.nodeAction = false;
	// Tableau des enttes de colonnes
	this.columnHeadings	= new Array();
	// Tableaux des noeuds issus du fichier XML
	this.xmlNodes	= null;
	// Tableaux des noeuds racines
	this.rootNodes	= new Array();
	// Position du dernier enfant
	this.lastChildPosition = 0;
	// Objet XHR
	this.objXHR = null;
}
TreeGridCatalogue.prototype.PRV__Initialise = PRV__Initialise_DEF;

// Rcupration du flux XML
function PRV__getXML_DEF(refToSee_P)
{
	this.objXHR = VS_getXHR();
 	// Gestion de l'vnement de retour serveur
 	var currentDivId = this.divId;
	this.objXHR.onreadystatechange = function() { TreeGridCatalogueHandler.xhrStateChangeGetXML(currentDivId,
																																															refToSee_P); };
	if(this.paramUrlXML == '') 
	{
		this.objXHR.open('GET',this.xmlScriptPath,true);
		this.objXHR.send(null);
	}
	else 
	{
		this.objXHR.open('POST',this.xmlScriptPath,true);
		this.objXHR.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		this.objXHR.send(this.paramUrlXML);
	}
}
TreeGridCatalogue.prototype.PRV__getXML = PRV__getXML_DEF;
 
// Ajout d'une colonne
function PRV__insertInnerColumn_DEF(description_P, 
																		width_P,
																		align_P)
{
	var nbColumns = this.columnHeadings.length;
	this.columnHeadings[nbColumns]	= new TreeGridColumnCatalogue(description_P, 
																												        width_P,
																												        align_P);
}
TreeGridCatalogue.prototype.PRV__insertInnerColumn = PRV__insertInnerColumn_DEF;

// retourne le code HTML du contrle pour affichage
function PRV__getHTML_DEF()
{
	var NextItemDepth	= 0;
	var CurItemDepth	= 0;
	
	this.PRV__addHTML("<table id='" + this.tableId + "' border='0' cellpadding='2' cellspacing='1' width='" + this.width + "'>");
	
	for(var key in this.rootNodes) this.PRV__getNodeHTML(this.rootNodes[key],0);
	
	this.PRV__addHTML('</table>');
	
	return this.strHTML;
}
TreeGridCatalogue.prototype.PRV__getHTML = PRV__getHTML_DEF;

// Ajout d'un noeud
function PRV__getNodeFromXML_DEF(nodeIndex_P,
																 nodeRef_P,
																 nodeRefParent_P,
																 nodePosition_P,
																 nodeIsEndNode_P,
																 nodeTitle_P,
																 nodeFile_P,
																 nodeFileSize_P)
{
	var nodeTMP = new TreeGridNodeCatalogue(nodeRef_P,
																	 				nodeRefParent_P,
																	 				nodePosition_P,
																	 				nodeIsEndNode_P,
																	 				nodeTitle_P,
																	 				nodeFile_P,
																					nodeFileSize_P);
	this.xmlNodes[nodeIndex_P] = nodeTMP;
	if(nodeTMP.isEndNode == 1) this.nbEndNodes++;
}
TreeGridCatalogue.prototype.PRV__getNodeFromXML = PRV__getNodeFromXML_DEF;

// Arrangement des noeuds pour former l'arbre
function PRV__arrangeNodes_DEF()
{
	for(var key in this.xmlNodes)
	{
		var nodeTMP = this.xmlNodes[key];
		if(nodeTMP.refParent == 0)
		{
			this.rootNodes[nodeTMP.refBdD] = nodeTMP;
			if(nodeTMP.position > this.lastChildPosition)  this.lastChildPosition = nodeTMP.position;
			
		}
		this.PRV__getChildrenNode(nodeTMP);
	}
	this.xmlNodes = null;
}
TreeGridCatalogue.prototype.PRV__arrangeNodes = PRV__arrangeNodes_DEF;

// Retourne un noeud par refBdD donne
function PRV__getChildrenNode_DEF(nodeParent_P)
{
	var nbNodesXML = this.xmlNodes.length;
	for(var key in this.xmlNodes)
	{
		var nodeTEMP = this.xmlNodes[key];
		if(nodeTEMP.refParent == nodeParent_P.refBdD) nodeParent_P.addChildNode(nodeTEMP);
	}
}
TreeGridCatalogue.prototype.PRV__getChildrenNode = PRV__getChildrenNode_DEF;



// Retourne un noeud par refBdD donne
function PRV__getNodeByRef_DEF(ref_P) 
{
	var nodeTofind = null;
	if(this.rootNodes[ref_P] != null) return this.rootNodes[ref_P];
	else 
	{
		for(var key in this.rootNodes) 
		{
			var nodeTMP = this.rootNodes[key];
			nodeTofind = nodeTMP.findChildNode(ref_P);
			if(nodeTofind != null) return nodeTofind;
		}
	}	
}
TreeGridCatalogue.prototype.PRV__getNodeByRef = PRV__getNodeByRef_DEF;

// Alimentation de l'arbre avec le fichier XML
function PRV__feedWithXML_DEF()
{
	this.xmlNodes = new Array();
	this.refLangue = this.xml.getElementsByTagName('reflangue')[0].firstChild.nodeValue;  
	var lignes = this.xml.getElementsByTagName('ligne'); 
	var nbLignes = lignes.length;
	for(var i = 0; i < nbLignes; i++) 
	{
		var ligneXML = lignes[i];
		var refXML = lignes[i].getElementsByTagName('ref')[0].firstChild.nodeValue;
		var refParentXML = lignes[i].getElementsByTagName('refparent')[0].firstChild.nodeValue;
		var positionXML = lignes[i].getElementsByTagName('position')[0].firstChild.nodeValue;
		var isEndNodeXML = lignes[i].getElementsByTagName('isendnode')[0].firstChild.nodeValue;
		var intituleXML = lignes[i].getElementsByTagName('intitule')[0].firstChild.nodeValue;
		var fileXML = null;
		if(lignes[i].getElementsByTagName('fichier')[0].firstChild != null) fileXML = lignes[i].getElementsByTagName('fichier')[0].firstChild.nodeValue;
		var fileSizeXML = null;
		if(lignes[i].getElementsByTagName('size')[0].firstChild != null) fileSizeXML = lignes[i].getElementsByTagName('size')[0].firstChild.nodeValue;
		this.PRV__getNodeFromXML(i,
														 refXML,
														 refParentXML,
														 positionXML,
														 isEndNodeXML,
														 intituleXML,
														 fileXML,
														 fileSizeXML);
	}
	this.PRV__arrangeNodes();
}
TreeGridCatalogue.prototype.PRV__feedWithXML = PRV__feedWithXML_DEF;

	
// Retourne le code HTML pour le dcalage d'un noeud suivant sa profondeur 
function PRV__getHtmlDepth_DEF(depth_P)
{
	var htmlDepth = '';
	for(var i = 0; i < depth_P; i++) htmlDepth += "<img src=" + this.srcImageWhite + ">";
	return htmlDepth;
}
TreeGridCatalogue.prototype.PRV__getHtmlDepth = PRV__getHtmlDepth_DEF;

// Ajout de code HTML pour la gnration de l'arbre
function PRV__addHTML_DEF(strHtml)
{
	this.strHTML += strHtml;
}
TreeGridCatalogue.prototype.PRV__addHTML = PRV__addHTML_DEF;

// Gnration du code pour les contenus de colonnes
function PRV__getColumnsHTML_DEF(node_P)
{
	this.PRV__getColumnTitleHTML(node_P);
}
TreeGridCatalogue.prototype.PRV__getColumnsHTML = PRV__getColumnsHTML_DEF;

// Gnration du code pour le contenu de la colonne "Intitul"
function PRV__getColumnTitleHTML_DEF(node_P)
{
	var urlDownload;
	if(node_P.file != null) urlDownload = this.popUpDownload + '?' + this.frmRefBdD + '=' + node_P.refBdD + '&frm_ref_langue=' + this.refLangue; 
	this.PRV__addHTML('</td><td>');
	if(node_P.childrenNodes == null)
	{
		if(node_P.file != null) 
		{
			this.PRV__addHTML('<p class="rubrique_04"><a href="' + urlDownload + '" title="&gt; ' + this.titleFileDownload + '">' + node_P.title + '</a>&nbsp;<img src="' + this.srcImagePDF + '" width="19" height="20" align="absmiddle">&nbsp;(' + node_P.fileSize + ')</p>');
		}
		else {
		// >>>>>>>>> MODIFICATION DBD STUDIO >>>>>>>>>
			switch (node_P.title) {
				case "Enveloppes Universelles 2010":
				this.PRV__addHTML('<p class="rubrique_04"><a href="http://www.publications-sarel.com/docs/_/cat_EU_2010/" target="blank" style="color:#009530;">' + node_P.title + '</a></p>');
				break;
				case "Enveloppes VDI et électroniques":
				this.PRV__addHTML('<p class="rubrique_04"><a href="http://www.e-catalogue.schneider-electric.fr/navdoc/catalog/ev/big/index.htm" target="blank" style="color:#009530;">' + node_P.title + '</a></p>');
				break;
				default:
				this.PRV__addHTML('<p class="rubrique_04">' + node_P.title + '</p>');
			}
		// <<<<<<<<< MODIFICATION DBD STUDIO <<<<<<<<<
		}
	}
	else
	{
		if(node_P.file != null) 
		{
			this.PRV__addHTML('<h1><a href="' + urlDownload + '" title="&gt; ' + this.titleFileDownload + '">' + node_P.title + '</a>&nbsp;<img src="' + this.srcImagePDF + '" width="19" height="20" align="absmiddle">&nbsp;<span class="normal">(' + node_P.fileSize + ')</span></h1>');
		}
		else this.PRV__addHTML('<h1>' + node_P.title + '</h1>');
	}
	this.PRV__addHTML('</td></tr></table>');	
	this.PRV__addHTML('</td>');
}
TreeGridCatalogue.prototype.PRV__getColumnTitleHTML = PRV__getColumnTitleHTML_DEF;

// Affectation de la visibilit d'un noeud
function PRV__setNodeVisibility_DEF(nodeRef_P, nodeVisible_P) 
{
	var nodeId = this.patternNodeId + nodeRef_P;
	var strDisplay = '';
	if(!nodeVisible_P) strDisplay = 'none';
	document.getElementById(nodeId).style.display = strDisplay;
}
TreeGridCatalogue.prototype.PRV__setNodeVisibility = PRV__setNodeVisibility_DEF;

// Affectation d'une image  un noeud
function PRV__setImage_DEF(nodeRef_P, imgName_P)
{
	var nodeImage = document.getElementById('treeNodeImg_' + nodeRef_P); 
	nodeImage.src = imgName_P;
	if(imgName_P != this.imgWhite ) nodeImage.style.cursor = 'pointer'
}
TreeGridCatalogue.prototype.PRV__setImage = PRV__setImage_DEF;

// Ecrit le code HTML pour un noeud donn
function PRV__getNodeHTML_DEF(node_P,depth_P) 
{
	var nodeId = this.patternNodeId + node_P.refBdD;
	this.PRV__addHTML('<tr id="' + nodeId + '"><td>');
	this.PRV__addHTML("<table border='0' cellpadding='0' cellspacing='0'><tr align='left' valign='top'><td>");
	this.PRV__addHTML(this.PRV__getHtmlDepth(depth_P));
	this.PRV__addHTML('</td><td>');
	var imgId = "treeNodeImg_" + node_P.refBdD;
	this.PRV__addHTML("<img id='" + imgId + "' src='" + this.srcImageDot + "' border='0' align='absmiddle' onClick=javascript:TreeGridCatalogueHandler.toggleTree('" + this.divId + "'," + node_P.refBdD + ")>");		
	this.PRV__addHTML('</td>');
	this.PRV__getColumnsHTML(node_P);
	this.PRV__addHTML('</tr>');	
	for(var key in node_P.childrenNodes) this.PRV__getNodeHTML(node_P.childrenNodes[key],depth_P + 1); 
}
TreeGridCatalogue.prototype.PRV__getNodeHTML = PRV__getNodeHTML_DEF;

// Cache les enfants d'un noeud
function PRV__hideNodeChildren_DEF(node_P) 
{
	if(node_P.childrenNodes != null) 
	{
		for(var key in node_P.childrenNodes) 
		{
			var childrenNodeTMP = node_P.childrenNodes[key];
			this.PRV__setNodeVisibility(key,false);
			if(childrenNodeTMP.childrenNodes != null) 
			{
				this.PRV__setImage(childrenNodeTMP.refBdD,this.srcImageExpand);
				this.PRV__hideNodeChildren(childrenNodeTMP);
			}
		}
	}
}
TreeGridCatalogue.prototype.PRV__hideNodeChildren = PRV__hideNodeChildren_DEF;

// Permet d'afficher l'arbre avec tous les noeuds ferms
function PRV__collapseAll_DEF()
{
	for(var key in this.rootNodes) 
	{
		var nodeTMP = this.rootNodes[key]; 
		nodeTMP.hideChildren = false;
		this.toggleTree(key);
	}
}
TreeGridCatalogue.prototype.PRV__collapseAll = PRV__collapseAll_DEF;

// Ouverture de l'arbre pour montrer un noeud précis
function PRV__seeNode_DEF(ref_P) 
{
	ref_P = parseInt(ref_P);
	var nodeToSee = this.PRV__getNodeByRef(ref_P);
	if(nodeToSee != null) 
	{
		if(nodeToSee.refParent != 0) 
		{
			this.toggleTree(nodeToSee.refParent);
			this.PRV__seeNode(nodeToSee.refParent);
		}
	}
	//this.setRowSelected(ref_P);	
}
TreeGridCatalogue.prototype.PRV__seeNode = PRV__seeNode_DEF;

/*
 *
 *	Méthodes publiques
 *
 */
 
// Ajout d'un paramtre d'URL pour le flux XML
function addParamUrlXML_DEF(paramUrl_P)
{
	this.paramUrlXML = paramUrl_P;
}
TreeGridCatalogue.prototype.addParamUrlXML = addParamUrlXML_DEF;

// Ajout d'un paramtre d'URL pour le flux XML
function setPopUpDownload_DEF(popUp_P)
{
	this.popUpDownload = popUp_P;
}
TreeGridCatalogue.prototype.setPopUpDownload = setPopUpDownload_DEF;

// Affectation du message de chargement
function setMessageLoading_DEF(msgLoading_P)
{
	this.msgLoading = msgLoading_P;
}
TreeGridCatalogue.prototype.setMessageLoading = setMessageLoading_DEF;

// Affectation du message d'arbre vide
function setMessageNoRessource_DEF(msgNoRessource_P)
{
	this.msgNoRessource = msgNoRessource_P;
}
TreeGridCatalogue.prototype.setMessageNoRessource = setMessageNoRessource_DEF;

// Affectation du titre pour le lien de téléchargement du fichier
function setTitleFileDownload_DEF(titleFileDownload_P)
{
	this.titleFileDownload = titleFileDownload_P;
}
TreeGridCatalogue.prototype.setTitleFileDownload = setTitleFileDownload_DEF;


// Permet d'afficher l'arbre 
// Ouvert sur le noeud eventuellement slectionn
function show_DEF(refToSee_P)
{
	// Effacement de l'HMTL du div contenant l'arbre
	document.getElementById(this.divId).innerHTML = '<p class="message_1">' + this.msgLoading + '...</p>';
	this.PRV__Initialise();
	this.PRV__getXML(refToSee_P);
}
TreeGridCatalogue.prototype.show = show_DEF;

// Rcupration du flux XML
function getXmlFromXHR_DEF(refToSee_P)
{
	if(this.objXHR.readyState == 4 && this.objXHR.status == 200)
	{
   	this.xml = this.objXHR.responseXML.documentElement;
		this.objXHR.abort();
		this.objXHR = null;
		// Cration des colonnes
		this.PRV__insertInnerColumn('Intitulé','');
		this.PRV__feedWithXML();
		document.getElementById(this.divId).innerHTML = this.PRV__getHTML();
		if(document.getElementById(this.tableId).innerHTML != '') 
		{
			this.PRV__collapseAll();
			if(typeof(refToSee_P) != 'undefined') this.PRV__seeNode(refToSee_P);	
		}
		else document.getElementById(this.divId).innerHTML = '<p class="message_1">' + this.msgNoRessource + '.</p>';
	} 
}
TreeGridCatalogue.prototype.getXmlFromXHR = getXmlFromXHR_DEF;

// Gestion ouverture / fermeture des noeuds
function toggleTree_DEF(nodeRef_P,nodeClicked_P)
{
	this.nodeToggled = nodeClicked_P;
	var nodeTMP = this.PRV__getNodeByRef(nodeRef_P);
	var nodeImage;
	
	(nodeTMP.hideChildren) ? nodeImage = this.srcImageCollapse : nodeImage = this.srcImageExpand;
	
	
	// Image clique : "plus"
	if(nodeTMP.hideChildren) 
	{
		if(nodeTMP.childrenNodes != null) 
		{
			for(var key in nodeTMP.childrenNodes) this.PRV__setNodeVisibility(nodeTMP.childrenNodes[key].refBdD,true);
			this.PRV__setImage(nodeRef_P,nodeImage);
		}
	// Image clique : "moins"
	} 
	else 
	{
		if(nodeTMP.childrenNodes != null) 
		{
			this.PRV__hideNodeChildren(nodeTMP);
			this.PRV__setImage(nodeRef_P,nodeImage);
		}
	}
	
	nodeTMP.hideChildren = !nodeTMP.hideChildren;
}
TreeGridCatalogue.prototype.toggleTree = toggleTree_DEF;

// Dfintion d'une colonne
function TreeGridColumnCatalogue(title_P,
																 width_P,
																 align_P) 
{
	this.title = title_P;
	this.width = width_P;
	this.align = align_P;
}

// Dfintion d'un noeud
function TreeGridNodeCatalogue(ref_P,
															 refParent_P,
															 position_P,
															 isEndNode_P,
															 title_P,
															 file_P,
															 fileSize_P) 
{
	this.refBdD = ref_P;
	this.refParent = parseInt(refParent_P);
	this.position = parseInt(position_P);
	this.isEndNode = isEndNode_P;
	this.title = title_P;
	this.file = file_P;
	this.fileSize = fileSize_P;
	
	this.hideChildren = true;
	this.childrenNodes = null;
	this.depth = 0;
	this.lastChildPosition = 0;
}

// Ajout d'un noeud enfant
function addChildNode_DEF(node_P) 
{
	if(this.childrenNodes == null) this.childrenNodes = new Array();
	node_P.depth = this.depth + 1;
	if(node_P.position > this.lastChildPosition) this.lastChildPosition = node_P.position;
	this.childrenNodes[node_P.refBdD] = node_P;
}
TreeGridNodeCatalogue.prototype.addChildNode = addChildNode_DEF;

// Retourne un noeud enfant par sa ref si il existe
function findChildNode_DEF(ref_P) 
{
	if(this.refBdD == ref_P) return this;
	if(this.childrenNodes == null) return null;
	else 
	{
		if(this.childrenNodes[ref_P] != null) return this.childrenNodes[ref_P]; 
		else 
		{
			for(var key in this.childrenNodes) 
			{
				childNode = this.childrenNodes[key].findChildNode(ref_P);
				if(childNode != null) return childNode;
			}
		}
	}
}
TreeGridNodeCatalogue.prototype.findChildNode = findChildNode_DEF;

