/**
 *  Formulaire en Ajax
 *  */
$(function(){
	$('#contactForm').ajaxForm({
		dataType:  'json', 
		success : function(data){
			if(data.erreur.email){ display_error( data.erreur.email ); }
			if(data.erreur.message){ display_error( data.erreur.message ); }
			if(data.succes){
				display_succes(data.succes);
				$("#contactForm input").val(" ");
			}
		}
	});
	
	
	$('#newsletterForm').ajaxForm({
		dataType:  'json', 
		success : function(data){
			if(data.erreur.email){ display_error( data.erreur.email ); }
			if(data.succes){
				display_succes(data.succes);
				$("#contactForm input").val(" ");
			}
		}
	});
});

$(function(){
	$('#bouton_theme a').click(function(){
		$.post('/?switch=1');
		$('body').fadeOut(500,function(){
			if($('link:first').attr('href') == "/css/style2.css"){
				$('link:first').attr('href',"/css/style.css");
			}
			else{
				$('link:first').attr('href',"/css/style2.css");
			}
			$('body').fadeIn();
		})
	});
});
/**
 * Message de succes ou échec d'une action
 * */
function display_error(error){
	 $("body").prepend("<div id='info_no'><h3>"+error+"</h3></div>");
	 $("#info_no").hide().slideDown().click(function(){
		$(this).slideUp();
	 });
}
function display_succes(msg){
	 $("body").prepend("<div id='info_yes'><h3>"+msg+"</h3></div>");
	 $("#info_yes").hide().slideDown().click(function(){
		$(this).slideUp();
	 });
}

/**
 * Toggle
 * */
$(function(){
	// On cache tous les panneaux
	$("#headUp div[class$=_on]").hide();
	// Quand on clique sur un éléments du bandeau on affiche le truc correspondant
	$("#bandeau span[class$=_off]").click(function(){
		headShow("."+$(this).attr("class").replace("_off","_on"));
	});

});

// On choisir l'elem à afficher, on cache alors les autres avec un slideUp
function headShow(elem){
	// Tous les éléments composants le bandeau
	var elems = [".hat_map_on",".hat_inscription_on",".hat_recherche_on",".hat_contact_on"];
	for( var i in elems){
		if(elems[i]==elem){
			$(elems[i]).slideToggle(500);
		}
		else{
			$(elems[i]).slideUp(500);
		}
	}
}


/**
*   Change la couleur des boites en rollOver
**/

$(document).ready(function(){

	// On cache tout
	$(".boite_carte_plus").stop().animate({ opacity: 0 }, 300);
	
	// Quand on passe la souris sur la boite
	$(".boite_carte").hover(function() {
		$(this).find("p,h3").stop().animate({ color: "#333333" }, 300);
		$(this).find(".boite_carte_plus").stop().animate({ opacity: 1 }, 300);
	
	},function() {
		$(this).find("p,h3").stop().animate({ color: "#777777" }, 300);
		$(this).find(".boite_carte_plus").stop().animate({ opacity: 0 }, 300);
	});
	
	// Le clique sur tout le plat
	$(".boite_carte").css("cursor","pointer").click(function(){
		window.location = $(this).find("a").attr("href");
	})

});


/**
 *    CARROUSEL JS
 **/
var carrousel = {
    nbSlide : 0,
    nbCurrent : 1,
    elemCurrent : null,
    elem : null,
    timer : null,
    
    init : function(elem){
        this.nbSlide = elem.find(".slide").length;

        // Créer la pagination
        for(var i=1;i<=this.nbSlide;i++){
            elem.find(".navigation").append("<div>"+elem.find(".pager:eq("+(i-1)+")").text()+"</div>");
        }

        var page = elem.find(".navigation div").click(function(){
            carrousel.gotoSlide( page.index(this)+1 );
        })

        // Initialisation du carrousel
        this.elem=elem;
        elem.find(".slide").hide();
        elem.find(".slide:first").show();
        this.elemCurrent = elem.find(".slide:first");
        this.elem.find(".navigation").css("opacity",1);   // On rend la navigation opaque
        this.elem.find(".navigation div:first").addClass("active");

        // On cré le timer
        carrousel.play();

        // Stop quand on passe dessus
        elem.mouseover(carrousel.stop);
        elem.mouseout(carrousel.play);
    },
    gotoSlide : function(num){
        if(num==this.nbCurrent){ return false; }

        /* Animation en fadeIn/fadeOut*/ 

        this.elemCurrent.fadeOut();
        this.elem.find("#slide"+num).fadeIn();

        this.elem.find(".navigation div").removeClass("active");
        this.elem.find(".navigation div:eq("+(num-1)+")").addClass("active");
        this.nbCurrent = num;
        this.elemCurrent = this.elem.find("#slide"+num);
    },
    next : function(){
        var num  = this.nbCurrent+1;
        if(num  >this.nbSlide){
            num  = 1;
        }
        this.gotoSlide(num);
    },
    prev : function(){
        var num  = this.nbCurrent-1;
        if(num< 1){
            num= this.nbSlide;
        }
        this.gotoSlide(num);
    },
    stop : function(){
        window.clearInterval(carrousel.timer);
    },
    play : function(){
        window.clearInterval(carrousel.timer);
        carrousel.timer = window.setInterval("carrousel.next()",2500);
    }

}

$(function(){
    carrousel.init($("#carrousel"));
});

$(function(){$(".boite_carte img").corner("15px");}); 

