let mapElements=document.getElementsByClassName("map-element");let mobileCheck=window.matchMedia("(max-width: 575px)");let mobileOpenOverlay=true;let overlaysFetched=false;let showMapOverlay=function(map,mapElement){mapElement.classList.add("overlay");map.dragging.disable();map.touchZoom.disable();map.scrollWheelZoom.disable();}
let hideMapOverlay=function(map,mapElement){mapElement.classList.remove("overlay");map.dragging.enable();map.touchZoom.enable();map.scrollWheelZoom.enable();}
for(let mapElement of mapElements){const url=mapElement.dataset.url;const itemCollectionId=mapElement.dataset.collection;const centerLat=mapElement.dataset.centerLat;const centerLon=mapElement.dataset.centerLon;const corner1=L.latLng(58.66382791782926,-8.164376845626853);const corner2=L.latLng(50.020644284630606,3.9578247070312504);const mapNavigationNameElement=mapElement.nextElementSibling.querySelector("span.map-info-name");const mapNavigationInfoButtonElement=mapElement.nextElementSibling.querySelector(".map-info-button");const mapNavigationPreviousButtonElement=mapElement.nextElementSibling.querySelector("input[type=image]:first-of-type");const mapNavigationNextButtonElement=mapElement.nextElementSibling.querySelector("input[type=image]:last-of-type");const mapOverlayNameElement=mapElement.querySelector("h3.map-info-name");const mapOverlayContentElement=mapElement.querySelector(".map-overlay-content");const mapOverlayIconElement=mapElement.querySelector(".map-overlay-icon");const mapOverlayTextElement=mapElement.querySelector(".map-overlay-text");const mapOverlayCloseElement=mapElement.querySelector(".map-overlay-close");let mapItems=[];let currentZoom=9;let currentIndex=38;let currentOverlayImage=undefined;let map=L.map(mapElement,{minZoom:8,maxZoom:10,maxBounds:L.latLngBounds(corner1,corner2),maxBoundsViscosity:1.0,zoomControl:false,doubleClickZoom:false});L.tileLayer(url,{tms:1}).addTo(map);L.control.zoom({position:"bottomright"}).addTo(map);map.setView([centerLat,centerLon],9);let highlightOverlay=function(index){if(!overlaysFetched)return;if(currentOverlayImage)
currentOverlayImage.classList.remove("highlight");const mapItem=mapItems[index];mapItem.overlay._image.classList.add("highlight");mapNavigationNameElement.innerText=mapItem.item.N;mapOverlayNameElement.innerText=mapItem.item.N;currentOverlayImage=mapItem.overlay._image;mapOverlayIconElement.src="/map/markers/"+mapItem.item.I+".webp";mapOverlayIconElement.style.display=mapItem.item.E?"block":"none";mapOverlayTextElement.innerHTML="";const lat=mapItem.overlay._bounds._northEast.lat-((mapItem.overlay._bounds._northEast.lat-mapItem.overlay._bounds._southWest.lat)/2);const lon=mapItem.overlay._bounds._northEast.lng-(Math.abs(mapItem.overlay._bounds._northEast.lng-mapItem.overlay._bounds._southWest.lng)/2);map.setView([lat,lon],map.getZoom());mapItem.item.D.forEach((d)=>{const p=document.createElement("p");const span1=document.createElement("span");const span2=document.createElement("span");span1.innerText=d.substring(0,1);span2.innerText=d.substring(1);p.appendChild(span1);p.appendChild(span2);mapOverlayTextElement.appendChild(p);});setTimeout(function(){mapOverlayContentElement.scroll({top:0});},100);}
let overlayClickHandler=function(event,item,index){highlightOverlay(index);currentIndex=index;if(!mobileOpenOverlay&&mobileCheck.matches)return;showMapOverlay(map,mapElement);}
mapNavigationPreviousButtonElement.addEventListener("click",function(){do{currentIndex=currentIndex-1;if(currentIndex===-1)currentIndex=mapItems.length-1;}while(currentIndex>0&&mapItems[currentIndex].item.N===mapItems[currentIndex-1].item.N);highlightOverlay(currentIndex);});mapNavigationNextButtonElement.addEventListener("click",function(){let currentName=mapItems[currentIndex].item.N;do{currentIndex=(currentIndex+1)%mapItems.length;highlightOverlay(currentIndex);}while(mapItems[currentIndex].item.N===currentName);});mapOverlayCloseElement.addEventListener("click",function(){hideMapOverlay(map,mapElement);});mapNavigationInfoButtonElement.addEventListener("click",function(){showMapOverlay(map,mapElement);});let mapZoomHandler=function(){mapElement.classList.remove("zoom-"+currentZoom);currentZoom=map.getZoom();mapElement.classList.add("zoom-"+currentZoom);}
map.on("zoomend",mapZoomHandler);mapZoomHandler();let windowResizeHandler=function(){highlightOverlay(currentIndex);};window.addEventListener("resize",windowResizeHandler);let mapClickHandler=(event)=>{if(mapElement.classList.contains("overlay")&&event.originalEvent.target.classList.contains("map-element"))
hideMapOverlay(map,mapElement);console.log(event.latlng);}
map.on("click",mapClickHandler);let fetchMapOverlays=function(){if(overlaysFetched)return;fetch("/umbraco/api/MapItems/GetAllMapItems/"+itemCollectionId+"?culture="+document.body.dataset.culture).then((response)=>{response.json().then(collections=>{for(let i=0;i<collections.length;++i){let collection=collections[i];for(let j=0;j<collection.C.length;++j){let item=collections[i].C[j];let bounds=L.latLngBounds([item.C[0],item.C[1]],[item.C[2],item.C[3]]);let overlay=L.imageOverlay("/map/markers/"+item.I+".webp",bounds,{interactive:true,zIndex:2});let index=mapItems.length;mapItems.push({overlay:overlay,item:item});overlay.addTo(map).addEventListener("click",(event)=>{overlayClickHandler(event,item,index);});}}
windowResizeHandler();if(!mobileCheck.matches)showMapOverlay(map,mapElement);});});}
let mapSection=mapElement.closest("section");let windowScrollHandler=function(){if(mapSection.getBoundingClientRect().top<window.innerHeight&&itemCollectionId){fetchMapOverlays();overlaysFetched=true;}}
window.addEventListener("scroll",windowScrollHandler);windowScrollHandler();};;
