/*
	JS pour FAB - Principal
	Simon - egzakt.com
	2009-07-16
*/

function bloc_deroulant(objet) {
	objet.next().slideToggle("normal");
	objet.toggleClass("selected");

	if (!$(".bloc_deroulant_selected a.selected").length) {
		objet.parent().toggleClass("bloc_deroulant_selected");
	}
	return false;
}


$(function() {
	$(".zone_deroulante:not(.ouvert)").hide();

	$("a.lien_deroulant").click(
		function(){
			return bloc_deroulant($(this));
		}
	);

	$(".tmpl_caract tr:even").addClass("paire");

	if ($(".contenu a.selected").length) {
		$(".contenu a.selected").parent().addClass("bloc_deroulant_selected");
	}

	// Ajuster la hauteur du background de #content
	// Defaut : 320px;
	if (parseInt($("#content").height()) > 320)
		$("#content_bg").css('height',$("#content").height());

	// Changement des photos
	$('#margin_photos .handle').click(function() {
		return animer_photos(this);
	});
	// ATIVEE PHOTOS CONTINUES
	photos_timeout = window.setTimeout('animer_photos_recursif()',photos_ms);

	// Changement des phrases à l'accueil
	$('.phrase a.handle').click(function() {
		return true;
	});

	// Changement des saviez-vous
	$('#saviezvous a.handle').click(function() {
		return change_saviezvous(this);
	});
	saviezvous_timeout = window.setTimeout('animer_saviezvous_recursif()',saviezvous_ms);


	// Emails
	// non-fonctionnel
// 	$("a").each(function() {
// 		if ($(this).attr('href').match(/mailto/)) {
// 			var mt = decode_emailfilter($(this).attr('href'));
// 			$(this).attr('href',mt);
// 			if ($(this).html() == "Activer javascript")
// 				$(this).html(mt.replace(/mailto:/g,''));
// 		}
// 	});

	if (typeof contact_ready == "function")
		contact_ready();
	if (typeof accueil_ready == "function")
		accueil_ready();
});

/* Loops */
var photos_ms = 10000;	// Temps en millisecondes
var photos_timeout = null;
function animer_photos_recursif() {
	animer_photos();
	if (!photos_timeout)
		photos_timeout = window.setTimeout('animer_photos_recursif()',photos_ms);
}

var saviezvous_ms = 5000;	// Temps en millisecondes
var saviezvous_timeout = null;
function animer_saviezvous_recursif() {
	if (typeof $('#saviezvous')[0] != "undefined") {
		change_saviezvous($('#saviezvous .handle'));
		if (saviezvous_timeout)
			saviezvous_timeout = window.setTimeout('animer_saviezvous_recursif()',saviezvous_ms);
	}
}

/* FUNCTION animer_photos()
 * Changement des photos
 * @author SGL
 *
 * Logique :
 *		Initial : 		[Photo1 = image-0] [Photo2 = image-1] [Photo3 = image-2]
 *		Premier click	[Photo1 = image-3] [Photo2 = image-0] [Photo3 = image-4]
 *		Deuxieme click	[Photo1 = image-5] [Photo2 = image-3] [Photo3 = image-6]
 *		Troisieme click	[Photo1 = image-0] [Photo2 = image-5] [Photo3 = image-1]	(boucler)
 * ...
 */

var change_photo_inaction = 0;			// Pour eviter les clicks lorsque l'anim fonctionne
var change_photo_inaction_total = 3;
var anim_version = 2;
function animer_photos(a) {
	if (change_photo_inaction == 0) {

		if (photos_timeout) {
			window.clearTimeout(photos_timeout);
			photos_timeout = null;	// !Important
		}

		change_photo_inaction = change_photo_inaction_total;

		// Trouver les prochaines photos _next
		if (anim_version == 1) {
			photos_next[2] = photos_next[1];
			photos_next[1] = boucler(photos_next[1],photos.length); // (photos_next[1] + 2 >= photos.length) ? photos_next[1]-(photos.length-2) : photos_next[1]+2;
			photos_next[3] = boucler(photos_next[3],photos.length); // (photos_next[3] + 2 >= photos.length) ? photos_next[3]-(photos.length-2) : photos_next[3]+2;
		} else if (anim_version == 2) {
			photos_next[1] = boucler(photos_next[1],photos.length);
			photos_next[2] = boucler(photos_next[2],photos.length);
			photos_next[3] = boucler(photos_next[3],photos.length);
		}

		anim_temps = 500;
		anim_opacity = 0.5;

	// console.log(photos_next[1]);
	// console.log(photos_next[2]);
	// console.log(photos_next[3]);
	// console.log("--");

		$('#photo1').stop().animate(
			{ backgroundPosition:"(480px 0)", opacity:anim_opacity },
			anim_temps,
			function() {
				// Placer image de _next dans la photo visible
				$(this).css("backgroundImage",$('#'+$(this).attr('id')+'_next').css('backgroundImage'));
				$(this).css("opacity",1.0);
				$(this).css("backgroundPosition","0 0");

				// Placer une nouvelle photo dans _next, selon le array de photos defini au depart
				$('#photo1_next').css('backgroundImage',photos[photos_next[1]]);

				change_photo_inaction--;
			}
		);
		$('#photo2').stop().animate(
			{ backgroundPosition:"(480px 0)", opacity:anim_opacity },
			anim_temps,
			function() {
				// Placer image de _next dans la photo visible
				$(this).css("backgroundImage",$('#'+$(this).attr('id')+'_next').css('backgroundImage'));
				$(this).css("opacity",1.0);
				$(this).css("backgroundPosition","0 0");

				// Placer une nouvelle photo dans _next, selon le array de photos defini au depart
				$('#photo2_next').css('backgroundImage',photos[photos_next[2]]);

				change_photo_inaction--;
			}
		);
		$('#photo3').stop().animate(
			{ backgroundPosition:"(-480px 0)", opacity:anim_opacity },
			anim_temps,
			function() {
				// Placer image de _next dans la photo visible
				$(this).css("backgroundImage",$('#'+$(this).attr('id')+'_next').css('backgroundImage'));
				$(this).css("opacity",1.0);
				$(this).css("backgroundPosition","0 0");

				// Placer une nouvelle photo dans _next, selon le array de photos defini au depart
				$('#photo3_next').css('backgroundImage',photos[photos_next[3]]);

				change_photo_inaction--;
			}
		);

		// ATIVEE PHOTOS CONTINUES
		if (!photos_timeout)
			photos_timeout = window.setTimeout('animer_photos_recursif()',photos_ms + anim_temps);
	}
	return false;
}

/* HELPER pour boucler dans le array de photos
 */
 var facteur_boucle = 2;
function boucler(index,max,facteur) {
	if (facteur == null) facteur = facteur_boucle;
	return (index + facteur_boucle >= max) ? index-(max-facteur_boucle) : index+facteur_boucle;
}


/* FUNCTION change_phrase()
 * Changement des phrase a l'accueil
 * @author SGL
 *
 */
function change_phrase(handle) {

	if (phrase_timeout) {
		window.clearTimeout(phrase_timeout);
		phrase_timeout = null;	// !Important
	}

	var contenu = $(handle).nextAll("div.contenu")[0];
	var ids = $(handle).parent().attr('id').replace(/phrase_/,'');
	var texte_id = ids.replace(/^([0-9]*)_(.*)/,'$1');
	var bloc_id = ids.replace(/(.*)_([0-9]*)$/,'$2');
	$.ajax({
		type: "POST",
		url: "/"+langue+"/change-phrase",
		data: "texte_id="+texte_id+"&bloc_id="+bloc_id,
		dataType: "json",
		success: function(resultat){
			if (typeof(resultat.id) != "undefined") {
				$(contenu).fadeOut("fast",function() {
					$(handle).parent().attr('id','phrase_'+texte_id+'_'+resultat.id);
					$(contenu).html(resultat.contenu);
					$(contenu).fadeIn("normal");

					if (!phrase_timeout)
						phrase_timeout = window.setTimeout('animer_phrase_recursif()',phrase_ms);
				});
			}
		},
		error: function(e) {
			return false;
		}
	});
	return false;
}

/* FUNCTION change_saviezvous()
 * Changement des textes saviez-vous
 * @author SGL
 *
 */
function change_saviezvous(handle) {

	if (saviezvous_timeout) {
		window.clearTimeout(saviezvous_timeout);
		saviezvous_timeout = null;	// !Important
	}

	var contenu = $(handle).nextAll("div.contenu")[0];
	var titre = $(handle).nextAll("h2.titre")[0];
	var bloc_id = parseInt($(handle).parent().attr('class').replace(/saviezvous_/,''));
	$.ajax({
		type: "POST",
		url: "/"+langue+"/change-saviezvous",
		data: "bloc_id="+bloc_id,
		dataType: "json",
		success: function(resultat){
			if (typeof(resultat.id) != "undefined") {
				$('#saviezvous div.contenu, #saviezvous h2.titre').fadeOut("fast",function() {
					$(handle).parent().attr('class','saviezvous_'+resultat.id);
					$(contenu).html(resultat.contenu);
					$(titre).html(resultat.titre);
					$(this).fadeIn("normal");

					if (!saviezvous_timeout)
						saviezvous_timeout = window.setTimeout('animer_saviezvous_recursif()',saviezvous_ms);
				});
			}
		}
	});
	return false;
}

function decode_emailfilter(mt) {
	return mt.replace(/\?/g,'a').replace(/\$/g,'e').replace(/\@/g,'i').replace(/\&/g,'o').replace(/\%/g,'u').replace(/\//g,'@');
}

/* FUNCTION change_nouvelle()
 * Changement des nouvelles
 * @author SGL
 *
 */
function change_nouvelle(handle) {

	if (nouvelle_timeout) {
		window.clearTimeout(nouvelle_timeout);
		nouvelle_timeout = null;	// !Important
	}

	var contenu = $(handle).nextAll("div.contenu")[0];
	var titre = $(handle).nextAll("h2.titre")[0];
	var lien = $(handle).nextAll("div.suite")[0];
	var nouvelle_id = parseInt($(handle).parent().attr('id').replace(/nouvelle_/,''));

	$.ajax({
		type: "POST",
		url: "/"+langue+"/change-nouvelle",
		data: "nouvelle_id="+nouvelle_id,
		dataType: "json",
		success: function(resultat){
			if (typeof(resultat.id) != "undefined") {
				$('.nouvelle').fadeOut("fast",function() {
					$(handle).parent().attr('id','nouvelle_'+resultat.id);
					$(contenu).html(resultat.contenu);
					$(titre).html(resultat.titre);
					$(lien).children("a").attr('href',resultat.lien);
					if (resultat.suite) { $(lien).removeClass("invisible"); } else { $(lien).addClass("invisible"); }

					$(this).fadeIn("normal");

					if (!nouvelle_timeout)
						nouvelle_timeout = window.setTimeout('animer_nouvelle_recursif()',nouvelle_ms);
				});
			}
		}
	});
	return false;
}
