/* Grand Views Homes — interactions */
(function () {
  "use strict";

  /* ---- navbar scrolled state ---- */
  var nav = document.querySelector(".nav");
  function onScroll() {
    if (!nav) return;
    nav.classList.toggle("scrolled", window.scrollY > 40);
  }
  window.addEventListener("scroll", onScroll, { passive: true });
  onScroll();

  /* ---- mobile menu ---- */
  var burger = document.querySelector(".nav-burger");
  var menu = document.querySelector(".mobile-menu");
  function closeMenu() { if (menu) menu.classList.remove("open"); }
  if (burger && menu) {
    burger.addEventListener("click", function () { menu.classList.add("open"); });
    menu.querySelectorAll("a, .close").forEach(function (el) {
      el.addEventListener("click", closeMenu);
    });
  }

  /* ---- reveal on scroll ---- */
  var revealEls = document.querySelectorAll(".reveal");
  if ("IntersectionObserver" in window && revealEls.length) {
    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, rootMargin: "0px 0px -40px 0px" });
    revealEls.forEach(function (el) { io.observe(el); });
  } else {
    revealEls.forEach(function (el) { el.classList.add("in"); });
  }

  /* ---- theme toggle ---- */
  var KEY = "gvh-theme";
  var root = document.documentElement;
  function readTheme() { try { return localStorage.getItem(KEY); } catch (e) { return null; } }
  function writeTheme(v) { try { localStorage.setItem(KEY, v); } catch (e) {} }
  function applyTheme(t) {
    root.setAttribute("data-theme", t);
    document.querySelectorAll(".theme-toggle i").forEach(function (i) {
      i.className = t === "dark" ? "bi bi-sun" : "bi bi-moon-stars";
    });
  }
  var prefersDark = window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches;
  applyTheme(readTheme() || (prefersDark ? "dark" : "light"));
  document.addEventListener("click", function (e) {
    var btn = e.target.closest(".theme-toggle");
    if (!btn) return;
    var next = root.getAttribute("data-theme") === "dark" ? "light" : "dark";
    applyTheme(next); writeTheme(next);
  });

  /* ---- enquiry modal ---- */
  var overlay = document.getElementById("enquiryModal");
  function openModal(unit) {
    if (!overlay) return;
    overlay.classList.add("open");
    document.body.style.overflow = "hidden";
    var sel = overlay.querySelector('[name="interest"]');
    if (unit && sel) {
      for (var i = 0; i < sel.options.length; i++) {
        if (sel.options[i].value === unit) { sel.selectedIndex = i; break; }
      }
    }
  }
  function closeModal() {
    if (!overlay) return;
    overlay.classList.remove("open");
    document.body.style.overflow = "";
  }
  document.addEventListener("click", function (e) {
    var opener = e.target.closest("[data-modal-open]");
    if (opener) { e.preventDefault(); openModal(opener.getAttribute("data-unit") || ""); return; }
    if (e.target.closest("[data-modal-close]")) { closeModal(); return; }
    if (overlay && e.target === overlay) { closeModal(); }
  });
  document.addEventListener("keydown", function (e) {
    if (e.key !== "Escape") return;
    if (overlay && overlay.classList.contains("open")) { closeModal(); }
    else { closePanel(); }
  });

  /* ---- property detail panels (one-page overlay) ---- */
  var panels = {};
  document.querySelectorAll(".detail-panel").forEach(function (p) {
    panels[p.id.replace("detail-", "")] = p;
  });
  function openPanel(key) {
    var p = panels[key];
    if (!p) return;
    closeMenu();
    p.classList.add("open");
    p.setAttribute("aria-hidden", "false");
    document.body.classList.add("panel-open");
    p.scrollTop = 0;
    // ensure all reveal blocks inside the panel are shown
    p.querySelectorAll(".reveal").forEach(function (el) { el.classList.add("in"); });
    try { history.replaceState(null, "", "#" + key); } catch (e) {}
  }
  function closePanel() {
    var open = document.querySelector(".detail-panel.open");
    if (!open) return;
    open.classList.remove("open");
    open.setAttribute("aria-hidden", "true");
    document.body.classList.remove("panel-open");
    try { history.replaceState(null, "", location.pathname + location.search); } catch (e) {}
    var anchor = document.getElementById("properties");
    if (anchor) anchor.scrollIntoView({ block: "start" });
  }
  document.addEventListener("click", function (e) {
    var trigger = e.target.closest("[data-detail]");
    if (trigger) { e.preventDefault(); openPanel(trigger.getAttribute("data-detail")); return; }
    if (e.target.closest("[data-detail-close]")) { e.preventDefault(); closePanel(); return; }
  });
  // deep-link support: open a panel if the URL already has its hash
  (function () {
    var h = (location.hash || "").replace("#", "");
    if (h && panels[h]) { openPanel(h); }
  })();

  /* ---- simple form handling (front-end confirmation only) ---- */
  document.querySelectorAll("form[data-enquiry]").forEach(function (form) {
    form.addEventListener("submit", function (e) {
      e.preventDefault();
      var note = form.querySelector(".form-note");
      if (note) note.classList.add("show");
      form.reset();
    });
  });
})();
