document.addEventListener('DOMContentLoaded', () => {
if (typeof window.BiggerPicture !== 'function') {
console.error('[gallery-init] BiggerPicture not found. Check script path.');
return;
}
// --- Shared helpers for video detection + mime typing ---
const VIDEO_EXTENSIONS = ['mp4', 'webm', 'mov', 'm4v', 'ogv'];
function isVideo(href) {
if (!href) return false;
const clean = String(href).toLowerCase().split('#')[0].split('?')[0];
return VIDEO_EXTENSIONS.some(ext => clean.endsWith('.' + ext));
}
function mimeFromExt(href) {
const ext = String(href).toLowerCase().split('#')[0].split('?')[0].split('.').pop();
switch (ext) {
case 'mp4': case 'm4v': return 'video/mp4';
case 'webm': return 'video/webm';
case 'ogv': return 'video/ogg';
case 'mov': return 'video/quicktime';
default: return 'video/*';
}
}
// -------------------------------------------------------
function setupGalleries(root = document) {
const imgs = root.querySelectorAll('.figure img, img.org-svg');
imgs.forEach((img) => {
if (img.closest('a')) return;
const a = document.createElement('a');
const href = img.currentSrc || img.src;
a.href = href;
a.dataset.img = href;
a.dataset.alt = img.alt || '';
const setDims = () => {
a.dataset.width = img.naturalWidth || img.width || 1920;
a.dataset.height = img.naturalHeight || img.height || 1080;
};
if (img.complete) setDims();
else img.addEventListener('load', setDims);
img.style.cursor = 'zoom-in';
img.parentElement.insertBefore(a, img);
a.appendChild(img);
});
const containers = root.querySelectorAll('main, article, .content, body');
containers.forEach((container) => {
const links = Array.from(container.querySelectorAll(
'.figure a, a:has(img.org-svg), a[href$=".mp4"], a[href$=".webm"], a[href$=".mov"], a[href$=".m4v"], a[href$=".ogv"]'
));
if (!links.length) return;
links.forEach((link) => {
if (link.dataset.bpBound) return;
link.dataset.bpBound = 'true';
link.addEventListener('click', (e) => {
e.preventDefault();
bp.open({
items: links,
el: link,
caption: (el) => el.querySelector('img')?.alt || el.title || '',
maxZoom: 40,
onOpen(containerEl) {
const linkEl = bp.currItem;
const href = linkEl?.href || '';
if (isVideo(href)) {
teardownRotation();
const htmlLayer = containerEl.querySelector('.bp-html');
const imgLayer = containerEl.querySelector('.bp-img');
if (imgLayer) imgLayer.style.display = 'none';
htmlLayer.innerHTML = `
`;
return;
}
setupRotation(containerEl);
enhanceSVG(containerEl);
}
});
});
});
});
}
// Replaces plain video links with inline