$(document).ready(function(){
	/* SLIDER */
	$('.slider:not(.arrow)').slick({
		dots: true,
		arrows: false,
		autoplay: true,
	});

	$('.slider.arrow').slick({
		dots: false,
		arrows: true,
		slidesToShow: 2,
  		slidesToScroll: 2,
  		infinite: false,
  		autoplay: true,
  		responsive: [
			{
				breakpoint: 991,
				settings: {
					slidesToShow: 1,
					slidesToScroll: 1,
				}
			}
		]
	});

	// CTA Scroll sur slider
	$("#cta").click(function(){
		$("html, body").animate({
			scrollTop: parseFloat($(".section-01").offset().top) - $("header").innerHeight() + 2 
		})
	})

	

	/* MODAL FIX SCROLL */
	$('.modal').on('shown.bs.modal', function () {
		$("html").addClass("modal-open");
	})
	$('.modal').on('hidden.bs.modal', function () {
		$("html").removeClass("modal-open");
	})

	/* MENU LANGUE */
	$("#langue").click(function(){
		$("#langue ul").toggleClass("active");
	})

	/* MENU BURGER */
	$("#menu > span").click(function(){
		$("#menu ul").toggleClass("active");
	})

	/* SCROLL MENU */
	$("#menu a").click(function(e){
		if ($("body").hasClass("home")){
			e.preventDefault();

			var link = $(this).attr("href");
			$(this).parent().parent().find("li").removeClass("active");
			$(this).parent().addClass("active");

			$("html, body").animate({
				scrollTop: parseFloat($(link).offset().top) - $("header").innerHeight() + 2 
			})
		}else{
			location.href = $("#logo a").attr("href")+$(this).attr("href");
		}
	})

	/* STICKY MENU AUTO ACTIVE */
	setTimeout(function(){
		$("#menu a").each(function(){
			if ($($(this).attr("href")).length){
				var min = $($(this).attr("href")).position().top - $("header").innerHeight();
				var max = min + $($(this).attr("href")).height();

				$(this).parent().attr("data-min", parseInt(min));
				$(this).parent().attr("data-max", parseInt(max) - 10);
			}
		})
	}, 500)

	$(window).on("scroll", function(e){
		$("#menu a").each(function(){
			if ($(document).scrollTop() >= $(this).parent().data("min") && $(document).scrollTop() <= $(this).parent().data("max")){
				if (!$(this).parent().hasClass("active"))
					$("#menu li").removeClass("active");
				$(this).parent().addClass("active");
			}
		})
	});

	/* SCROLL POPIN */
	$(document).on("click", ".scroll a", function(e){
		e.preventDefault();

		var link = $(this).attr("href");
		var parent = $(this).parent().parent().parent();

		$("#popin").animate({
			scrollTop: parseFloat($(link.substring(link.indexOf("#"), 255)).position().top) - parent.innerHeight()
			//scrollTop: parseFloat($(link).position().top) - $(".modal .scroll").innerHeight()
		})
	})

	/* RECHERCHE */
	$("#menu ul + span").click(function(){
		$("#recherche").modal();
	})

	/* POPIN */
	$(".modal").off("scroll").on("scroll", function(e){
		if ($(this).find(".scroll").length){
			if ($(this).find(".scroll").hasClass("sticky")){
				if ($(this).scrollTop() < $(this).find(".scroll").attr("data-top")){
					$(this).removeClass("sticky");
					$(this).find(".scroll").removeClass("sticky");
				}
			}else{
				if ($(this).scrollTop() >= $(this).find(".scroll").position().top){
					$(this).find(".scroll").attr("data-top", $(this).find(".scroll").position().top);
					$(this).addClass("sticky");
					$(this).find(".scroll").addClass("sticky");
				}
			}
		}
	})

	$(document).on("click", ".ajax", function(e){
		e.preventDefault();

		var url = $(this).attr("href");

		$.get(url, function(reponse) {
			var html = $("<div>").html(reponse);
			$("#popin .modal-body").html(html.find("#ajax").html());
			$("#popin").modal();

			setTimeout(function(){
				$('#popin .modal-body .slider:not(.arrow)').slick({
					dots: true,
					arrows: false,
					autoplay: true
				});

				$('.selectpicker').selectpicker();
			}, 200);

			// Scroll
			if (url.indexOf("#") > 0){
				setTimeout(function(){
					$("#popin").animate({
						scrollTop: parseFloat($(url.substring(url.indexOf("#"), 255)).position().top) - $("#popin .modal-body .scroll").innerHeight()
					})
				}, 500);
			}
		})
	});

	/* FILTRES */
	$(".filtres a").click(function(e){
		e.preventDefault();

		$(this).parent().parent().find("li").removeClass("active");
		$(this).parent().addClass("active");

		$("#events.has-filtres .event").each(function(){
			$(this).parent().hide();
		});

		$("#events.has-filtres .event."+$(this).attr("data-filtre")).each(function(){
			$(this).parent().show();
		});
	})

	/* FORMULAIRE */
	$(document).on("submit", ".form-contact", function(e){
		var erreur = false;

		var email = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
		var string = /^([^0-9]*)$/;
		var number = /^([0-9]*)$/;

		$(".form-contact .form-control").each(function(){
			switch($(this).attr('name')){
				case "email":
					if ($.trim($(this).val()) == "" || !email.test($.trim($(this).val()))){
						$(this).addClass("error");
						erreur = true;
					}
					break;

				case "nom":
				case "prenom":
					if ($.trim($(this).val()) == "" || !string.test($.trim($(this).val()))){
						$(this).addClass("error");
						erreur = true;
					}
					break;

				case "telephone":
					if ($.trim($(this).val()) == "" || !number.test($.trim($(this).val()))){
						$(this).addClass("error");
						erreur = true;
					}
					break;

				default:
					if ($(this).attr('name') != undefined && $.trim($(this).val()) == ""){
						$(this).addClass("error");
						erreur = true;
					}
			}
		})

		$("form .form-control").off("focus").on("focus", function(){
			$(this).removeClass("error");
		})

		if (!erreur){
			return true;
		}else{
			return false;
		}
	})
});