/*!
 * georank-media.js — self-hosted, dependency-free rich media
 * Lightbox + Slideshow + Back-to-top + mobile nav toggle
 * Jeevan Organics / GeoRank sample build
 */
(function () {
  "use strict";

  var BACK_TO_TOP_ENABLED = true;

  /* ---------------- Mobile nav toggle ---------------- */
  function initNavToggle() {
    var btn = document.querySelector(".nav-toggle");
    var nav = document.querySelector(".site-nav");
    if (!btn || !nav) return;
    btn.addEventListener("click", function () {
      nav.classList.toggle("open");
      btn.setAttribute("aria-expanded", nav.classList.contains("open") ? "true" : "false");
    });
    nav.querySelectorAll("a").forEach(function (a) {
      a.addEventListener("click", function () { nav.classList.remove("open"); });
    });
  }

  /* ---------------- Lightbox ---------------- */
  var lightboxEl = null, lightboxGroup = [], lightboxIndex = 0;

  function buildLightbox() {
    if (lightboxEl) return lightboxEl;
    var el = document.createElement("div");
    el.className = "gm-lightbox";
    el.innerHTML =
      '<button class="gm-close" aria-label="Close">&times;</button>' +
      '<button class="gm-prev" aria-label="Previous">&#10094;</button>' +
      '<div style="display:flex;flex-direction:column;align-items:center;">' +
        '<img alt="">' +
        '<div class="gm-caption"></div>' +
      '</div>' +
      '<button class="gm-next" aria-label="Next">&#10095;</button>';
    document.body.appendChild(el);
    el.querySelector(".gm-close").addEventListener("click", closeLightbox);
    el.addEventListener("click", function (e) { if (e.target === el) closeLightbox(); });
    el.querySelector(".gm-prev").addEventListener("click", function () { showLightbox(lightboxIndex - 1); });
    el.querySelector(".gm-next").addEventListener("click", function () { showLightbox(lightboxIndex + 1); });
    lightboxEl = el;
    return el;
  }

  function showLightbox(idx) {
    if (!lightboxGroup.length) return;
    lightboxIndex = (idx + lightboxGroup.length) % lightboxGroup.length;
    var item = lightboxGroup[lightboxIndex];
    var img = lightboxEl.querySelector("img");
    img.src = item.href;
    img.alt = item.alt || "";
    lightboxEl.querySelector(".gm-caption").textContent = item.caption || "";
  }

  function openLightbox(group, startHref) {
    buildLightbox();
    lightboxGroup = group;
    var startIdx = 0;
    group.forEach(function (g, i) { if (g.href === startHref) startIdx = i; });
    lightboxEl.classList.add("open");
    document.body.style.overflow = "hidden";
    showLightbox(startIdx);
  }

  function closeLightbox() {
    if (!lightboxEl) return;
    lightboxEl.classList.remove("open");
    document.body.style.overflow = "";
  }

  function initLightbox() {
    var links = document.querySelectorAll(".georank-lightbox");
    if (!links.length) return;
    var groups = {};
    links.forEach(function (a) {
      var key = a.getAttribute("data-gallery") || "default";
      if (!groups[key]) groups[key] = [];
      var img = a.querySelector("img");
      groups[key].push({
        href: a.getAttribute("href") || (img ? img.src : ""),
        alt: img ? img.alt : "",
        caption: a.getAttribute("data-caption") || (img ? img.alt : "")
      });
      a.addEventListener("click", function (e) {
        e.preventDefault();
        openLightbox(groups[key], a.getAttribute("href") || (img ? img.src : ""));
      });
    });
    document.addEventListener("keydown", function (e) {
      if (!lightboxEl || !lightboxEl.classList.contains("open")) return;
      if (e.key === "Escape") closeLightbox();
      if (e.key === "ArrowLeft") showLightbox(lightboxIndex - 1);
      if (e.key === "ArrowRight") showLightbox(lightboxIndex + 1);
    });
  }

  /* ---------------- Slideshow ---------------- */
  function initSlideshow(root) {
    if (root.getAttribute("data-gm-init") === "1") return;
    root.setAttribute("data-gm-init", "1");
    var slides = root.querySelectorAll(".gs-slide");
    if (slides.length < 2) return;
    var current = 0, timer = null;

    var dotsWrap = document.createElement("div");
    dotsWrap.className = "gs-dots";
    slides.forEach(function (s, i) {
      var dot = document.createElement("button");
      dot.className = "gs-dot" + (i === 0 ? " active" : "");
      dot.setAttribute("aria-label", "Go to slide " + (i + 1));
      dot.addEventListener("click", function () { goTo(i); resetTimer(); });
      dotsWrap.appendChild(dot);
    });
    root.appendChild(dotsWrap);

    var prevBtn = document.createElement("button");
    prevBtn.className = "gs-arrow gs-prev";
    prevBtn.innerHTML = "&#10094;";
    prevBtn.setAttribute("aria-label", "Previous slide");
    var nextBtn = document.createElement("button");
    nextBtn.className = "gs-arrow gs-next";
    nextBtn.innerHTML = "&#10095;";
    nextBtn.setAttribute("aria-label", "Next slide");
    root.appendChild(prevBtn);
    root.appendChild(nextBtn);

    function goTo(i) {
      slides[current].classList.remove("active");
      dotsWrap.children[current].classList.remove("active");
      current = (i + slides.length) % slides.length;
      slides[current].classList.add("active");
      dotsWrap.children[current].classList.add("active");
    }
    function resetTimer() {
      if (timer) clearInterval(timer);
      timer = setInterval(function () { goTo(current + 1); }, 5000);
    }
    prevBtn.addEventListener("click", function () { goTo(current - 1); resetTimer(); });
    nextBtn.addEventListener("click", function () { goTo(current + 1); resetTimer(); });
    root.addEventListener("mouseenter", function () { if (timer) clearInterval(timer); });
    root.addEventListener("mouseleave", resetTimer);

    resetTimer();
  }

  function initAllSlideshows() {
    document.querySelectorAll(".georank-slideshow").forEach(initSlideshow);
  }

  /* ---------------- Back to top ---------------- */
  function initBackToTop() {
    if (!BACK_TO_TOP_ENABLED) return;
    var btn = document.createElement("button");
    btn.id = "gm-back-to-top";
    btn.setAttribute("aria-label", "Back to top");
    btn.innerHTML = "&#8593;";
    document.body.appendChild(btn);
    btn.addEventListener("click", function () {
      window.scrollTo({ top: 0, behavior: "smooth" });
    });
    window.addEventListener("scroll", function () {
      if (window.scrollY > window.innerHeight * 0.8) btn.classList.add("show");
      else btn.classList.remove("show");
    });
  }

  document.addEventListener("DOMContentLoaded", function () {
    initNavToggle();
    initLightbox();
    initAllSlideshows();
    initBackToTop();
  });

  window.GeorankMedia = { initSlideshows: initAllSlideshows };
})();
