jQuery(function() {
	var loaderContact = jQuery('img#chargementMail').hide();
	jQuery().ajaxStart(function() {
		loaderContact.show();
	}).ajaxStop(function() {
		loaderContact.hide();
		$('#cprenom').val("");
		$('#cnom').val("");
		$('#cemail').val("");
		$('#cmessage').val("");
		$('#ccopie').attr('checked', false);
	}).ajaxError(function(a, b, e) {
		throw e;
	});
	
	var v = jQuery("#contactForm").validate({
		submitHandler: function(form) {
			jQuery(form).ajaxSubmit({
				target: "#result"
			});
		}
	});
	
	var v2 = jQuery("#commentform").validate({
		submitHandler: function(form) {
			jQuery(form).ajaxSubmit({
				target: "#result"
			});
		}
	});
	
	$.fn.boxHover = function(){
		$(this).mouseover(function(){
			$(this).addClass("boxHover");
		}).mouseout(function(){
			$(this).removeClass("boxHover");}).click(function(e){
			document.location.href = $(this).find("a").attr("href");
			e.preventDefault();
		});
	};
	
	$.fn.btonHover = function(){
		$(this).hover(function(){
			$(this).addClass("btonHover");
		},function(){
			$(this).removeClass("btonHover");
		});
	};
	
	$.fn.mediaHover = function(){
		$(this).hover(function(){
			$(this).addClass("mediaHover");
		},function(){
			$(this).removeClass("mediaHover");
		});
	};
	
});	
	
$(document).ready(function(){

	var navTimers = [];  
	 $('#menu > li').hover(  
		function () {  
			var id = jQuery.data(this);  
			var $this = $(this);  
			navTimers[id] = setTimeout(function() {
				$this.find('ul').show();
				$this.find('a:first').addClass("hover");
				navTimers[id] = ""; 
			}, 200 );  
		},  
		function () {  
			var id = jQuery.data(this);  
			if (navTimers[id] != "" ) {  
				clearTimeout( navTimers[id] );  
			} else { 
					$(this).find("ul").hide(100);  
				$(this).find('a:first').removeClass("hover");
			}  
		}  
	);
	
	 $('#listeTri').hover(  
		function () {  
			var id = jQuery.data(this);  
			var $this = $(this);  
			navTimers[id] = setTimeout(function() {
				$('div#listeTriContent').slideDown(300); 
				navTimers[id] = ""; 
			}, 200 );  
		},  
		function () {  
			var id = jQuery.data(this);  
			if (navTimers[id] != "" ) {  
				clearTimeout( navTimers[id] );  
			} else { 
				$('div#listeTriContent').hide(100);
			}  
		}  
	);
		
	$(".boxFeatured").boxHover();
	$(".boxTopClic").boxHover();
	$(".boxSidebarTopClic").boxHover();
	$(".boxListeContainer").boxHover();
	$(".boxListeContainerLast").boxHover();
	$(".boxListeCheval").boxHover();
	$(".boxListeChevalLast").boxHover();
	$(".boxSidebarChevaux").boxHover();
	$(".planSiteContent li").boxHover();
	
	$(".btonSubmit").btonHover();
	
	$("div.panel-wrapper").mediaHover();
	$("div.photoSingle").mediaHover();
	$("div.galeriePhotoItem").mediaHover();
	$("div.galerieVideoItem").mediaHover();
	 
	$("div.sidebarVideoItem").hover(function(){
	   $(this).addClass("sidebarVideoItemHover");
	 },function(){
	   $(this).removeClass("sidebarVideoItemHover");
	 });

	$('#imgHeader').cycle({
		fx: 'fade',
		timeout: 7000,
		speed: 800,
		slideExpr: 'img'
	});

	$('textarea').elastic();

	if ($.browser.msie && $.browser.version == '6.0') {
		$('.btonPlayVideo').css("display","none");
		$('.btonPlayVideoSlider').css("display","none");
	}
	
	$('.bulle').css('word-wrap','break-word');
	$('.bulleEnCours').css('word-wrap','break-word');
	$('.bulleLast').css('word-wrap','break-word');
	
	$('.planSiteContent ul > li:last-child').css('border','none');
	$('#boxMediaDocument > p:last-child').css('border','none');
	$('#boxMediaDocument > p:last-child').css('padding-bottom','0');

	$("span.dureeVideo").css("opacity", "0.7");
	$("span.dureeVideoSlider").css("opacity", "0.7");
			
	miseEnPause = false; // Variable utilisˇe dans les callbacks de la prettyPhoto
	
	$("a[rel^='prettyPhoto']").prettyPhoto({
		animationSpeed: 'normal', /* fast/slow/normal */
		padding: 40, /* padding for each side of the picture */
		opacity: 0.7, /* Value betwee 0 and 1 */
		showTitle: false, /* true/false */
		allowresize: true, /* true/false */
		counter_separator_label: ' sur ', /* The separator for the gallery counter 1 "of" 2 */
		theme: 'dark_rounded', /* light_rounded / dark_rounded / light_square / dark_square */
		hideflash: false, /* Hides all the flash object on a page, set to TRUE if flash appears over prettyPhoto */
		modal: false, /* If set to true, only the close button will close the window */
		changepicturecallback: function(){
			if($("#player_play").css("display") == "none")
			{
				miseEnPause = true;
				$("#jquery_jplayer").pause();
			}
		}
	});	
	
	$('#newsSubmitFocus').hide();
	$('.champNews').focus(function() {  
		if (this.value == this.defaultValue){  
			this.value = '';  
		}  
		if(this.value != this.defaultValue){  
			this.select();  
		}  
	});  
	$('.champNews').blur(function() {  
		if (this.value == ''){  
			this.value = (this.defaultValue ? this.defaultValue : '');  
		}  
	});  
		

// ==================================================================================================
// Box newsletter	
	$('#newsForm').submit(function() 
	{  
		$.getJSON("../script/inscriptionNewsletter.php?q=" + $('#emailNewsletter').val(), function(data){
			$("#messageNewsletter").empty();
		
			if (data=="succes")
			{
				$('#emailNewsletter').val("");
				$("#messageNewsletter").animate({height:'25px'}, 400, function() {$("#messageNewsletter").append('<font color="green">Votre inscription est enregistr&eacute;e...</font>').hide().fadeIn(200);});
			}
			else  if (data=="existant")
			{
				$("#messageNewsletter").animate({height:'25px'}, 400, function() {$("#messageNewsletter").append('<font color="red">Adresse d&eacute;j&agrave; inscrite...</font>').hide().fadeIn(200);});
			}
			else  if (data=="nonValide")
			{
				$("#messageNewsletter").animate({height:'25px'}, 400, function() {$("#messageNewsletter").append('<font color="red">Adresse e-mail non valide...</font>').hide().fadeIn(200);});
			}

		});
		return false;
	});

// ==================================================================================================
// Box live search	
		
	document.searchForm.setAttribute( "autocomplete","off" );
	
	$('.champSearch').focus(function() {  
		if (this.value == this.defaultValue){  
			this.value = '';  
		}  
		if(this.value != this.defaultValue){  
			this.select();  
		}  
	});  
	$('.champSearch').blur(function() {  
		$("#autoComplete").fadeOut();
		if (this.value == ''){  
			this.value = (this.defaultValue ? this.defaultValue : '');  
		}  
	});
	
	function soumissionRecherche()
	{
		$(".searchForm").submit();
	}
	
	var delayKeyup = "";
	var autoSuggest = "";
	$("#listeResultats").append('<ul></ul>');
	$('.champSearch').keyup(function() 
	{  
		clearTimeout(autoSuggest);
		clearTimeout(delayKeyup);
		var search; 
		search = $('.champSearch').val(); 
		delayKeyup = setTimeout(function () {
			if (search.length > 2) // Si au moins 3 caract¸res saisis : Affichage de la box
			{
				$("#loaderSearch").fadeIn(200);
				$("#searchSubmit").fadeOut(200);

				// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
				// Affichage dynamique des rˇsultats 		
					$.ajaxSetup({ cache: false});			
					$.getJSON("../script/liveSearch.php", {q: $('.champSearch').val()},
					function(data) // "data" correspond au tableau JSON retournˇ par la page php de recherche
					{
						autoSuggest = setTimeout(function () {
							$("#autoComplete").fadeIn();
							$("#listeResultats ul li").remove();
							$.each(data[0].tuple, function(i) // Pour chaque ˇlˇment du rˇsultat de recherche ("i" reprˇsente le numˇro de ligne)
							{ 
								$("#listeResultats ul").append('<li><a href="' + data[0].tuple[i].url+ '" class="titreListeSearch"><img src="' + data[0].tuple[i].image + '"  class="imgSearch" /><span>' + data[0].tuple[i].titre + '</span><br />' + data[0].tuple[i].contenu + '</a></li>');
							});
		
							// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
							// Contenu du bas de la box de recherche 		
							if (data[0].nbResultat == 0)
							{
								$("#listeResultats ul li").remove();
								$(".aucunResultat").remove();
								$("a.voirResultats").remove();
								$("#autoComplete").append('<div class="aucunResultat">Il n\'existe aucun r&eacute;sultat pour cette recherche...</div>');
							}
							else if (data[0].nbResultat == 1)
							{
								// $(".listeResultats ul li").remove();
								$(".aucunResultat").remove();
								$("a.voirResultats").remove();
								$("#autoComplete").append('<a href="' + data[0].tuple[0].url+ '" class="voirResultats">Voir le r&eacute;sultat</a>');
							}
							else
							{
								// $(".listeResultats ul li").remove();
								$(".aucunResultat").remove();
								$("a.voirResultats").remove();
								$("#autoComplete").append('<a href="../../?s=' + $('.champSearch').val() +'" class="voirResultats">Voir les ' + data[0].nbResultat + ' r&eacute;sultats</a>');
							}
							$("#searchSubmit").fadeIn(200);
							$("#loaderSearch").fadeOut(200);
						}, 500);
					});
			}
			else// Si moins de 3 caract¸res saisis : Disparition de la box
			{
				$("#autoComplete").fadeOut();
				$("#loaderSearch").fadeOut(200);
				$("#searchSubmit").fadeIn(200);
			}
		}, 400);
	});		
		
});

$().ready(function() {
	$('#coda-slider-1Index').codaSlider({
		autoSlide: true,
		autoSlideInterval: 6000,
		autoSlideStopWhenClicked: true
	});
	$('#coda-slider-1').codaSlider();
	$('#coda-slider-2').codaSlider();
	$('#coda-slider-111').codaSlider();
	$('#coda-slider-113').codaSlider();
	$('#coda-slider-213').codaSlider();
});

$(function() {
	$("#boxTabs").tabs();		
});

