$(document).ready(function()
{
	/* -------------------------------------- */
	/* ---------- Détection d'IE 7 ---------- */
	/* -------------------------------------- */
	is_ie7 = navigator.userAgent.toLowerCase().indexOf('msie 7.0') > -1;
	is_ie8 = navigator.userAgent.toLowerCase().indexOf('msie 8.0') > -1;
	
	if (is_ie7) {
		$('html').attr('id', 'is_ie7');
	} else if (is_ie8) {
		$('html').attr('id', 'is_ie8');
	}
	
	
	/* --------------------------------------------------------------- */
	/* AutoEmpty : vidage automatique des champs de formulaire au clic */
	/* --------------------------------------------------------------- */
	$(".autoEmpty").each(function()
	{
		var defaultText = $(this).val();
		$(this).focus(function()
		{
			if ($(this).val() == defaultText)
			{
				$(this).val("");
			}
		});
		$(this).blur(function()
		{
			if ($(this).val() == "")
			{
				$(this).val(defaultText);
			}
		});
	});
	
	
	/* ---------------------------------------- */
	/* Dimensionnement et calage des sous menus */
	/* ---------------------------------------- */
	/* Affichage + positionnement nv1 */
	$('.nv0 li').each(function()
	{
		var nv0_largeur = $(this).width();
		
		$(this).children('ul.nv1').css('width', nv0_largeur);
		
		// IE 7
		if (is_ie7)
		{
			$(this).children('ul.nv1').css('margin-left', -nv0_largeur - 3);
		}
	});
	
	$('ul.nv1 li').each(function()
	{
		$(this).hover(function()
		{
			if ($(this).children().is('ul.nv2'))
			{
				var nv1_largeur = $(this).width();
				var nv1_hauteur = $(this).height();
				var nv1_padding = 8;
				
				if (!is_ie7)
				{
					$(this).children('ul.nv2').css({
						'margin-left': nv1_largeur,
						'margin-top': -nv1_hauteur - nv1_padding
					});
				}
				else if (is_ie7)
				{
					$(this).children('ul.nv2').css({
						'left': nv1_largeur,
						'margin-top': -nv1_hauteur + 7
					});
				}
			}
		});
	});
	
	
	/* ----------------------------------------- */
	/* Comptage du nombre d'images dans le diapo */
	/* ----------------------------------------- */
	nb_img_diapo 	= 0;
	img_en_cours 	= 1;
	img_a_afficher	= 0;
	
	$('.img_diapo').each(function()
	{
		nb_img_diapo++;
	});
	
	// Lancement automatique du diaporama
	timerDiapo = setInterval(diapoAuto, 5000);
	
});


/* --------------------- */
/* ----- Diaporama ----- */
/* --------------------- */
function diapoAuto()
{
	if (img_en_cours < nb_img_diapo)
		img_a_afficher = img_en_cours + 1;
	else
		img_a_afficher = 1;
	
	$('#diapo_'+img_en_cours).fadeOut(1000);
	$('#diapo_'+img_a_afficher).fadeIn(1000, function()
	{
		img_en_cours = img_a_afficher;
	});
}

function diapo(sens)
{
	clearInterval(timerDiapo);
	if (sens == 1)
	{
		if (img_en_cours < nb_img_diapo)
			img_a_afficher = img_en_cours + 1;
		else
			img_a_afficher = 1;
	}
	else if (sens == -1)
	{
		if (img_en_cours > 1)
			img_a_afficher = img_en_cours - 1;
		else
			img_a_afficher = nb_img_diapo;
	}
	
	$('#diapo_'+img_en_cours).fadeOut(1000);
	$('#diapo_'+img_a_afficher).fadeIn(1000, function()
	{
		img_en_cours = img_a_afficher;
	});
	
	timerDiapo = setInterval(diapoAuto, 5000);
}


/* ----------------------------------------- */
/* ----- Positionnement des infobulles ----- */
/* ----------------------------------------- */
function posInfobulle(cible)
{
	// Centrage
	var nav_largeur		= $(window).width();
	var nav_hauteur		= $(window).height();
	var cible_largeur	= $(cible).width();
	var cible_hauteur	= $(cible).height();
	var cible_top		= 0;
	var cible_left 		= 0
	
	// alert('Largeur nav : ' + nav_largeur + ' / Hauteur nav : ' + nav_hauteur + '\nLargeur info : ' + cible_largeur + ' / Hauteur info : ' + cible_hauteur);
	
	if (nav_largeur - cible_largeur > 0)
		cible_left = (nav_largeur - cible_largeur) / 2;
	else
		cible_left = 0;
	
	if (nav_hauteur - cible_hauteur > 0)
		cible_top = (nav_hauteur - cible_hauteur) / 2;
	else
		cible_top = 0;
	
	// alert('Top : ' + cible_top + ' / Left : ' + cible_left);
	
	$(cible).css({
		'top': 	cible_top,
		'left':	cible_left
	});
}


/* ----------------------------------------------- */
/* ----- Affichage / Masquage des infobulles ----- */
/* ----------------------------------------------- */
function ouvrirInfobulle(cible)
{
	// Positionnement
	posInfobulle(cible);
	
	// Affichage
	$(cible).css('visibility', 'visible');
}
function fermerInfobulle(cible)
{
	$(cible).css('visibility', 'hidden');
	$(cible+" input[type=text]").val("");
	$(cible+" select").val("");
	$(cible+" table.cont_captcha").show();
	$(cible+" input#promo_checkbox").attr("disabled","disabled");
	$(cible+" p").show();
	$(cible+" p#envoi_ko").hide();
	$(cible+" p#envoi_ok").hide();
}
