/*!
 * pp-lightbox — dependency-free image gallery/lightbox for view pages.
 *
 * Any anchor marked data-pp-lb (href = full image URL) opens a full-screen
 * viewer. data-pp-lb-group scopes next/prev navigation (a field id, a record id,
 * or "page"). Optional data-pp-lb-cap = caption. Uses event delegation, so
 * markup rendered after load still works. Keyboard + swipe + wheel navigable.
 */
(function () {
  if (window.__ppLightbox) return;
  window.__ppLightbox = true;

  var overlay, imgEl, capEl, ctrEl, current = [], idx = 0;

  function build() {
    if (overlay) return;
    overlay = document.createElement('div');
    overlay.className = 'pp-lb-overlay';
    overlay.innerHTML =
      '<button class="pp-lb-close" aria-label="Close">&times;</button>' +
      '<button class="pp-lb-nav pp-lb-prev" aria-label="Previous">&#8249;</button>' +
      '<figure class="pp-lb-stage"><img class="pp-lb-img" alt=""><figcaption class="pp-lb-cap"></figcaption></figure>' +
      '<button class="pp-lb-nav pp-lb-next" aria-label="Next">&#8250;</button>' +
      '<div class="pp-lb-counter"></div>';
    document.body.appendChild(overlay);
    imgEl = overlay.querySelector('.pp-lb-img');
    capEl = overlay.querySelector('.pp-lb-cap');
    ctrEl = overlay.querySelector('.pp-lb-counter');
    overlay.querySelector('.pp-lb-close').addEventListener('click', close);
    overlay.querySelector('.pp-lb-prev').addEventListener('click', function (e) { e.stopPropagation(); step(-1); });
    overlay.querySelector('.pp-lb-next').addEventListener('click', function (e) { e.stopPropagation(); step(1); });
    overlay.addEventListener('click', function (e) { if (e.target === overlay || e.target.classList.contains('pp-lb-stage')) close(); });
    // swipe
    var sx = 0;
    overlay.addEventListener('touchstart', function (e) { sx = e.touches[0].clientX; }, { passive: true });
    overlay.addEventListener('touchend', function (e) { var dx = e.changedTouches[0].clientX - sx; if (Math.abs(dx) > 40) step(dx < 0 ? 1 : -1); });
  }

  function group(a) {
    var g = a.getAttribute('data-pp-lb-group');
    var all = Array.prototype.slice.call(document.querySelectorAll('[data-pp-lb]'));
    if (!g) return [a];
    return all.filter(function (el) { return el.getAttribute('data-pp-lb-group') === g; });
  }

  function show(i) {
    if (!current.length) return;
    idx = (i + current.length) % current.length;
    var a = current[idx];
    var src = a.getAttribute('href') || a.getAttribute('data-pp-lb') || '';
    imgEl.classList.remove('pp-lb-loaded');
    imgEl.src = src;
    imgEl.onload = function () { imgEl.classList.add('pp-lb-loaded'); };
    var cap = a.getAttribute('data-pp-lb-cap') || '';
    capEl.textContent = cap; capEl.style.display = cap ? '' : 'none';
    ctrEl.textContent = current.length > 1 ? (idx + 1) + ' / ' + current.length : '';
    var multi = current.length > 1;
    overlay.querySelector('.pp-lb-prev').style.display = multi ? '' : 'none';
    overlay.querySelector('.pp-lb-next').style.display = multi ? '' : 'none';
  }
  function step(d) { show(idx + d); }

  function open(a) {
    build();
    current = group(a);
    var i = current.indexOf(a); if (i < 0) { current = [a]; i = 0; }
    overlay.classList.add('pp-lb-on');
    document.documentElement.style.overflow = 'hidden';
    show(i);
    document.addEventListener('keydown', onKey);
  }
  function close() {
    if (!overlay) return;
    overlay.classList.remove('pp-lb-on');
    document.documentElement.style.overflow = '';
    imgEl.src = '';
    document.removeEventListener('keydown', onKey);
  }
  function onKey(e) {
    if (e.key === 'Escape') close();
    else if (e.key === 'ArrowRight') step(1);
    else if (e.key === 'ArrowLeft') step(-1);
  }

  document.addEventListener('click', function (e) {
    var a = e.target.closest && e.target.closest('[data-pp-lb]');
    if (!a) return;
    // only intercept image links
    e.preventDefault();
    open(a);
  });

  window.__ppLightboxInit = function () {}; // delegation-based; nothing to bind
})();
