/* Nekochan Gallery — lightbox + keyboard nav */ (function() { 'use strict'; const lb = document.getElementById('lightbox'); const lbImg = document.getElementById('lb-img'); const lbCap = document.getElementById('lb-caption'); const lbClose = document.getElementById('lb-close'); const lbPrev = document.getElementById('lb-prev'); const lbNext = document.getElementById('lb-next'); if (!lb) return; let items = []; let current = 0; function open(index) { current = index; const item = items[current]; lbImg.src = item.full; lbImg.alt = item.caption; lbCap.textContent = item.caption; lb.classList.add('active'); document.body.style.overflow = 'hidden'; lbPrev.style.visibility = current > 0 ? 'visible' : 'hidden'; lbNext.style.visibility = current < items.length - 1 ? 'visible' : 'hidden'; } function closeLb() { lb.classList.remove('active'); document.body.style.overflow = ''; lbImg.src = ''; } function prev() { if (current > 0) open(current - 1); } function next() { if (current < items.length - 1) open(current + 1); } // Build item list from photo grid document.querySelectorAll('.photo-thumb-link').forEach(function(a, i) { items.push({ full: a.dataset.full, caption: a.dataset.caption || '' }); a.addEventListener('click', function(e) { e.preventDefault(); open(i); }); }); lbClose.addEventListener('click', closeLb); lbPrev.addEventListener('click', prev); lbNext.addEventListener('click', next); lb.addEventListener('click', function(e) { if (e.target === lb) closeLb(); }); document.addEventListener('keydown', function(e) { if (!lb.classList.contains('active')) return; if (e.key === 'Escape') closeLb(); if (e.key === 'ArrowLeft') prev(); if (e.key === 'ArrowRight') next(); }); // Touch swipe support let touchStartX = null; lb.addEventListener('touchstart', function(e) { touchStartX = e.touches[0].clientX; }, { passive: true }); lb.addEventListener('touchend', function(e) { if (touchStartX === null) return; const dx = e.changedTouches[0].clientX - touchStartX; if (Math.abs(dx) > 50) { dx < 0 ? next() : prev(); } touchStartX = null; }, { passive: true }); })();