/*
    Author: Johan HAUDIQUET / O. Massot
    Version: 1.0
*/
 

/*============================
    TABLE DES MATIERES
==============================
 :: SWITCH COULEURS
 :: AFFICHAGE SLIDER
 :: AFFICHAGE RESEAU
============================*/


/*============================
    Slick carousels
============================*/
$('.carousel').slick({
	arrows: true,
	dots: true,
	autoplay: true,
	autoplaySpeed: 2400,
	draggable: false,
	speed: 700
});


/*============================
    Leaflet maps
============================*/
$(document).ready(function(){

	function showMap(mapDiv) {

		let mapId = $(mapDiv).attr("id");
		if (!mapId) {
			console.error('missing id attribute for map');
			return;
		}

		// Collect the data from th map children <i>
		var items = [];
		mapDiv.children('.item-geodata').each(function () {
			let item = {
				id: $(this).data('id'),
				long: $(this).data('long'),
				lat: $(this).data('lat'),
				label: $(this).data('label')
			};
			items.push(item);
		});

		// Instanciate the map object  @see https://leafletjs.com/reference-1.6.0.html#map-factory
		var mapOptions = {scrollWheelZoom: false};
		var initialZoom = 13;
		var map = L.map(mapId, mapOptions);
		if (items.length > 0) {
			map.setView([items[0].lat, items[0].long], initialZoom);
		} else {
			map.setView([46.3399276, 2.6067229], 5);
		}

		// Add the tile layer
		// L.tileLayer('https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}', {
		//     maxZoom: 18,
		//     id: 'mapbox/streets-v11',
		//     tileSize: 512,
		//     // zoomOffset: -1,
		//     attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
		//     accessToken: 'pk.eyJ1Ijoib2xpdmllci1tYXNzb3QiLCJhIjoiY2s5OGl1M2cxMWNpajNscnV4Zm5maWY3eSJ9.YDESFgB-JuAhplTzXI6hGQ',
		// }).addTo(map);
		L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
			attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
		}).addTo(map);

		// Collect the event geodata to create the markers
		let markers = []
		items.forEach(function (item) {
			let marker = L.marker([item.lat, item.long]).addTo(map);
			marker.bindPopup(item.label);
			markers.push(marker);
		});

		// Set the view
		if (items.length > 0) {
			var markersGroup = new L.featureGroup(markers);
			map.fitBounds(markersGroup.getBounds());
			map.zoomSnap = 1;
			map.zoomOut();
		}
	}


	// Display map on events index page
	if ($('.ot-all-events #event-map').length) {
		showMap($('#event-map').first());
	}

	// Display map on event's details page
	if ($('.ot-show-event #event-map').length) {
		showMap($('#event-map').first());
	}

	// Display map on network structures page
	if ($('.ot-structures #structure-map').length) {
		showMap($('#structure-map').first());
	}

	// Automatically open popups if they have the display-modal class
    //	(the only/dirty way I found to force the opening of the popups is to trgigger the click on the link...)
	$('.trigger-on-load').trigger('click');

});


/*============================
    Defer img loading
============================*/

$(window).bind("load", function() {
	$('img.defer').each(function() {
		$(this).attr("src", $(this).data('src'));
	});
});
