var intervalMEACarrousel = null;
var currentIndex = 0;

// Nombre de MEA total
var nbMEA = $('#une .visuel img').length;

$(document).ready(function() {
    
	if ($("input[name='tx_powermail_pi1[uid8]']") != false){
		$("input[name='tx_powermail_pi1[uid8]']").attr("value",$("#pagepid").attr("value"));
	}
	
	// Suppression de la colonne droite si vide
	if ($('#part3') != false){
		if ($('#part3').children().length==0) $('#part2').css('margin','0px').css('width','100%');
	}
	
	// Animation du carrousel
	if ($("#une .visuel") != false){
		animateCarrousel();
	}
	if ($("#une ul.sous-menu li:first") != false){
		$("#une ul.sous-menu li:first").addClass("first");
	}
	
	// Fix page de recherche
	if ($(".tx-indexedsearch") != false){
		$(".tx-indexedsearch .resultat div:last.result_count").remove();
		
		// On cache par defaut le lien 'Afficher les options'
		//$('.tx-indexedsearch .sword .option').css('display', 'none');
		
		// Effet sur les options de recherche avancee
		$('.tx-indexedsearch a.deplier').click( function() {		
			$('.tx-indexedsearch .sword .option').hide(10, function(){ 
				$('.tx-indexedsearch #options').slideDown('fast');
			});	
			return false;
		});	
		$('.tx-indexedsearch a.replier').click( function() {		
			$('.tx-indexedsearch #options').slideUp('fast');
			$('.tx-indexedsearch .sword .option').show(10);		
			return false;		
		});
	}
});


/**
 * Gere l'animation du carrousel
 * 
 * @param string Id du bonton ou vide pour afficher le premier bouton par defaut
 * @param boolean Activer/Desactiver le fondu pour passer a l'animation suivante
 */
function animateCarrousel(){
	// On affecte la classe btn_{chiffre} aux images
	$("#une .visuel").find("img").each(function(i) {
		key = i+1;
		$(this).addClass( "btn_" + key );
	});

	// On affecte la classe btn_{chiffre} aux titres des images
	$("#une .visuel").find("span").each(function(i) {
		key = i+1;
		$(this).addClass( "btn_" + key );
	});

   	// Declenchement de l'animation sans effet de fondu
	changeMEACarrousel('', false);

	// Fonction au clic d'un bouton
	$('#une .pagination li a').click( function() {

		// Id du bouton
		var idBtn = $(this).attr("id");
		
		// On arrete le defilement
		if (intervalMEACarrousel != null)
			window.clearTimeout(intervalMEACarrousel);
		
		if (idBtn == 'btn_prev'){		
			if (currentIndex > 0) {
				showMEACarrousel('btn_' + currentIndex, true);				
			} else {				
				showMEACarrousel('btn_' + nbMEA, true);
			}			
		} 
		else {
			if (idBtn == 'btn_next'){			
				if ((currentIndex + 1) > 3) {
					showMEACarrousel('btn_1', true);				
				} else {				
					showMEACarrousel('btn_' + (currentIndex + 2), true);
				}		
			} else {
				showMEACarrousel(idBtn, true);
			}
		}
	});
}

/**
 * Affiche la MEA du carrousel
 * 
 * @param string Id du bonton ou vide pour afficher le premier bouton par defaut
 * @param boolean Activer/Desactiver le fondu pour passer e l'animation suivante
 */
function changeMEACarrousel(idBtn, withEffect){
	// Nombre de MEA total
	nbMEA = $('#une .visuel img').length;

	if (nbMEA > 0) {
		// Par defaut, affichage de la premiere image
		if (idBtn == '') {			
			idBtn = 'btn_1';

			// Par de fondu pour le premier onglet affiche au demarrage
			withEffect = false;
		}
		
		if (idBtn == 'btn_prev')
			idBtn = 'btn_1';

		if (idBtn == 'btn_next')
			idBtn = 'btn_' + nbMEA;

		//*** Calcul de la prochaine MEA e afficher ***
		// Numero de la MEA en cours
		currentIndex = $('#une img').index($('#une img.' + idBtn));

		if (currentIndex >= (nbMEA - 1))
			var nextIndex = 0;
		else
			var nextIndex = currentIndex + 1;

		// Recuperation de image suivante ayant cet index
		var nextIdBtn = $('#une img').get(nextIndex).className;

		// Affichage de la MEA
		showMEACarrousel(idBtn, withEffect);

		if (intervalMEACarrousel != null)
			window.clearTimeout(intervalMEACarrousel);

		intervalMEACarrousel = window.setTimeout("changeMEACarrousel('" + nextIdBtn + "', true)", 5000);
	}
}

/**
 *	Affiche une MEA du carrousel
 *
 *	@param string Id du bouton
 *	@param boolean Activer/Desactiver le fondu pour passer a l'animation suivante
 */
function showMEACarrousel(idBtn, withEffect){

	currentIndex = $('#une a img').index($('#une img.' + idBtn));
	
	// On masque les images
	$('#une .visuel img').hide();
	
	// On affiche l'image correspondant au bouton
	if (withEffect) {	
		$('#une img.' + idBtn).fadeIn("slow");
	} else {
		$('#une img.' + idBtn).show();
	}
	
	// On masque les hover sur le bouton
	$('#une .pagination li a').css("text-decoration", "none");
	
	// On met un hover sur le bouton
	$('#une #' + idBtn ).css("text-decoration", "underline");
}
