//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', idepTool_MenuInit);
function idepTool_MenuInit(){
	new IdepToolMenu('menuLeft');
	new IdepToolMenu('menuTop');
}

//Début de la classe

var IdepToolMenu = Class.create();
IdepToolMenu.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(item.href == 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);
	}

}