/* LCSV — shared scripts: mobile menu, hero slider, newsletter PDF swap */

document.addEventListener('DOMContentLoaded', () => {
  initMobileMenu();
  initHeroSlider();
  initFooterYear();
});

/* --------- Mobile nav toggle --------- */
function initMobileMenu() {
  const toggle = document.getElementById('nav-toggle');
  const nav = document.getElementById('site-nav');
  if (!toggle || !nav) return;

  toggle.addEventListener('click', () => {
    const open = nav.classList.toggle('mobile-open');
    toggle.setAttribute('aria-expanded', open ? 'true' : 'false');
    toggle.innerHTML = open
      ? '<i class="fa-solid fa-xmark"></i>'
      : '<i class="fa-solid fa-bars"></i>';
  });

  nav.querySelectorAll('a').forEach(link => {
    link.addEventListener('click', () => {
      nav.classList.remove('mobile-open');
      toggle.setAttribute('aria-expanded', 'false');
      toggle.innerHTML = '<i class="fa-solid fa-bars"></i>';
    });
  });
}

/* --------- Hero slider --------- */
function initHeroSlider() {
  const slider = document.getElementById('hero-slider');
  if (!slider) return;

  const slides = slider.querySelectorAll('.slide');
  const dots = slider.querySelectorAll('.dot');
  const prev = slider.querySelector('.slider-prev');
  const next = slider.querySelector('.slider-next');

  if (!slides.length) return;

  let current = 0;
  let timer;
  const INTERVAL = 5500;

  const show = (i) => {
    slides.forEach((s, idx) => s.classList.toggle('active', idx === i));
    dots.forEach((d, idx) => {
      d.classList.toggle('active', idx === i);
      if (idx === i) {
        d.classList.add('w-9', 'bg-gold');
        d.classList.remove('w-3', 'bg-white/40');
      } else {
        d.classList.add('w-3', 'bg-white/40');
        d.classList.remove('w-9', 'bg-gold');
      }
    });
    current = i;
  };

  const advance = () => show((current + 1) % slides.length);
  const back = () => show((current - 1 + slides.length) % slides.length);

  const start = () => { timer = setInterval(advance, INTERVAL); };
  const stop = () => clearInterval(timer);

  dots.forEach((d, idx) => d.addEventListener('click', () => {
    stop(); show(idx); start();
  }));
  if (next) next.addEventListener('click', () => { stop(); advance(); start(); });
  if (prev) prev.addEventListener('click', () => { stop(); back(); start(); });

  slider.addEventListener('mouseenter', stop);
  slider.addEventListener('mouseleave', start);

  show(0);
  start();
}

/* --------- Footer year --------- */
function initFooterYear() {
  const y = document.getElementById('footer-year');
  if (y) y.textContent = new Date().getFullYear();
}
