document.addEventListener('DOMContentLoaded', function(){
  if (window.AOS) AOS.init({duration:650, once:true, offset:70});
  if (window.Swiper) {
    document.querySelectorAll('.mj-swiper').forEach(function(el){
      new Swiper(el, {slidesPerView:1.15, spaceBetween:16, pagination:{el:el.querySelector('.swiper-pagination'), clickable:true}, breakpoints:{768:{slidesPerView:2.4}, 1200:{slidesPerView:3.4}}});
    });
  }
  document.querySelectorAll('[data-mj-map]').forEach(function(el){
    if (!window.L) return;
    const lat = parseFloat(el.dataset.lat || '1.3521');
    const lng = parseFloat(el.dataset.lng || '103.8198');
    const map = L.map(el, {scrollWheelZoom:false}).setView([lat,lng], 11);
    L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {attribution:'&copy; OpenStreetMap'}).addTo(map);
    L.marker([lat,lng]).addTo(map);
  });
});
async function loadStates(countryId, targetId){
  const target=document.getElementById(targetId); if(!target) return;
  target.innerHTML='<option value="">Loading...</option>';
  const base=(window.MJ_BASE_URL||'').replace(/\/$/,'');
  const r=await fetch(base+'/ajax/states.php?country_id='+encodeURIComponent(countryId));
  target.innerHTML=await r.text();
}
async function loadCities(stateId, targetId){
  const target=document.getElementById(targetId); if(!target) return;
  target.innerHTML='<option value="">Loading...</option>';
  const base=(window.MJ_BASE_URL||'').replace(/\/$/,'');
  const r=await fetch(base+'/ajax/cities.php?state_id='+encodeURIComponent(stateId));
  target.innerHTML=await r.text();
}
if('serviceWorker' in navigator){
  const base=(window.MJ_BASE_URL||'').replace(/\/$/,'');
  navigator.serviceWorker.register(base+'/service-worker.js').catch(()=>{});
}
