import 'slick-carousel';

$(function(){
	console.log("$ ready !!");
	if($(".path-frontpage").length > 0){
		$('.last-news').slick({
			slidesToShow: 1,
			slidesToScroll: 1,
			centerMode: true,
			centerPadding: '300px',
		});
		$('.last-ressources').slick({
			slidesToShow: 3,
			slidesToScroll: 1,
			centerMode: true,
			centerPadding: '60px',
			infinite: true,
		});
	}

	if($("#programme_toc-block_5").length > 0){
		$('#programme_toc-block_5>div>div').slick({
			slidesToShow: 1,
			slidesToScroll: 1,
			centerMode: true,
			centerPadding: '200px',
		});
	}
	
	$('#centre_de_ressources-block_2 .item-list ul').slick({
		slidesToShow: 3,
  		slidesToScroll: 1,
		centerMode: true,
  		centerPadding: '60px',
		infinite: false,
	});

	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,
		});	
		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,
		});	
		
		$('#home_blocks-block_3>div>div').slick({
			slidesToShow: 3,
			slidesToScroll: 1,
			centerMode: false,
			centerPadding: '60px',
			infinite: false,
		});
		
	}
	
	
	if($('.path-centre-de-ressources').length > 0){

		let elem = $(".content_container").find(".views-element-container header>div");
		$(elem[0]).slick({
			slidesToShow: 1,
			slidesToScroll: 1,
			centerMode: true,
			centerPadding: '100px'
		});
	}
	

	$('.close-block').click(function(){
		$(this).parent().fadeOut();
	});
	$('.open-block').click(function(){
		$(this).prev().css("display","grid");
	});
	
	$('.views-element-container header').click(function(){
		let row = $('.views-element-container').find(".views-row");
		let id = ['programmes-block_4','home_themes-block_1','villes-block_1'];
		if($('.path-frontpage').length > 0){
			$(row).each(element => {
				
				if($(row[element]).is(":visible") &&  id.indexOf($(row[element]).parents().eq(2).attr("id")) != -1){
					$(row[element]).fadeOut();
				}
			});
			$('.views-element-container').find("header").removeClass('active');
			$('.views-element-container').find("header .arrow").remove();
			$(this).append('<span class="arrow"><br>▼</span>');
			$(this).toggleClass('active');

			$(".home_info_container").fadeOut();
			if($(this).parents().eq(2).attr("id") == "programmes-block_4" ){
				$("#home_prog_container").fadeIn();
			}
			if($(this).parents().eq(2).attr("id") == "home_themes-block_1" ){
				$("#home_theme_container").find(".views-row").css("opacity","0");
				$("#home_theme_container").fadeIn(function(){
					$("#home_theme_container").slick('refresh');
					$("#home_theme_container").find(".views-row").css("opacity","1");
				});	
			}
			if($(this).parents().eq(2).attr("id") == "villes-block_1" ){
				//console.log("squik")
				$("#home_map_container").fadeIn();
			}

		}
		if($('.page-node-type-programme').length > 0){
			$(this).parent().find(".views-row").fadeToggle();
		}

		
	})

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

		//reconstruction header 
		var header = $('.views-element-container').find("header");

		$(header).each(element => {
			let words = $(header[element]).text().trim().split(" ");
			let newcontent = words.join('<br>');
			console.log(words, newcontent);
			$(header[element]).empty();
			$(header[element]).append(newcontent);
			
		});
		$('#programmes-block_4').after('<div id="home_map_container" class="home_info_container"></div><div id="home_theme_container" class="home_info_container"></div><div id="home_prog_container" class="home_info_container"></div>');
		//populate info container
		 let inner_rows = $('#home_themes-block_1').find(".views-row").clone();
		 $("#home_theme_container").empty().append(inner_rows);
		 $("#home_theme_container").slick({
			slidesToShow: 5,
			slidesToScroll: 4,
			centerMode: true,
			// centerPadding: '10%',
			infinite: false,
		});
		 inner_rows = $('#programmes-block_4').find(".views-row").clone();
		 $("#home_prog_container").empty().append(inner_rows);
		 $('#programmes-block_4 header').append('<span class="arrow"><br>▼</span>').addClass("active");
		 
		 let map = $('#villes-block_1').find("#leaflet-map-view-villes-block-1").clone();
		 $("#home_map_container").empty().append(map);	 
	}
	
	//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");
		}
		  
	});
	
//// PAGE POPSU ////
// projets-block_2
if($(".page-node-type-programme").length > 0){
	$(".body").append('<footer><a class="more">Lire la suite</a></footer>');

	let elem = "<div class='main_logo'></div>"
	$('#projets-block_2').prepend(elem);

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

	});

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

}
	
});



// $(document).on('DOMNodeInserted', function(e) {
// 	// console.log(e.target);
//     if ( $(e.target).attr("id") == "home_theme_container" ) {

//     }
// });


