/* Marketing site v4 — lightbox + interactions */
(function () {
  "use strict";

  var yearEl = document.getElementById("y");
  if (yearEl) yearEl.textContent = String(new Date().getFullYear());

  // Sticky nav
  var nav = document.getElementById("nav");
  function onScroll() {
    if (!nav) return;
    nav.classList.toggle("is-scrolled", window.scrollY > 8);
  }
  onScroll();
  window.addEventListener("scroll", onScroll, { passive: true });

  // Mobile menu
  var burger = document.getElementById("burger");
  var menu = document.getElementById("menu");
  if (burger && menu) {
    burger.addEventListener("click", function () {
      var open = menu.classList.toggle("is-open");
      burger.setAttribute("aria-expanded", open ? "true" : "false");
    });
    menu.querySelectorAll("a").forEach(function (link) {
      link.addEventListener("click", function () {
        menu.classList.remove("is-open");
        burger.setAttribute("aria-expanded", "false");
      });
    });
  }

  // Chat panel
  var fab = document.getElementById("chatFab");
  var panel = document.getElementById("chatPanel");
  var closeBtn = document.getElementById("chatClose");
  if (fab && panel) {
    fab.addEventListener("click", function () {
      panel.hidden = !panel.hidden;
    });
  }
  if (closeBtn && panel) {
    closeBtn.addEventListener("click", function () {
      panel.hidden = true;
    });
  }

  // FAQ: one open
  document.querySelectorAll(".faq-item").forEach(function (item) {
    item.addEventListener("toggle", function () {
      if (!item.open) return;
      document.querySelectorAll(".faq-item").forEach(function (other) {
        if (other !== item) other.open = false;
      });
    });
  });

  // ——— Lightbox: click ảnh → xem ảnh gốc full ———
  var lb = document.getElementById("lightbox");
  var lbImg = document.getElementById("lbImg");
  var lbCap = document.getElementById("lbCap");
  var lbCounter = document.getElementById("lbCounter");
  var lbOpen = document.getElementById("lbOpen");
  var lbPrev = document.getElementById("lbPrev");
  var lbNext = document.getElementById("lbNext");
  if (!lb || !lbImg) return;

  var gallery = [];
  var current = 0;

  function collectGallery() {
    gallery = Array.prototype.slice.call(document.querySelectorAll("img.zoomable")).map(function (img) {
      return {
        src: img.getAttribute("data-full") || img.currentSrc || img.src,
        alt: img.alt || "",
        el: img,
      };
    });
  }

  function show(index) {
    if (!gallery.length) return;
    current = (index + gallery.length) % gallery.length;
    var item = gallery[current];
    lbImg.src = item.src;
    lbImg.alt = item.alt;
    if (lbCap) lbCap.textContent = item.alt || "Ảnh sản phẩm";
    if (lbCounter) lbCounter.textContent = current + 1 + " / " + gallery.length;
    if (lbOpen) {
      lbOpen.href = item.src;
      lbOpen.setAttribute("download", "");
    }
  }

  function openAt(index) {
    collectGallery();
    if (!gallery.length) return;
    show(index);
    lb.hidden = false;
    document.body.classList.add("lb-open");
    // close chat if open
    if (panel) panel.hidden = true;
    try {
      lb.querySelector(".lightbox-close").focus();
    } catch (e) {}
  }

  function closeLb() {
    lb.hidden = true;
    document.body.classList.remove("lb-open");
    lbImg.removeAttribute("src");
  }

  function indexOfImg(img) {
    collectGallery();
    for (var i = 0; i < gallery.length; i++) {
      if (gallery[i].el === img) return i;
    }
    // fallback by src
    var src = img.getAttribute("data-full") || img.src;
    for (var j = 0; j < gallery.length; j++) {
      if (gallery[j].src === src || gallery[j].src.indexOf(src) !== -1) return j;
    }
    return 0;
  }

  // Click media-card or zoomable
  document.addEventListener("click", function (e) {
    var card = e.target.closest(".media-card");
    if (card) {
      var img = card.querySelector("img.zoomable");
      if (img) {
        e.preventDefault();
        openAt(indexOfImg(img));
      }
      return;
    }
    var z = e.target.closest("img.zoomable");
    if (z) {
      e.preventDefault();
      openAt(indexOfImg(z));
    }
  });

  // Keyboard on cards
  document.addEventListener("keydown", function (e) {
    if (e.key === "Enter" || e.key === " ") {
      var card = e.target.closest && e.target.closest(".media-card");
      if (card && document.activeElement === card) {
        e.preventDefault();
        var img = card.querySelector("img.zoomable");
        if (img) openAt(indexOfImg(img));
      }
    }
  });

  // Close / nav controls
  lb.addEventListener("click", function (e) {
    if (e.target.closest("[data-lb-close]")) {
      closeLb();
    }
  });
  if (lbPrev) {
    lbPrev.addEventListener("click", function (e) {
      e.stopPropagation();
      show(current - 1);
    });
  }
  if (lbNext) {
    lbNext.addEventListener("click", function (e) {
      e.stopPropagation();
      show(current + 1);
    });
  }

  document.addEventListener("keydown", function (e) {
    if (lb.hidden) return;
    if (e.key === "Escape") closeLb();
    if (e.key === "ArrowLeft") show(current - 1);
    if (e.key === "ArrowRight") show(current + 1);
  });
})();
