import 'slick-carousel';

var mobile = false;
var containers_id  = ["projets-block_1","programmes-block_4","home_themes-block_1"];

$(function(){
	console.log("$ ready !!");
	
	if( navigator.userAgent.match(/Android/i) || navigator.userAgent.match(/webOS/i) || navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i) || navigator.userAgent.match(/iPod/i) || navigator.userAgent.match(/BlackBerry/i)){
		mobile = true;
	}

	if($(".path-frontpage").length > 0){

		// $("#"+).fadeIn();

		
		$('.last-news').slick({
			slidesToShow: 1,
			slidesToScroll: 1,
			centerMode: true,
			centerPadding: '300px',
			responsive: [
			{
				breakpoint: 800,
				settings: {
					centerPadding: '0',
				}
			  }]
		});
		$('.last-ressources').slick({
			slidesToShow: 3,
			slidesToScroll: 1,
			centerMode: true,
			centerPadding: '60px',
			infinite: true,
			responsive: [
				{
					breakpoint: 800,
					settings: {
						slidesToShow: 2,
						centerPadding: '0',
					}	
				},
				{
					breakpoint: 400,
					settings: {
						slidesToShow: 1,
						centerPadding: '0',
					}
				}]
		});
	}

	if($("#programme_toc-block_5").length > 0){
		$('#programme_toc-block_5>div>div').slick({
			slidesToShow: 1,
			slidesToScroll: 1,
			centerMode: true,
			centerPadding: '200px',
			responsive: [
				{
					breakpoint: 800,
					settings: {
						centerPadding: '0',
						adaptiveHeight: true
					}
				  }]
		});
	}
	
	$('#centre_de_ressources-block_2>div>div').slick({
		slidesToShow: 3,
  		slidesToScroll: 1,
		centerMode: false,
  		centerPadding: '60px',
		infinite: true,
		responsive: [
			{
				breakpoint: 800,
				settings: {
					slidesToShow: 1,
  					slidesToScroll: 1,
					centerPadding: '40px',
					centerMode: true,
				}
			  }]
	});

	if($(".path-actus").length > 0){
		let elem = $("#actus_blocks-block_2").find("footer").detach();
		$("#actus_blocks-block_2").append(elem);
		$('#actus_blocks-block_2>div>div').slick({
			slidesToShow: 3,
			slidesToScroll: 1,
			centerMode: false,
			centerPadding: '60px',
			infinite: false,
			responsive: [
				{
					breakpoint: 800,
					settings: {
						slidesToShow: 1,
						slidesToScroll: 1,
						centerMode: true,
						centerPadding: '30px',
					}
			}]
		});	
		elem = $("#actus_blocks-block_1").find("footer").detach();
		$("#actus_blocks-block_1").append(elem);
		$('#actus_blocks-block_1>div>div').slick({
			slidesToShow: 3,
			slidesToScroll: 1,
			centerMode: false,
			centerPadding: '60px',
			infinite: false,
			responsive: [
				{
					breakpoint: 800,
					settings: {
						slidesToShow: 1,
						slidesToScroll: 1,
						centerMode: true,
						centerPadding: '30px',
					}
			}]
			
		});	
		
		$('#home_blocks-block_3>div>div').slick({
			slidesToShow: 3,
			slidesToScroll: 1,
			centerMode: false,
			centerPadding: '60px',
			infinite: false,
			responsive: [
				{
					breakpoint: 800,
					settings: {
						slidesToShow: 1,
						slidesToScroll: 1,
						centerPadding: '0',
					}
			}]
		});
		
	}
	
	
	if($('.path-centre-de-ressources').length > 0){

		let elem = $(".content_container").find(".views-element-container header>div");
		$(elem[0]).slick({
			slidesToShow: 3,
			slidesToScroll: 1,
			centerMode: false,
			centerPadding: '60px',
			responsive: [
				{
					breakpoint: 800,
					settings: {
						slidesToShow: 1,
						slidesToScroll: 1,
						centerMode: true,
						centerPadding: '30px',
					}
				  }]
		});
	}
	

	$('.close-block').click(function(){
		$(this).parent().fadeOut();
		if(mobile == true){
			$("body").css("overflow","auto");
		}
	});
	$('.open-block').click(function(){
		$(this).prev().css("display","grid");
		if(mobile == true){
			$("body").css("overflow","hidden");
		}
	});
	
	$("body").on("click", ".header_container header", function(){

			$('.header_container').find("header").removeClass('active');
			$('.header_container').find("header .arrow").remove();
			$(this).append('<span class="arrow"><br>▼</span>');
			$(this).toggleClass('active');
			
			containers_id.forEach((element) => {
				$("#"+element).fadeOut();
			});
			var d = $(this).data("dom");
			$("#"+d).fadeIn(function(){

				if(d == "home_themes-block_1"){
					
					$("#home_themes-block_1>div>div").slick('refresh');
				}
			});

		
	});

	// HOME PAGE 
	if($(".path-frontpage").length > 0){
		
		let elem = "<div class='main_logo'></div>";
		$(".about .views-row").prepend(elem);

		var header_container = "<div class='header_container'></div>";
		
		$('#home_blocks-block_2').after(header_container);

		
		//reconstruction header 
		var header = $('.views-element-container').find("header");
		$(header).each((index , element) => {
			
			let parent_id =  $(header[index]).parents().eq(2).attr("id");
			let words = $(header[index]).text().trim().split(" ");
			let newcontent = words.join('<br>');
			$(header[index]).empty();
			$(header[index]).append(newcontent);
			$(header[index]).attr('data-dom', parent_id);
			let h = $(header[index]).clone();
			if(index == 0){
				$(h).append('<span class="arrow"><br>▼</span>');
				$(h).addClass("active");
			}
			$('.header_container').append(h);
			
		});
		$("#home_themes-block_1 header").remove();
		$("#home_themes-block_1>div>div").slick({
			slidesToShow: 5,
			slidesToScroll: 4,
			centerMode: false,
			// centerPadding: '10%',
			infinite: false,
			responsive: [
				{
					breakpoint: 800,
					settings: {
						slidesToShow: 2,
						slidesToScroll: 1,
						centerPadding: '10px',
					}
				  }]
		});

		
		containers_id.forEach((element) => {
			$("#"+element).fadeOut();
		});
		$("#"+$('.header_container').find(".active").data("dom")).fadeIn();
	}

	
	
	//ajout header motif in page static
	if($(".page-node-type-static").length > 0){
		let elem = "<div class='layout__region layout__region--top'></div>"
		$(".layout--threecol-25-50-25").prepend(elem);
	}
	//last-ressources empty item
	$(".last-ressources .views-field-field-programme .field-content").each(function(){

		if ($(this).children().length == 0) {
			$(this).append("POPSU")
			$(this).addClass("empty");
		}
		  
	});
	
//menu ouvertur sur page event et actu
if($(".page-node-type-evenement").length > 0 || $(".page-node-type-actualite").length > 0){

	$('.views-element-container header').click(function(){
		$(this).parent().find(".view-grouping").fadeToggle();
		$(this).parent().find(".views-row").fadeToggle();
	});

	$('.view-grouping-header').click(function(){
		$(this).parent().find(".view-grouping-content").slideToggle();
	});
}

if($(".page-node-type-programme").length > 0 || $(".page-node-type-projet").length > 0 || $(".page-node-type-theme").length > 0 ){
	

	$(".field_ressources_liees article").each(function(){
		let title = $(this).find("h2").clone();
		$(this).find("h2").remove();
		$(this).find("div").first().append(title);
		
	})

	$('.views-element-container header').click(function(){
		$(this).parent().find(".view-grouping").fadeToggle();
		$(this).parent().find(".views-row").fadeToggle();
	});

	$('.view-grouping-header').click(function(){
		$(this).parent().find(".view-grouping-content").slideToggle();
	});

	//test hauteur
	let hb = $(".block-region-second .body").height();
	let ttp = 0;
	$(".block-region-second .body p").each(function(){
		ttp = ttp + $(this).height() ;
	});
	if(hb < ttp + 30){
		$(".block-region-second .body").append('<footer><a class="more">Lire la suite</a></footer>');
	}else{
		$(".block-region-second .body").addClass("body_open");	
	}
	//reteste on windows resize 
	window.addEventListener('resize', function(){
		$(".block-region-second .body").removeClass("body_open");
		let hb = $(".block-region-second .body").height();
		let ttp = 0;
		$(".block-region-second .body p").each(function(){
			ttp = ttp + $(this).height() ;
		});
		if(hb < ttp+30){
			
			if($(".block-region-second .body footer").length == 0){
				
				$(".block-region-second .body").append('<footer><a class="more">Lire la suite</a></footer>');
			}	
		}else{
			if($(".block-region-second .body footer").length > 0){
				$(".block-region-second .body footer").remove();
				
			}
			$(".block-region-second .body").addClass("body_open");	
		}
		
	});
	let elem = "<div class='main_logo'></div>"
	
	if( $("body").hasClass("programme-7") || $("body").hasClass("programme-6") || $("body").hasClass("programme-27") ){
		if(mobile != true){
			$('#projets-block_2').prepend(elem);
		}else{
			$('#projets-block_2').append(elem);
			$(".main_logo").css("position","relative");
		}
	}else{
		$('#projets-block_2').append(elem);
		$(".main_logo").css("position","relative")
		
	}

	// ouverture et fermeture des paragraphes
	$(".field_titre").on("click", function(){
		let e = $(this).parent().find(".field_texte");
		$(e).toggleClass("crop");
		$(this).toggleClass("rotate");

	});

	$(".field_partenaires").parent().find("h2").on("click",  function(){
		$(this).toggleClass("rotate");
		if($(".field_partenaires").is(":visible")){
			$(".field_partenaires").fadeOut("slow");
		}else{
			$(".field_partenaires").fadeIn("slow", function(){
				$(this).css("display","grid")
			});
		}
	});

	$(".field_equipes").parent().find("h2").on("click",  function(){
		$(this).toggleClass("rotate");
		if($(".field_equipes").is(":visible")){
			$(".field_equipes").fadeOut("slow");
		}else{
			$(".field_equipes").fadeIn("slow", function(){
				$(this).css("display","grid")
			});
		}
	});

	$(".block-region-second .body").on("click", ".more", function(){
		
		$(".body").toggleClass("body_open");
		if($('.body_open').length > 0){
			$(this).empty().html("Fermer")
		}else{
			$(this).empty().html("Lire la suite")
		}
		
	});

	$(".layout__region--bottom .field_ressources_liees>div:nth-of-type(2)").slick({
		slidesToShow: 3,
		slidesToScroll: 1,
		centerMode: true,
		centerPadding: '60px',
		infinite: true,
		responsive: [
			{
				breakpoint: 800,
				settings: {
					slidesToShow: 2,
					centerPadding: '0',
				}	
			},
			{
				breakpoint: 400,
				settings: {
					slidesToShow: 1,
					centerPadding: '0',
				}
			}]
	});

};


//page about navigation : 
if($(".page-node-type-static").length > 0){
	$(".field_titre").on("click", function(){
		let elem = $(this).find('div:not([class])').html();
		$(".block-region-second").find(".field_titre").each(function(){
			let title = $(this).html();
			
			if($(".field_groupe_documents").length > 0){
				title = $($(title)[2]).html();
			}
			if(title == elem){
				$('html,body').animate({
					scrollTop: $(this).offset().top
				}, 'slow');
				return false;
			}
		});
	});
}

//hide empty or unclickable sidebar 
if($(".block-region-first").find("a").length == 0 ){
	if($(".page-node-type-static").length == 0 ){
		$(".block-region-first").hide();
	}
};

//crop text for collection 
// $(".block-region-third .views-field-description__value").addClass("hidden_text");
// $(".block-region-third .views-field-description__value").on("click", function(){
// 	$(this).removeClass('hidden_text');
// })

});
