/**
 *
 * @access public
 * 
 * @return void
 **/
var MenuPrincipal = new Class ({
	
	initialize: function () {
		this.liste = $('menu_principal').getChildren('div.menu_principal_gauche').getElement('ul')[0].getChildren('li');
		this.liste.each(function(item, key) {
			this.liste[key].desactive = true;
			// s'il y a un sous menu
			if (item.getElement('ul') != null) {
				this.liste[key].effect = new Fx.Tween(this.liste[key].getElement('ul'),{
														property:'opacity',
														duration: 250
														});
				this.liste[key].effect.set(0);
				this.liste[key].effect.cancel();
				item.getElement('ul').setStyle('display', 'block');
				
				item.getElement('ul').addEvent('mouseover',this.mouseOver.bind(this));
				item.getElement('ul').addEvent('mouseout',this.mouseOut.bind(this));
			}			
		}, this);
		
		this.boutons = [];
		this.liste.each(function(item, key) {
			this.boutons[key] = item.getElement('a');/*.addEvent('mouseenter', function () {
				this.active(item.id);
			}));
			var i = 0;*/
		}, this);
		
		this.boutons.each(function(button) {
			button.addEvent('mouseover',this.active.bind(this));
		   }.bind(this)
		);
		$('content').addEvent('mouseover', this.desactive.bind(this));
		
		this.etat = "desactive";
		this.time = 0;
		
	},
	active: function (id) {
		// on reçoit l'événement
		var event = new Event(id);
		// on sélectionne l'élément appelant
		var clicked = event.target;
		
		this.etat = "active";
		this.time = 0;
		
		var parent = clicked.getParent();
		this.liste.each(function(item, key) {
			if (item.id != parent.id) {
				// on vérifie qu'il y a un sous-menu
				if (item.getElement('ul')) {
					this.liste[key].desactive = true;
					this.liste[key].effect.start(0);
				}
			}
			else if (item.id == parent.id && item.getElement('ul')) {
				this.liste[key].desactive = false;
				/******	POUR CHEATER CE !§?@§§! de BUG		********/
				//this.liste[key].effect.from = 0;
				//this.liste[key].effect.to = 1;
				this.liste[key].effect.start(0,1);
				//alert(clicked+', état : '+clicked.getParent().desactive+', from:'+item.effect.from+', now:'+this.liste[key].effect.now+', to:'+this.liste[key].effect.now);
			}
		}, this);
	
		/*
		// on cherche la liste des sous-menu
		liste = $(id).getParent().getChildren();

		// on ferme chaque sous-menu
		liste.each(function(item, index){
		    // le sous-menu de cette section existe et est affiché
		    if (item.getElement('ul') && item.getElement('ul').getStyle('opacity') != 0 && item != $(id)) {
		    	item.getElement('ul').setStyle('z-index', '10');
				//item.getElement('ul').get('tween', {property: 'opacity', duration: 'short'}).start(0);
				new Fx.Style(item.getElement('ul'),'opacity',{ duration: 250 }).start(0);
		    }

		});

		// on n'effectue les opération que si le sous-menu existe
		if ($(id).getElement('ul')){
			// si le sous-menu est déjà activé, on ne fait rien
			if ($(id).getElement('ul').getStyle('opacity') != 1 || $(id).getElement('ul').getStyle('display') != 'block') {
				$(id).getElement('ul').setStyles({ opacity: 0, display: 'block', 'z-index': '11'});
				//$(id).getElement('ul').get('tween', {property: 'opacity', duration: 'short'}).start(1);
				new Fx.Style($(id).getElement('ul'),'opacity',{ duration: 250 }).start(1);
			}


		}*/
	},
	desactive: function (statut) {
		if (this.etat == "active") {
			this.etat = "desactive";
			if (this.time == 0) {
				this.time = 1000;
				//console.debug("demarrage ...");
				this.desactive.bind(this, "ok").delay(2000);
			}
		} else if (this.time == 1000 && statut == "ok") {
			this.liste.each(function(item, key) {
				// on vérifie qu'il y a un sous-menu
				if (item.getElement('ul')) {
					this.liste[key].desactive = true;
					this.liste[key].effect.start(0);
				}
			}, this);
			this.time = 0;
			//console.debug("fini ! "+this.etat+" "+statut);
		}
		/*
		//alert('on sort');
		liste = $('menu_principal').getChildren('div.menu_principal').getElement('ul').getChildren();
		//alert(liste);
		liste.each(function(valeur, key){
			valeur.each(function(item, index){
				// le sous-menu de cette section existe et est affiché
			    if (item.getElement('ul') && item.getElement('ul').getStyle('opacity') == 1) {
			    	//item.getElement('ul').get('tween', {property: 'opacity', duration: 'short'}).start(0);
					new Fx.Style(item.getElement('ul'),'opacity',{ duration: 250 }).start(0);
			    }

			}); //The optional second argument for binding isn't used here.
		});*/
	},
	mouseOver: function() {
		//alert(this.etat);
		this.etat = "active";
		this.time = -1;
		//alert('ok');
	},
	mouseOut: function() {
		//alert(this.etat);
		this.etat = "active";
		this.time = 0;
		//alert('ok');
	}
});


function menuGaucheActive (menu_id) {

	var mgauche = $('menu_gauche').getElement('div.menu_gauche');
	var divs = mgauche.getChildren('div');
	var div = $(menu_id);
	// on cherche le nombre de sous-menu
	if (div) {
		var taille = 0;
		div.getChildren().each(function(item, key){
										
			if (item.href != null) taille += item.getStyle('line-height').toInt();
			else {
				taille += item.getStyle('margin-bottom').toInt();// + item.getStyle('margin-top').toInt();
				item.getChildren().each(function(value, key) {
					//taille += value.getElement('a').getStyle('height').toInt() + value.getElement('a').getStyle('margin-bottom').toInt();
					taille += value.getElement('a').getStyle('height').toInt() + value.getElement('a').getStyle('margin-bottom').toInt();
					
				});
			}
		});
		//taille -= 4;
		// on vérifie que le sous-menu ne s'affiche pas
		if (div.getStyle('height') == '19px' || div.getStyle('height').toInt() == (taille + div.getStyle('padding-bottom').toInt() + div.getStyle('border-width').toInt())) {
			// on choisit un menu spécifique, on effectue la redirection
			if (menuGaucheActive.arguments.length == 1) {
				// si le menu est déjà ouvert, on effectue une simple redirection
				if (div.getStyle('height').toInt() == (taille + div.getStyle('padding-bottom').toInt() + div.getStyle('border-width').toInt()))
					window.location.replace($(menu_id).getElement('a').href);
				else {
					// on modifie sa taille
					new Fx.Morph($(menu_id), {duration: 500, onComplete: function() {window.location.replace($(menu_id).getElement('a').href);}}).start({
						'height': taille+'px'
					});
					/*$(menu_id).getElement('a').setStyle('color', 'rgb(119, 193, 11)');*/
				}
			} // ouverture d'un des 2 premiers menus, on ne redirige pas
			else {
				new Fx.Morph($(menu_id), {duration: 500}).start({
					'height': taille+'px'
				});
					
			}
			
//			$(menu_id).setStyle('background-image', 'url(assets/templates/pcas/images/menu_gauche_ouvert.gif)');
		
			// on test s'il ne s'agit pas d'un premier affichage
			if (menuGaucheActive.arguments.length == 1)
			
				// on cherche les autres menus
				divs.each(function(item, key) {
					// on ferme les menus sauf celui survolé
					if (item.id != menu_id && item.getStyle('height') != '19px') {
						var height = item.getStyle('height').toInt();
						new Fx.Tween(item.id, {duration: 500, property:'height'}).start(19);
						//$(item.id).setStyle('background-position', '0px '+((item.getStyle('padding-top').toInt()+item.getStyle('padding-bottom').toInt()+height) - 136 + 108)+'px');
//						$(item.id).setStyle('background-image', 'url(assets/templates/pcas/images/menu_gauche_ferme.gif)');
						/*$(item.id).getElement('a').setStyle('color', 'rgb(0, 144, 215)');*/
					}
				});
		} else  {
			window.location.replace($(menu_id).getElement('a').href);
		}
				
	}

}

/*
window.addEvent('domready', function() {
	var mgauche = $('menu_gauche').getElement('div.menu_gauche');
	if (mgauche)
		mgauche.getChildren('div').each(function(item, key) {
			// y a t-il un sous menu ?
			if (item.getElement('div')) {
				item.getElement('a').addEvent('click', function() {
					menuGaucheActive(item.id);
					return false;
				});
			}
		});
});
*/
if (typeof addLoadEvent != 'function')
	function addLoadEvent(func) {
		var oldonload = window.onload;
		if (typeof window.onload != 'function') {
			window.onload = func;
		} else {
			window.onload = function() {
				oldonload();
				func();
			}
		}
	}
	

addLoadEvent(function() {
	var mgauche = $('menu_gauche').getElement('.menu_gauche');
	if (mgauche)
		mgauche.getChildren('div').each(function(item, key) {
			// y a t-il un sous menu ?
			if (item.getElement('div')) {
				item.getElement('a').addEvent('click', function() {
					menuGaucheActive(item.id);
					return false;
				});
			}
		});
});
addLoadEvent(function() {
	new MenuPrincipal();
	var mgauche = $('menu_gauche').getElement('.menu_gauche');
	var active = mgauche.getElement('div.active');
	
	// si on consulte une page du menu
	if (!active) {
	// sinon on affiche les 2 premiers sous menu	
		var premier = mgauche.getElement('div');
		var deuxieme = premier.getNext();
		menuGaucheActive(premier.id, true);
		menuGaucheActive(deuxieme.id, true);
	}	
});
