//Lancement du script



if(typeof Prototype == 'undefined') {

	throw('Tool_Menu : Veuillez importer la classe "Prototype"');

}



if(typeof Scriptaculous == 'undefined') {

	throw('Tool_Menu : Veuillez importer la classe "Scriptaculous"');

}



Event.observe(window, 'load', Tool_MenuInit);

function Tool_MenuInit(){

	new ToolMenu('menuLeft');

	new ToolMenu('menuTop');

}



//Début de la classe



var ToolMenu = Class.create();

ToolMenu.prototype = {

	

	//Liste des variables

	var_MenuId	 		: false,

	var_ClassEncours	: 'rubrique_active',

	var_ClassTop		: 'menuTop',

	var_Current			: false,

	var_CurrentStart	: false,

	var_iBtn			: 1,

	durationUp			: 0.8,

	durationDown		: 0.4,

	

	//Constructeur

	initialize : function(menu){

		this.var_MenuId = menu;

		

		var arrayElt = $$('#' + this.var_MenuId + ' li a');

		if(arrayElt.length){

			arrayElt.each(

				this.initMenu.bind(this)

			);

			

			this.initCurrentMenu();

		}

	},

	

	//Initialisation des liens

	

	initMenu : function(item, index){

		var arrayAncetre = Element.ancestors(item);

		if(!arrayAncetre[1].hasAttribute('id') || arrayAncetre[1].getAttribute('id')!= this.var_MenuId){

			if(parseInt(arrayAncetre[1].style.top) != '-6000' || this.var_MenuId != this.var_ClassTop){

				new Element.hide(arrayAncetre[1]);

				

				var arrayChild = Element.childElements(arrayAncetre[2]);

				for(var i=0;i<arrayChild.length;i++){

					if(arrayChild[i].tagName == 'A' && this.linkAllowOuverture(arrayChild[i])){

						Event.observe(arrayChild[i], 'click' , this.openmenu.bind(this, arrayAncetre[1]) , false);

						arrayChild[i].onclick = function(){ return false; };

						break;

					}

				}

			}

		}else{

			arrayAncetre[0].className = 'bt' + this.var_iBtn++;	

		}

		

		this.selectCurrentRubrique(item);

	},

	

	//On regarde si le lien doit etre activé ou non (ou si déjà activé !)

	

	linkAllowOuverture : function(item){

		if(this.substr(item.href, -1) == '#' || item.href == window.location.href){

			return true;

		}else{

			return false;

		}

	},

	

	//Ouverture du menu

	

	openmenu : function(item){

		if(item != this.var_Current){

			if(this.var_Current){

				new Effect.BlindUp(this.var_Current, {duration : this.durationUp});

			}

			new Effect.BlindDown(item, {duration : this.durationDown});

			this.var_Current = item;

		}

	},

	

	//On récupere la rubrique courante

	

	selectCurrentRubrique : function(item){

		if(this.pagin_replace(item.href) == this.pagin_replace(window.location.href)){

			this.var_CurrentStart = item;

		}

	},

	

	//Ouverture du menu courant

	

	initCurrentMenu : function(){

		if(this.var_CurrentStart){

			var arrayAncetre = Element.ancestors(this.var_CurrentStart);

			

			Element.addClassName(this.var_CurrentStart, this.var_ClassEncours);

			

			if(!arrayAncetre[1].hasAttribute('id') || arrayAncetre[1].getAttribute('id')!= this.var_MenuId){

				this.openmenu(arrayAncetre[1]);

			}else{

				var arrayChild = Element.childElements(arrayAncetre[0]);

				for(var i=0;i<arrayChild.length;i++){

					if(arrayChild[i].tagName == 'UL'){

						this.openmenu(arrayChild[i]);

						break;

					}

				}

			}

		}

	},

	

	//Fonction PHP.js

	

	substr : function (f_string, f_start, f_length){

	 

		f_string += '';

	 

		if(f_start < 0){

			f_start += f_string.length;

		}

		if(f_length == undefined){

			f_length = f_string.length;

		}else if(f_length < 0){

			f_length += f_string.length;

		}else {

			f_length += f_start;

		}

		if(f_length < f_start) {

			f_length = f_start;

		}

		return f_string.substring(f_start, f_length);

	},

	

	preg_replace : function (string_pattern, string_pattern_replace, my_string)  {

		var new_string = String (my_string);

		var reg_exp= RegExp(string_pattern, "gi");

		return new_string.replace (reg_exp, string_pattern_replace);

	},

	

	pagin_replace : function (texte){

		var reg = '&RSpage=[0-9]*';

		texte = this.preg_replace(reg, '', texte);

		var reg = '-[0-9]\.htm';

		texte = this.preg_replace(reg, '.htm', texte);

		return texte;

	}



}



 
