$(function()
{

	loopImg(1);

	// menu
	$("#header ul li").css("backgroundPosition","0px -203px");
	$("#header ul li.current_0").css("backgroundPosition","0px -273px");
	$("#header ul li").each(function()
	{
		$(this).mouseover(function()
		{
			$(this).stop().animate({"backgroundPosition":"(0px -273px)"},{duration:250});
		});
		
		$(this).mouseout(function()
		{
			if(!$(this).hasClass("current_0"))
			{
				$(this).stop().animate({"backgroundPosition":"(0px -203px)"},{duration:250});
			}
		});
	});
	
	$('.gallery-full a').each(function(){
		if($(this).attr("id") != "")
		{
			$(this).find('img').attr("title",$(this).find('img').attr("alt"));
		}
	});
	
	// header reduce
	$('.show-hide').click(function()
	{
			if(headerOpen)
			{
				$('#header').animate({"height":"163px"},800);
				$('.header-945').animate({"height":"163px"},800);
				$('body').stop().animate({"backgroundPosition":"(0px -274px)"},800);
				$('#global').css("background","none");
				$('.banner').hide();
				$('.event').hide();
				$('.show-hide').html("Agrandir le bandeau");
				headerOpen = false;
			}
			else
			{
				$('#header').animate({"height":"437px"},800);
				$('.header-945').animate({"height":"437px"},800);
				$('body').stop().animate({"backgroundPosition":"(0px 0px)"},800);
				$('#global').css("background","none");
				$('.banner').show();
				$('.event').show();
				$('.show-hide').html("Réduire le bandeau");
				headerOpen = true;
			}
			$.ajax({
				type: "POST",
				url: "_template/ajax/header_display.php",
				data: "headerOpen="+headerOpen
			});
	});
	
	// right tabs
	$('.tab1').mouseover(function()
	{
		$('.tab1').animate({"right":"246px"},400);
		$('.tabA').animate({"right":"0px"},400);
		$('.tab2').animate({"right":"0px"},500)
		$('.tabB').animate({"right":"-246px"},500);
		$('.tab3').animate({"right":"0px"},500)
		$('.tabC').animate({"right":"-246px"},500);
		$('.tab4').animate({"right":"0px"},500)
		$('.tabD').animate({"right":"-246px"},500);
	});
	$('.tabA').mouseleave(function()
	{
		$('.tab1').animate({"right":"0px"},500)
		$('.tabA').animate({"right":"-246px"},500);
	});
	
	$('.tab2').mouseover(function()
	{
		$('.tab2').animate({"right":"246px"},400);
		$('.tabB').animate({"right":"0px"},400);
		$('.tab1').animate({"right":"0px"},500)
		$('.tabA').animate({"right":"-246px"},500);
		$('.tab3').animate({"right":"0px"},500)
		$('.tabC').animate({"right":"-246px"},500);
		$('.tab4').animate({"right":"0px"},500)
		$('.tabD').animate({"right":"-246px"},500);
	});
	$('.tabB').mouseleave(function()
	{
		$('.tab2').animate({"right":"0px"},500)
		$('.tabB').animate({"right":"-246px"},500);
	});
	
	$('.tab3').mouseover(function()
	{
		$('.tab3').animate({"right":"246px"},400);
		$('.tabC').animate({"right":"0px"},400);
		$('.tab2').animate({"right":"0px"},500)
		$('.tabB').animate({"right":"-246px"},500);
		$('.tab1').animate({"right":"0px"},500)
		$('.tabA').animate({"right":"-246px"},500);
		$('.tab4').animate({"right":"0px"},500)
		$('.tabD').animate({"right":"-246px"},500);
	});
	$('.tabC').mouseleave(function()
	{
		$('.tab3').animate({"right":"0px"},500)
		$('.tabC').animate({"right":"-246px"},500);
	});
	
	$('.tab4').mouseover(function()
	{
		$('.tab4').animate({"right":"246px"},400);
		$('.tabD').animate({"right":"0px"},400);
		$('.tab2').animate({"right":"0px"},500)
		$('.tabB').animate({"right":"-246px"},500);
		$('.tab3').animate({"right":"0px"},500)
		$('.tabC').animate({"right":"-246px"},500);
		$('.tab1').animate({"right":"0px"},500)
		$('.tabA').animate({"right":"-246px"},500);
	});
	$('.tabD').mouseleave(function()
	{
		$('.tab4').animate({"right":"0px"},500)
		$('.tabD').animate({"right":"-246px"},500);
	});
	
	// gallery
	var count = 0;
	var max = 0;
	$('.gallery-full img').each(function(){max++;});
	
	$('.gallery-right').click(function()
	{		
		if(count != -max+5)
		{
			count--;
			$('.gallery-full').animate({marginLeft: (count*175)+"px"},{queue:false, duration:500});
			$('.gallery-left').css("background","url(_css/gallery-left.png) no-repeat left center");
		}
		
		if(count == -max+5)
		{
			$(this).css("background","url(_css/gallery-right-not.png) no-repeat right center");
		}
		else
		{
			$(this).css("background","url(_css/gallery-right.png) no-repeat right center");
		}
	});
	
	$('.gallery-left').click(function()
	{
		if(count != 0)
		{
			count++;
			$('.gallery-full').animate({marginLeft: (count*175)+"px"},{queue:false, duration:500});
			$('.gallery-right').css("background","url(_css/gallery-right.png) no-repeat right center");
		}
		
		if(count == 0)
		{
			$(this).css("background","url(_css/gallery-left-not.png) no-repeat left center");
		}
		else
		{
			$(this).css("background","url(_css/gallery-left.png) no-repeat left center");
		}
	});

	$().piroBox({
		my_speed: 300, //animation speed
		bg_alpha: 0.7, //background opacity
		radius: 4, //caption rounded corner
		scrollImage : false, // true == image follows the page _|_ false == image remains in the same open position
						   // in some cases of very large images or long description could be useful.
		slideShow : 'true', // true == slideshow on, false == slideshow off
		slideSpeed : 3, //slideshow duration in seconds(3 to 6 Recommended)
		pirobox_next : 'piro_next', // Nav buttons -> piro_next == inside piroBox , piro_next_out == outside piroBox
		pirobox_prev : 'piro_prev', // Nav buttons -> piro_prev == inside piroBox , piro_prev_out == outside piroBox
		close_all : '.piro_close' // add class .piro_overlay(with comma)if you want overlay click close piroBox
	});
	
	// tabs
	$('.event span').each(function(nbTab)
	{
		$(this).click(function()
		{
			$('.event span.current').removeClass('current');
			$(this).addClass('current');
			$('.maininfo').fadeOut(500);
			$('.mi'+(nbTab+1)).fadeIn(500);
		});
	});
	
	// cube
	$('.cube span').each(function(nbBall)
	{
		$(this).click(function()
		{
			$('.cube span.current').removeClass('current');
			$(this).addClass('current');
			$('.txtc').slideUp(500);
			$('.txt-'+(nbBall+1)).slideDown(500);
		});
	});
	
});

function loopImg(iCount)
{
	// Function for banner
	setTimeout
	(
		function()
		{
			$('.img-'+iCount).fadeOut(700);
			if($('.img-'+(iCount+1)).html() !== null)
			{
				$('.img-'+(iCount+1)).fadeIn(700);
			}
			else
			{
				$('.img-1').fadeIn(700);
			}
			iCount++;
			
			if($('.img-'+iCount).html() === null)
			{
				iCount = 1;
			}
			
			loopImg(iCount);
		}
		,5000	
	)
}

function inscription_newsletter() {
	var email = $('#nl_mail').val();
	var nom = $('#nl_name').val();
	var prenom = $('#nl_forname').val();
	
	if(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(email)) {
		$.ajax({
			type: "POST",
			url: "_template/ajax/inscription_newsletter.php",
			data: "email="+email+"&nom="+nom+"&prenom="+prenom,
			beforeSend: function() {
				$('#form_newsletter').replaceWith('<img id="newsletter_loading" style="position:absolute;top:50px;left:115px;" src="_images/ajax-loader.gif" alt="chargement" />');
			},
			success: function(msg){
				$('#newsletter_loading').replaceWith('<div>Votre inscription a bien été prise en compte.</div>');
			}
		});
	} else {
		alert('le champs email est obligatoire');
	}
	
}

function display_event_popup(event,element) {
	$('.cal-pop').remove();
	
	var html = '';
	html += '<div class="cal-pop" style="display:none;">';
		html += '<span class="cal-x" onclick="$(\'.cal-pop\').remove();"></span>';
		html += '<span class="cal-down"></span>';
		
		html += '<h4 style="background:#62B05A;visibility:visible;">'+event.title+'</h4>';
		html += '<p>';
		html += 'Date : <strong>'+event.date_text+'</strong><br />';
		if(event.date_heure_text != undefined && event.date_heure_text !== '')
			html += 'D&eacute;part &agrave; <strong>'+event.date_heure_text+'</strong>';
		html += '</p>';
		html += '<div class="cal-desc">';
		if(event.description != undefined)
			html += event.description;
		if(event.link != undefined && event.link !== '')
			html += '<a href="'+event.link+'">Plus d\'informations</a>';
		html += '</div>';
		//html += '<a href="#" class="cal-fichier">T&eacute;l&eacute;charger <span>fichier.odf</span><b></b></a>';
	html += '</div>';
	
	$('#global').append(html);
	
	// 17 pour la taille de la fleche
	// 20 pour le padding de 10 en haut et 10 en bas
	$('.cal-pop').css('top',element.pageY - $('.cal-pop').height() - 17 - 20);
	$('.cal-pop').css('left',element.pageX - (($('.cal-pop').width() + 20) / 2));
	$('.cal-pop').css('display','block');
}
