/* site.js — behaviour only (header & footer are rendered server-side via PHP includes) */
(function () {
  /* sticky header style */
  var hdr = document.getElementById("hdr");
  if (hdr) {
    var onScroll = function () { hdr.classList.toggle("scrolled", window.scrollY > 12); };
    onScroll();
    window.addEventListener("scroll", onScroll, { passive: true });
  }

  /* mobile menu */
  var burger = document.getElementById("burger");
  var links = document.getElementById("navlinks");
  if (burger && links) {
    burger.addEventListener("click", function () {
      var open = links.classList.toggle("open");
      burger.setAttribute("aria-expanded", open);
    });
  }

  /* hero slideshow (only on pages that have one) */
  var show = document.getElementById("slideshow");
  if (show) {
    var slides = Array.prototype.slice.call(show.querySelectorAll(".slide"));
    var dotsWrap = document.getElementById("slideDots");
    var idx = 0, timer;
    var dots = [];
    var go = function (n) {
      slides[idx].classList.remove("is-active");
      dots[idx].classList.remove("active");
      idx = (n + slides.length) % slides.length;
      slides[idx].classList.add("is-active");
      dots[idx].classList.add("active");
    };
    dots = slides.map(function (_, i) {
      var b = document.createElement("button");
      b.setAttribute("aria-label", "Go to slide " + (i + 1));
      b.addEventListener("click", function () { go(i); restart(); });
      dotsWrap.appendChild(b);
      return b;
    });
    if (dots[0]) dots[0].classList.add("active");
    var reduce = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
    var restart = function () { clearInterval(timer); if (!reduce) timer = setInterval(function () { go(idx + 1); }, 5000); };
    restart();
  }

  /* reveal on scroll (progressive) */
  if ("IntersectionObserver" in window) {
    var io = new IntersectionObserver(function (entries) {
      entries.forEach(function (e) { if (e.isIntersecting) { e.target.classList.add("in"); io.unobserve(e.target); } });
    }, { threshold: 0.12 });
    document.querySelectorAll(".fade-up").forEach(function (el) { io.observe(el); });
  } else {
    document.querySelectorAll(".fade-up").forEach(function (el) { el.classList.add("in"); });
  }
})();
