/**
 * Classe : NestleDHTMLNav
 * Projet : Nestle
 * Auteur :  Thibault LH.
 * CP     : Eric L.
 */

function NestleDHTMLNav(s_runTimePath){
	/**
	 * Cette classe représente l'objet que l'on utilisera dans la page HTML comme interface.
	 * Cette classe propose une méthode init qu'il faut appeler une fois que la structure est construite.
	 * Cette classe hérite de la classe NestleBranch, c'est cette dernière qui propose les méthodes d'ajout de branches
	 * Une branche sans fils, est en fait un lien.
	 */

	//ND Ajout de la propriete au niveau de l'objet Navigation
	this.m_selectedBranchID="";
	this.setSelectedID      = new Function("selectedID", "this.m_selectedBranchID = selectedID;");
	this.getSelectedID      = new Function("return this.m_selectedBranchID;");
	//FIN ND
	
	if(NestleDHTMLNav.arguments.length != NestleDHTMLNav.length) {alert("Method NestleDHTMLNav : "+NestleDHTMLNav.arguments.length+" parameters were provided, when "+NestleDHTMLNav.length+" are attempted");return false;}
	// METHODES
	this.draw = NestleDHTMLNavDraw;
	
	//NestleBranch.call(this, s_runTimePath, null, 'root_id', 'root_label', 0); // Heritage	
	this.m_runTimePath  = s_runTimePath;
	
	
	 //function NestleBranch(s_runTimePath, o_parent, s_branchId, s_branchLabel, i_branchLevel, s_branchLink){
	
	
		this.m_parent       = null;
		this.m_branchs      = new Array();
		this.m_isSelected   = false;
		this.m_isLast       = false;
		this.m_cssLink        = "";
		this.m_cssContainer   = "";
		
		this.m_id    = 'root_id';
		this.m_label = 'root_label';
		this.m_level = 0;
		this.m_link  = null;//s_branchLink;
	
		// METHODES
		this.getRunTimePath = new Function("return this.m_runTimePath;");
		this.getId          = new Function("return this.m_id;");
		this.getLabel       = new Function("return this.m_label;");
		this.getLevel       = new Function("return this.m_level;");
		this.getLink        = new Function("return this.m_link;");
	
		this.addBranch       = NestleBranchAddBranch;
		this.init            = NestleBranchInit;
		this.getHTML         = NestleBranchGetHTML;
		this.setLast         = new Function("this.m_isLast = true;");
		this.isLast          = new Function("return this.m_isLast;");
		this.getParent       = new Function("return this.m_parent;");
		this.hasChildren     = new Function("return this.m_branchs.length > 0;");
		this.hasSelectedChild    = NestleBranchHasSelectedChild;
		this.setSelected     = NestleBranchSetSelected;
		this.isSelected      = new Function("return this.m_isSelected;");
		this.setCssLink      = new Function("cssName", "this.m_cssLink = cssName;");
		this.getCssLink      = new Function("return this.m_cssLink;");
		this.setCssContainer = new Function("cssName", "this.m_cssContainer = cssName;");
		this.getCssContainer = new Function("return this.m_cssContainer;");
		this.rollCssContainer= NestleBranchRollCssContainer;
		this.closeBranch     = NestleBranchCloseBranch;
		this.openBranch      = NestleBranchOpenBranch;
		this.getNodeById     = NestleChannelGetNodeById;
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
NestleDHTMLNav.prototype = NestleBranch;


function NestleDHTMLNavDraw(o_DomContainer){
	/**
	 *
	 */	
	
	var s_html = "<ul>";
	for(var i = 0 ; i < this.m_branchs.length ; i++) {
		s_html+=this.m_branchs[i].getHTML();
	}
	s_html+= "</ul>";
	if(o_DomContainer) {
		o_DomContainer.innerHTML = s_html;	
	}
	else
		alert("Méthode NestleDHTMLNavDraw : Paramètre o_DomContainer est null");
}


// ========================================================================================================================================================
/**
 * Classe : NestleBranch
 * Projet : Nestle
 * Auteur : Thibault LH.
 * CP     : Eric L.
 */
 function NestleBranch(s_runTimePath, o_parent, s_branchId, s_branchLabel, i_branchLevel, s_branchLink){
	 /**
	  * Cette classe permet de représenter un objet de la navigation. C'est soit un conteneur de liens (s'il possède des enfants), soit un lien (s'il ne possède pas d'enfants).
	  */

	// CHAMPS	
	this.m_runTimePath  = s_runTimePath;

	this.m_parent       = o_parent;
	this.m_branchs      = new Array();
	this.m_isSelected   = false;
	this.m_isLast       = false;
	this.m_cssLink        = "";
	this.m_cssContainer   = "";
	
	this.m_id    = s_branchId;
	this.m_label = s_branchLabel;
	this.m_level = i_branchLevel;
	this.m_link  = s_branchLink;

	// METHODES
	this.getRunTimePath = new Function("return this.m_runTimePath;");
	this.getId          = new Function("return this.m_id;");
	this.getLabel       = new Function("return this.m_label;");
	this.getLevel       = new Function("return this.m_level;");
	this.getLink        = new Function("return this.m_link;");

	this.addBranch       = NestleBranchAddBranch;
	this.init            = NestleBranchInit;
	this.getHTML         = NestleBranchGetHTML;
	this.setLast         = new Function("this.m_isLast = true;");
	this.isLast          = new Function("return this.m_isLast;");
	this.getParent       = new Function("return this.m_parent;");
	this.hasChildren     = new Function("return this.m_branchs.length > 0;");
	this.hasSelectedChild    = NestleBranchHasSelectedChild;
	this.setSelected     = NestleBranchSetSelected;
	this.isSelected      = new Function("return this.m_isSelected;");
	this.setCssLink      = new Function("cssName", "this.m_cssLink = cssName;");
	this.getCssLink      = new Function("return this.m_cssLink;");
	this.setCssContainer = new Function("cssName", "this.m_cssContainer = cssName;");
	this.getCssContainer = new Function("return this.m_cssContainer;");
	this.rollCssContainer= NestleBranchRollCssContainer;
	this.closeBranch     = NestleBranchCloseBranch;
	this.openBranch      = NestleBranchOpenBranch;
	this.getNodeById     = NestleChannelGetNodeById;


}

function NestleBranchAddBranch(s_branchId, s_branchLabel, s_branchLink){
	/**
	 * Cette méthod crée un nouvel objet NestleBranch.
	 * branchLink est optionnel et ne sera utilisé que si le noeud courant n'a pas d'enfants.
	 * Retourne l'objet crée.
	 */
	var  o = new NestleBranch(this.getRunTimePath()+'.m_branchs['+this.m_branchs.length+']', this.getRunTimePath(), s_branchId, s_branchLabel, this.m_level + 1, s_branchLink);
	this.m_branchs[this.m_branchs.length] = o;
	return o;
}
function NestleBranchInit(){
	/**
	 * Cette méthode permet à chaque objet branche de s'initialiser.
	 * 1 -> La propriété isLast du dernier enfant de l'élément courant est mise à true.
	 * 2 -> Si l'élément courant est de niveau 1 alors on définit la css du lien et du UL
	 * 3 -> Si l'élément courant est de niveau 2 alors on définit la css du lien
	 * 4 -> Si l'élément courant possède des enfants, on les initialise aussi en appelant leur méthode init();
	 */

	if(this.m_branchs[this.m_branchs.length-1])
		this.m_branchs[this.m_branchs.length-1].setLast();

	if(this.getLevel()==1) 	{
		//long traitement tests niveau1
		if (this.hasChildren())
		{
			if (this.isLast())
			{
				if (this.hasSelectedChild())
				{
					// Avec Enfant, Dernier Selectionné (ouvert)
					this.setCssLink ("LNopenedBranch");
					this.setCssContainer("LNopenedLeefContainer");
				}
				else
				{
					// Avec Enfant, Dernier NON Selectionné (fermé)
					this.setCssLink ("LNclosedLastBranch");
					this.setCssContainer("LNclosedLeefContainer");
				}
			}
			else
			{
				if (this.hasSelectedChild())
				{
					// Avec Enfant, NON Dernier Selectionné (ouvert)
					this.setCssLink ("LNopenedBranch");
					this.setCssContainer("LNopenedLeefContainer");
				}
				else
				{
					// Avec Enfant, NON Dernier NON Selectionné (fermé)
					this.setCssLink ("LNclosedBranch");
					this.setCssContainer("LNclosedLeefContainer");
				}
			}
		}
		// SANS ENFANT
		else
		{
			if (this.isLast())
			{
					// SANS Enfant, Dernier 
					this.setCssLink ("LNlinkLastBranch");
			}
			else
			{
					//SANS ENFANT NON DERINER
					this.setCssLink ("LNlinkBranch");
			}

		}
	}
	else
	{
		// element de second niveau				
		//ND Modifs pour utiliser la pptes de la NAV			
		if(eval(this.getParent()) != null)
		{					
			var s_SelectedID= (eval(eval(this.getParent()).getParent()).getSelectedID());														

		}																							
		if (this.m_id==s_SelectedID)
		//Fin ND
		{
			this.setCssLink("LNhighlightedLeef");
		}
		else
		{
			this.setCssLink("LNstandardLeef");
		}
	}
	if(this.hasChildren()) {
		for(var i = 0 ; i < this.m_branchs.length ; i++) 
			this.m_branchs[i].init();
	}
}

function NestleBranchGetHTML(){
	/**
	 * Cette méthode retourne le code HTML nécessaire pour visualiser l'élément courant.
	 * Si l'élément courant possède des enfants, l'élément courant appelle leur méthode getHTML() pour les rendre visuellement
	 */
	var s_html = '<li><a id="a_'+this.getId()+'" onFocus="blur()" '; // Ouverture de l'élément courant, qui contient toujours un A
	
	if(this.hasChildren())
		s_html+='href="javascript:'+this.getRunTimePath()+'.rollCssContainer(this)"'; // Si l'élément courant a des enfants, on lui affecte un onlick qui permettra de déplier ou replier les enfants
	else
		s_html+='href="'+this.getLink()+'"'; // Si l'élément courant n'a pas d'enfants, c'est un lien on affecte son href

	s_html+=' class="'+this.getCssLink()+'"'; // Affectation de la classe CSS (définie dans le init) pour le lien, et fermeture du lien.

	s_html+='>'+this.getLabel()+"</a>"; // Texte du lien

	if(this.hasChildren()) { // si l'élément courant a des enfants ...
		s_html+='<ul id="ul_'+this.getId()+'" class="'+this.getCssContainer()+'">'; // ... on crée un container UL d'enfants, affectation de la classe CSS (définie lors du init) ...
		for(var i = 0 ; i < this.m_branchs.length ; i++)  // ... pour chaque enfant ...
			s_html+=this.m_branchs[i].getHTML(); // ...on lui demande de renvoyer son code HTML
		s_html+='</ul>'; // fermeture du container d'enfants
	}

	s_html+='</li>'; // fermeture de l'élément courant
	return s_html;
}
function NestleBranchHasSelectedChild(){
	/**
	 * Si l'élément courant possède un enfant sélectionné, renvoie true, sinon false
	 */
	//ND Modifs pour utiliser la pptes de la NAV	 
	var b_hasSelectedChild = false;
	if(this.hasChildren()) 
	{	
		if(eval(this.getParent()) != null)
		{
			var tmp = (eval(this.getParent()));
			var s_SelectedID= eval(tmp.getRunTimePath()).getSelectedID();						
			for(var i = 0 ; i < this.m_branchs.length ; i++)
			{																			
					if(this.m_branchs[i].m_id==s_SelectedID)
					{	
						b_hasSelectedChild = true;
					}				
			}
		}
	}
	return b_hasSelectedChild;
}
function NestleBranchSetSelected(){
	/**
	 * Cette méthode met la propriété m_isSelected  de l'élément courant à true.
	 * Par ailleurs, elle indique au container du présent élément qu'il est lui aussi sélectionné.
	 */
	// Le menu courant est selectionné
	this.m_isSelected = true;
	
	
	//ND méthode fonctionne bien on a bien le setSelected qui fonctionne
	// On conserve en mémoire (lastOpenedMenu) le chemin pour accéder au parent, pour pouvoir le refermer
	if(eval(this.getParent()) != null) 
	{
		var tmp = eval(this.getParent());
		lastOpenedMenu = tmp.getRunTimePath();
	}
}

var lastOpenedMenu = null;

function NestleBranchRollCssContainer(linkObject){
	/**
	 * Cette méthode est appelée lors des clics sur les branches.
	 * Si un précédent menu est ouvert, elle le ferme.
	 * Selon l'état ouvert ou fermé de l'élément courant, elle appelle la méthode close ou open de l'élément courant.
	 */

	if((lastOpenedMenu != null) && (this.getRunTimePath() != lastOpenedMenu))  {
		eval(lastOpenedMenu+'.closeBranch()')
	}

	if(document.getElementById("ul_"+this.getId()).className == "LNclosedLeefContainer")
		this.openBranch();
	else
		this.closeBranch();

}

function NestleBranchCloseBranch(){
	/**
	 * Cette méthode ferme la branche de l'élément courant :
	 * En agissant sur le A, pour que la flèche passe de ouverte à fermer
	 * En agissant sur le UL, pour lui indiquer qu'il est fermé, et que ses enfants doivent se cacher
	 */
	if(document.getElementById("a_"+this.getId()))
		document.getElementById("a_"+this.getId()).className = "LNclosedBranch";
	else
		alert("close => "+"a_"+this.getId()+"not exist")

	if(document.getElementById("ul_"+this.getId())) {
		document.getElementById("ul_"+this.getId()).className = "LNclosedLeefContainer";
		lastOpenedMenu = null;
	}
	else
		alert("close => "+"ul_"+this.getId()+"not exist")
}
function NestleBranchOpenBranch(){
	/**
	 * Cette méthode ferme la branche de l'élément courant :
	 * En agissant sur le A, pour que la flèche passe de ouverte à ouvrir
	 * En agissant sur le UL, pour lui indiquer qu'il est ouvert, et que ses enfants doivent s'ouvrir
	 */
	if(document.getElementById("a_"+this.getId()))
		document.getElementById("a_"+this.getId()).className = "LNopenedBranch";
	else
		alert("open => "+"a_"+this.getId()+"not exist")

	if(document.getElementById("ul_"+this.getId())) {
		document.getElementById("ul_"+this.getId()).className = "LNopenedLeefContainer";
		lastOpenedMenu = this.getRunTimePath();
	}
	else
		alert("open => "+"ul_"+this.getId()+"not exist")
}

function NestleChannelGetNodeById(s_id){

	if(this.getId() == s_id)
		return this;
		
	for(var i = 0 ; i < this.m_branchs.length ; i++) {
		if(this.m_branchs[i].getId() == s_id)
			return this.m_branchs[i];
		if(this.m_branchs[i].hasChildren()) 
			if(this.m_branchs[i].getNodeById(s_id) != null)
				return this.m_branchs[i].getNodeById(s_id);
	}
	return null;
}