<?php add_action('wp_footer', 'custom_js_for_representatives'); function custom_js_for_representatives() { if (is_page(325008)) { ?> <script defer> document.addEventListener("DOMContentLoaded", function () { const INTERVAL = 500; let userMarker = null; let defaultBounds = null; function haversineDistance(lat1, lng1, lat2, lng2) { const toRad = deg => deg * Math.PI / 180; const R = 6371e3; const �1 = toRad(lat1), �2 = toRad(lat2); const �� = toRad(lat2 - lat1); const �� = toRad(lng2 - lng1); const a = Math.sin(�� / 2) ** 2 + Math.cos(�1) * Math.cos(�2) * Math.sin(�� / 2) ** 2; const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); return R * c; } function initToggleCards(map, mapContainer) { const allCards = document.querySelectorAll(".js-retailer-card"); const allGridItems = document.querySelectorAll(".jet-listing-grid__item"); document.querySelectorAll(".js-toggle-retailer").forEach(toggle => { toggle.removeEventListener("click", toggle.__handler); const handler = function () { const currentCard = this.closest(".js-retailer-card"); const currentGridItem = currentCard.closest(".jet-listing-grid__item"); const currentBody = currentCard.querySelector(".retailer-body"); const backBtn = currentCard.querySelector(".js-back-btn"); allCards.forEach(card => { if (card !== currentCard) { card.style.display = "none"; card.classList.remove("open"); const body = card.querySelector(".retailer-body"); if (body) body.style.maxHeight = null; const btn = card.querySelector(".js-back-btn"); if (btn) btn.style.display = "none"; } }); allGridItems.forEach(item => { if (item !== currentGridItem) { item.style.display = "none"; } else { item.style.display = "block"; } }); currentCard.classList.add("open"); currentBody.style.maxHeight = currentBody.scrollHeight + "px"; if (backBtn) backBtn.style.display = "block"; const offset = mapContainer.getBoundingClientRect().top + window.scrollY; window.scrollTo({ top: offset - 100, behavior: 'smooth' }); const lat = parseFloat(currentCard.querySelector('.data-lat')?.textContent.trim()); const lng = parseFloat(currentCard.querySelector('.data-lng')?.textContent.trim()); if (!isNaN(lat) && !isNaN(lng)) { map.flyTo({ center: [lng, lat], zoom: 5, essential: true }); } }; toggle.__handler = handler; toggle.addEventListener("click", handler); }); document.querySelectorAll(".js-back-btn").forEach(button => { button.removeEventListener("click", button.__handler); const handler = function () { resetView(); }; button.__handler = handler; button.addEventListener("click", handler); }); } function resetView() { const allCards = document.querySelectorAll(".js-retailer-card"); const allGridItems = document.querySelectorAll(".jet-listing-grid__item"); allCards.forEach(card => { card.style.display = "block"; card.classList.remove("open"); const body = card.querySelector(".retailer-body"); if (body) body.style.maxHeight = null; const backBtn = card.querySelector(".js-back-btn"); if (backBtn) backBtn.style.display = "none"; }); allGridItems.forEach(item => { item.style.display = "block"; }); if (defaultBounds) { const mapContainer = document.querySelector('.jet-map-listing'); const map = jQuery(mapContainer).data('mapInstance'); if (map && typeof map.fitBounds === 'function') { map.fitBounds(defaultBounds, { padding: 60, maxZoom: 4 }); } } } function expandRetailerBodiesInPopups() { const popupCards = document.querySelectorAll(".mapboxgl-popup .js-retailer-card"); popupCards.forEach(card => { const body = card.querySelector(".retailer-body"); if (body) { card.classList.add("open"); body.style.maxHeight = body.scrollHeight + "px"; } }); } function handleUserLocation(map, markers, centerAndZoomToNearest = false, updateSearch = false) { if (!navigator.geolocation) { alert("A b�ng�szQd nem t�mogatja a helymeghat�roz�st."); return; } navigator.geolocation.getCurrentPosition( position => { const userLat = position.coords.latitude; const userLng = position.coords.longitude; if (userMarker) userMarker.remove(); const el = document.createElement('div'); el.style.backgroundColor = 'red'; el.style.width = '18px'; el.style.height = '18px'; el.style.borderRadius = '50%'; el.style.border = '2px solid white'; el.style.boxShadow = '0 0 4px rgba(0,0,0,0.5)'; userMarker = new mapboxgl.Marker(el) .setLngLat([userLng, userLat]) .addTo(map); if (centerAndZoomToNearest && Array.isArray(markers)) { let nearest = null; let shortestDistance = Infinity; markers.forEach(marker => { const lat = marker.latLang.lat; const lng = marker.latLang.lng; const dist = haversineDistance(userLat, userLng, lat, lng); if (dist < shortestDistance) { shortestDistance = dist; nearest = { lat, lng }; } }); if (nearest) { const bounds = new mapboxgl.LngLatBounds(); bounds.extend([userLng, userLat]); bounds.extend([nearest.lng, nearest.lat]); map.fitBounds(bounds, { padding: 60, maxZoom: 14 }); } else { map.flyTo({ center: [userLng, userLat], zoom: 7 }); } } if (updateSearch) { fetch(`https://nominatim.openstreetmap.org/reverse?format=json&lat=${userLat}&lon=${userLng}&accept-language=en`) .then(res => res.json()) .then(data => { const country = data?.address?.country; if (country) { const input = document.querySelector('input[name="searchWords"]'); const button = document.querySelector('.jet-search-filter__submit'); if (input) { input.value = country; input.dispatchEvent(new Event('input', { bubbles: true })); } if (button) { setTimeout(() => button.click(), 100); } } }); } }, error => alert("Helymeghat�roz�s sikertelen: " + error.message) ); } const waitForMapReady = setInterval(() => { const mapContainer = document.querySelector('.jet-map-listing'); if (!mapContainer) return; const $mapContainer = jQuery(mapContainer); const map = $mapContainer.data('mapInstance'); const markers = $mapContainer.data('markers'); if (map && typeof map.flyTo === 'function') { clearInterval(waitForMapReady); map.addControl(new mapboxgl.NavigationControl(), 'top-right'); defaultBounds = new mapboxgl.LngLatBounds(); markers.forEach(marker => { defaultBounds.extend([marker.latLang.lng, marker.latLang.lat]); }); handleUserLocation(map, markers, false); initToggleCards(map, mapContainer); expandRetailerBodiesInPopups(); const locationBtn = document.createElement('button'); locationBtn.innerText = '=��� Use my location'; locationBtn.className = 'mapboxgl-ctrl my-location-btn'; locationBtn.style.cssText = ` position: absolute; top: 20px; left: 10px; color:#000; z-index: 10; background: white; border: 1px solid #ccc; padding: 6px 10px; font-size: 14px; border-radius: 4px; cursor: pointer; box-shadow: 0 1px 4px rgba(0,0,0,0.3); `; mapContainer.appendChild(locationBtn); const resetBtn = document.createElement('button'); resetBtn.innerText = '=�� Reset'; resetBtn.className = 'mapboxgl-ctrl reset-location-btn'; resetBtn.style.cssText = ` position: absolute; top: 60px; left: 10px; color:#000; z-index: 10; background: white; border: 1px solid #ccc; padding: 6px 10px; font-size: 14px; border-radius: 4px; cursor: pointer; box-shadow: 0 1px 4px rgba(0,0,0,0.3); `; mapContainer.appendChild(resetBtn); document.querySelectorAll('.reset-location-btn').forEach(resetButton => { resetButton.addEventListener('click', () => { const input = document.querySelector('input[name="searchWords"]'); const button = document.querySelector('.jet-search-filter__submit'); if (input) { input.value = ''; input.dispatchEvent(new Event('input', { bubbles: true })); } if (button) { setTimeout(() => button.click(), 100); } resetView(); }); }); locationBtn.addEventListener('click', () => { handleUserLocation(map, markers, true, true); }); const observer = new MutationObserver(() => { setTimeout(() => { initToggleCards(map, mapContainer); expandRetailerBodiesInPopups(); }, 300); }); observer.observe(document.body, { childList: true, subtree: true }); } }, INTERVAL); }); </script> <?php } } ?>