//Lancement du script
/*
if(typeof Prototype == 'undefined') {
	throw('IdepTool_Menu : Veuillez importer la classe "Prototype"');
}

if(typeof Scriptaculous == 'undefined') {
	throw('IdepTool_Menu : Veuillez importer la classe "Scriptaculous"');
}
*/

Event.observe(window, 'load', menuAccInit);
function menuAccInit(){
	new menuAcc('menuLeft');
}

//Début de la classe

var menuAcc = Class.create();
menuAcc.prototype = {
	
	//Liste des variables
	var_MenuId	 		: false,
	var_ClassEncours	: 'enCours',
	var_ClassTop		: 'menuTop',
	var_Current			: false,
	var_CurrentStart	: false,
	var_iBtn			: 1,
	durationUp			: 0.8,
	durationDown		: 0.4,
	action				: 'click',
	
	//Constructeur
	initialize : function(menu){
		this.var_MenuId = menu;
		
		if(this.var_ClassTop == this.var_MenuId){
			this.action = 'over';
		}
		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){
				var arrayChild = Element.childElements(arrayAncetre[2]);
				for(var i=0;i<arrayChild.length;i++){
					if(arrayChild[i].tagName == 'A' && this.linkAllowOuverture(arrayChild[i])){
						if(this.action == 'click'){
							new Element.hide(arrayAncetre[1]);
							Event.observe(arrayChild[i], 'click' , this.openmenu.bind(this, arrayAncetre[1]) , false);
							arrayChild[i].onclick = function(){ return false; };
						}else{
							//Mouse over !
						}
						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){
			
			this.closemenu();
			
			if(this.var_ClassTop == this.var_MenuId)
				item.style.top = 'auto';
			new Effect.BlindDown(item, {duration : this.durationDown});
			this.var_Current = item;
		}
	},
	
	//Ouverture du menu
	
	closemenu : function(){
		if(this.var_Current){
			new Effect.BlindUp(this.var_Current, {duration : this.durationUp});
			if(this.var_ClassTop == this.var_MenuId)
				this.var_Current.style.top = '-6000px';
			this.var_Current = false;
		}
	},
	
	//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;
		}*/
		if(item.href){
			var temp = item.href.split('rayon_id=')[1];
			if(temp && temp != 'undefined'){
				var temp = temp.split('&')[0];
				if(temp && temp != 'undefined'){
					var page = temp;
				}
			}
		}
		if(window.location.href){
			var temp = window.location.href.split('rayon_id=')[1];
			if(temp && temp != 'undefined'){
				var temp = temp.split('&')[0];
				if(temp && temp != 'undefined'){
					var page2 = temp;
				}
			}
		}
		if(page && page2 && page == page2){
			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(this.var_ClassTop != this.var_MenuId){
				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;
	}

}
