/**
*@addtogroup COMPOSANT
*
*Comp_Menu
*Script permettant de gérer les effets des menus.
*
*@author François Béliveau <fbeliveau@mexup-soft.com>
*@copyright 2006 MEXUP <http://www.mexup-soft.com>
*@license GNU General Public License <http://www.gnu.org/copyleft/gpl.html>
*@version 0.1.0
*/
function Comp_Menu()
{
	//singleton
	obj_Pointeur = this.constructor;
	if(obj_Pointeur.obj_Instance == undefined)
	{
		//propriétés
		this.bool_Compatible = null;
		this.array_Requis = new Array(
			document.getElementById,
			document.getElementsByTagName,
			document.createElement,
			document.createTextNode,
			Bib_Elements.get_ElementsByClassName,
			Bib_Elements.get_Cible,
			Bib_Elements.set_Action,
			Bib_Elements.unset_Action,
			Bib_Elements.exec_StopAction
		);

		this.str_ClasseClick = 'menu_click';
		this.str_ClasseClickActif = 'comp_menu_click';
		this.str_ClasseClickUnique = 'menu_click_unique';
		this.str_ClasseClickUniqueActif = 'comp_menu_click_unique';
		this.str_ClasseRubriqueOuvert = 'comp_menu_ouvert';
		this.str_ClasseRubriqueFermer = 'comp_menu_fermer';
		this.str_ClasseRubriqueActif = 'actif';

		obj_Pointeur.obj_Instance = this;
	}
	else
		return obj_Pointeur.obj_Instance;
}

/*------------------------------------------------------------
METHODES PRIVEES
------------------------------------------------------------*/

/**
*check_Compatible
*Spécifit si le navigateur du client est compatible ou non avec le code de la classe.
*Un système de "cache" est utilisé pour stocker le résultat dans une variable.
*Ce qui permet d'appeler autant de fois que l'on souhaite la fonction sans pour autant refaire le test complètement
*
*@return     boolean
*/
Comp_Menu.prototype.check_Compatible = function()
{
/*
	if(this.bool_Compatible != true && this.bool_Compatible != false)
	{
*/
		this.bool_Compatible = true;
		try
		{
			var int_NbRequis = this.array_Requis.length - 1;
			for(var int_I = 0; int_I<int_NbRequis; int_I++)
			{
				if(!this.array_Requis[int_I])
				{
					this.bool_Compatible = false;
					break;
				}
			}
		}
		catch(obj_Erreur)
		{
			this.bool_Compatible = false;
		}
/*
	}
*/
	return this.bool_Compatible;
};

/**
*exec_AffichageMasquage
*Affichage ou masquage d'un menu.
*
*@param     object[Evenement]
*
*@return     boolean
*/
Comp_Menu.prototype.exec_AffichageMasquage = function(obj_Evenement)
{
	var obj_Cible = Bib_Elements.get_Cible(obj_Evenement);
	var obj_Ul = obj_Cible.parentNode.getElementsByTagName('ul')[0];
	var obj_A = obj_Cible.parentNode.getElementsByTagName('a')[0];

	if(obj_Ul.style.display == 'none')
	{
		obj_A.className = 'comp_menu_ouvert';
		obj_Ul.style.display = 'list-item';
	}
	else
	{
		obj_A.className = 'comp_menu_fermer';
		obj_Ul.style.display = 'none';
	}
	return Bib_Elements.exec_StopAction(obj_Evenement);
};

/**
*exec_AffichageMasquageUnique
*Affichage ou masquage d'un menu en s'assurant qu'un seul des menus soit visible par profondeur.
*
*@param     object[Evenement]
*
*@return     boolean
*/
Comp_Menu.prototype.exec_AffichageMasquageUnique = function(obj_Evenement)
{
	var obj_Cible = Bib_Elements.get_Cible(obj_Evenement);
	var obj_Ul = obj_Cible.parentNode.getElementsByTagName('ul')[0];
	var obj_A = obj_Cible.parentNode.getElementsByTagName('a')[0];

	if(obj_Ul.style.display == 'none')
	{
		obj_A.className = 'comp_menu_ouvert';
		obj_Ul.style.display = 'list-item';
	}
	else
	{
		obj_A.className = 'comp_menu_fermer';
		obj_Ul.style.display = 'none';
	}

	var obj_UlTemp = obj_Cible;
	while(obj_UlTemp.nodeName.toLowerCase() != 'ul' && obj_UlTemp)
	{
		obj_UlTemp = obj_UlTemp.parentNode;
	}

	var array_Ul = obj_UlTemp.getElementsByTagName('ul');
	var int_NbUl = array_Ul.length - 1;
	for(int_NbUl; int_NbUl >= 0; int_NbUl--)
	{
		if(array_Ul[int_NbUl] != obj_Ul)
		{
			if(array_Ul[int_NbUl].parentNode.nodeName.toLowerCase() == 'li')
				array_Ul[int_NbUl].parentNode.getElementsByTagName('a')[0].className = 'comp_menu_fermer';
			array_Ul[int_NbUl].style.display = 'none';
		}
	}
	return Bib_Elements.exec_StopAction(obj_Evenement);
};

/*------------------------------------------------------------
METHODES PUBLIQUES
------------------------------------------------------------*/

/**
*exec_Initialisation
*Execute l'initialisation d'un menu.
*
*@param     object[element HTML]
*@param     string
*
*@return     void
*/
Comp_Menu.prototype.exec_Initialisation = function(obj_Menu, str_Type)
{
	if(!this.check_Compatible())
		return;

	if(!obj_Menu)
		return;

	var array_Ul = obj_Menu.getElementsByTagName('ul');
	var int_NbUl = array_Ul.length - 1;
	for(int_NbUl; int_NbUl >= 0; int_NbUl--)
	{
		var obj_LiTemp = array_Ul[int_NbUl].parentNode;
		if(!obj_LiTemp)
			continue;

		if(obj_LiTemp.nodeName.toLowerCase() != 'li')
			continue

		var str_Libelle = obj_LiTemp.firstChild.nodeValue;
		if(str_Libelle == '' || str_Libelle == null)
			continue;

		//masquage par défaut
		array_Ul[int_NbUl].style.display = 'none';

		//création des liens "fictif" pour y associer les évenements (on pourrait faire sans lien, mais cela rendrait la rubrique inacessible au clavier)
		var obj_ATemp = document.createElement('a');
		var obj_LibelleA = document.createTextNode(str_Libelle);
		obj_ATemp.setAttribute('href', '#ouverture-fermeture-Menu');
		obj_ATemp.setAttribute('class', this.str_ClasseRubriqueFermer);
		obj_ATemp.appendChild(obj_LibelleA);
		obj_LiTemp.replaceChild(obj_ATemp, obj_LiTemp.firstChild);

		//affectation des évenements
		if(str_Type == this.str_ClasseClick)
			Bib_Elements.set_Action(obj_ATemp, 'click', this.exec_AffichageMasquage, false);
		else if(str_Type == this.str_ClasseClickUnique)
			Bib_Elements.set_Action(obj_ATemp, 'click', this.exec_AffichageMasquageUnique, false);
	}

	//on rend visible l'item actif
	if(str_Type == this.str_ClasseClick || str_Type == this.str_ClasseClickUnique)
	{
		var array_Menu = Bib_Elements.get_ElementsByClassName(obj_Menu, 'li', this.str_ClasseRubriqueActif);
		var int_NbMenu = array_Menu.length;
		if(int_NbMenu>0)
		{
			var obj_Parent = array_Menu[0].parentNode;
			while(obj_Parent != obj_Menu)
			{
				if(obj_Parent.nodeName.toLowerCase() == 'ul')
				{
					obj_Parent.style.display = 'list-item';

					var obj_A = obj_Parent.parentNode.getElementsByTagName('a')[0]
					obj_A.className = this.str_ClasseRubriqueOuvert;
				}

				obj_Parent = obj_Parent.parentNode;
			}
		}
	}

	if(str_Type == this.str_ClasseClick)
		obj_Menu.className = this.str_ClasseClickActif;
	else if(str_Type == this.str_ClasseClickUnique)
		obj_Menu.className = this.str_ClasseClickUniqueActif;
};

/**
*exec_InitialisationGlobale
*Execute l'initialisation des menus du document.
*
*@return     void
*/
Comp_Menu.prototype.exec_InitialisationGlobale = function()
{
	if(!this.check_Compatible())
		return;

	var array_Menu = Bib_Elements.get_ElementsByClassName(document, 'ul', [this.str_ClasseClickUnique, this.str_ClasseClick]);
	var int_NbMenu = array_Menu.length;

	for(var int_I=0; int_I<int_NbMenu; int_I++)
	{
		this.exec_Initialisation(array_Menu[int_I], array_Menu[int_I].className);
	}
};

if(Mxp_Init.set_NouvelleFonction)Mxp_Init.set_NouvelleFonction(function(){var obj_Temp = new Comp_Menu();obj_Temp.exec_InitialisationGlobale();});
