'use strict';
let lbIndex = 0;
function openLightbox(i) {
lbIndex = i;
renderLightbox();
document.getElementById('lightbox').classList.remove('hidden');
document.body.style.overflow = 'hidden';
document.addEventListener('keydown', lbKey);
}
function closeLightbox() {
document.getElementById('lightbox').classList.add('hidden');
document.body.style.overflow = '';
document.removeEventListener('keydown', lbKey);
['lb-video', 'lb-audio'].forEach(id => {
const el = document.getElementById(id);
el.pause && el.pause();
});
history.replaceState(null, '', location.pathname + location.search);
}
function lbNav(delta) {
let next = lbIndex + delta;
while (next >= 0 && next < ENTRIES.length && !ENTRIES[next].type) next += delta;
if (next >= 0 && next < ENTRIES.length) {
lbIndex = next;
renderLightbox();
}
}
function lbToggleInfo() {
document.getElementById('lb-info-panel').classList.toggle('hidden');
}
function lbBuildInfo(e) {
const rows = [];
if (e.name !== e.title) rows.push(['File', e.name]);
if (e.taken_at) {
const d = new Date(e.taken_at.replace(/[+-]\d{2}:\d{2}$|Z$/, ''));
rows.push(['Date', d.toLocaleString(undefined, { dateStyle: 'medium', timeStyle: 'short' })]);
}
if (e.width && e.height) rows.push(['Dimensions', `${e.width} × ${e.height}`]);
document.getElementById('lb-info-dl').innerHTML =
rows.map(([k, v]) => `
${k}${v}`).join('');
document.getElementById('lb-info-panel').classList.add('hidden');
document.getElementById('lb-info-btn').style.display = rows.length ? '' : 'none';
}
function renderLightbox() {
const e = ENTRIES[lbIndex];
const img = document.getElementById('lb-img');
const vid = document.getElementById('lb-video');
const aud = document.getElementById('lb-audio');
[img, vid, aud].forEach(el => { el.classList.add('hidden'); el.pause && el.pause(); });
lbBuildInfo(e);
if (e.type === 'image') {
img.src = e.src; img.alt = e.title;
img.classList.remove('hidden');
} else if (e.type === 'video') {
vid.src = e.src;
vid.classList.remove('hidden');
vid.play().catch(() => {});
} else if (e.type === 'audio') {
aud.src = e.src;
aud.classList.remove('hidden');
aud.play().catch(() => {});
}
document.getElementById('lb-title').textContent = e.title !== e.name ? e.title : '';
document.getElementById('lb-caption').textContent = e.caption || '';
document.getElementById('lb-counter').textContent = `${lbIndex + 1} / ${ENTRIES.length}`;
const dl = document.getElementById('lb-download');
dl.href = e.src;
dl.download = e.name;
// Update URL hash so the address bar is the shareable link
history.replaceState(null, '', location.pathname + location.search + '#photo=' + encodeURIComponent(e.name));
}
function lbCopyLink() {
navigator.clipboard.writeText(location.href).then(() => {
const btn = document.getElementById('lb-copylink');
const orig = btn.textContent;
btn.textContent = '✓ Copied!';
setTimeout(() => { btn.textContent = orig; }, 1800);
}).catch(() => {
// Fallback: select a temporary input
const tmp = document.createElement('input');
tmp.value = location.href;
document.body.appendChild(tmp);
tmp.select();
document.execCommand('copy');
document.body.removeChild(tmp);
});
}
function lbKey(ev) {
if (ev.key === 'Escape') closeLightbox();
else if (ev.key === 'ArrowLeft') lbNav(-1);
else if (ev.key === 'ArrowRight') lbNav(1);
}
// Restore lightbox from URL hash on page load
window.addEventListener('DOMContentLoaded', () => {
const m = location.hash.match(/^#photo=(.+)$/);
if (m) {
const name = decodeURIComponent(m[1]);
const idx = ENTRIES.findIndex(e => e.name === name);
if (idx >= 0) openLightbox(idx);
}
});
// Album search filter + slideshow link (kept together so filter state feeds the link)
(function () {
const input = document.getElementById('album-search');
const link = document.getElementById('ss-launch');
function updateSsLink() {
if (!link) return;
const p = [];
if (document.getElementById('ss-opt-shuffle')?.checked) p.push('shuffle=1');
if (document.getElementById('ss-opt-fullscreen')?.checked) p.push('fullscreen=1');
if (input && input.value.trim()) {
const visible = [...document.querySelectorAll('#album-grid .album-card')]
.filter(c => c.style.display !== 'none')
.map(c => c.dataset.rel)
.filter(Boolean);
if (visible.length) p.push('dirs=' + visible.map(encodeURIComponent).join(','));
}
link.href = link.dataset.base + (p.length ? '?' + p.join('&') : '');
}
if (input) {
input.addEventListener('input', () => {
const q = input.value.trim().toLowerCase();
document.querySelectorAll('#album-grid .album-card').forEach(card => {
const label = (card.querySelector('.album-label')?.textContent || '').toLowerCase();
card.style.display = !q || label.includes(q) ? '' : 'none';
});
updateSsLink();
});
}
['ss-opt-shuffle', 'ss-opt-fullscreen'].forEach(id =>
document.getElementById(id)?.addEventListener('change', updateSsLink)
);
})();
// Touch swipe
(function () {
let startX = null;
const lb = document.getElementById('lightbox');
if (!lb) return;
lb.addEventListener('touchstart', e => { startX = e.changedTouches[0].clientX; }, { passive: true });
lb.addEventListener('touchend', e => {
if (startX === null) return;
const dx = e.changedTouches[0].clientX - startX;
if (Math.abs(dx) > 50) lbNav(dx < 0 ? 1 : -1);
startX = null;
}, { passive: true });
})();