document.addEventListener('DOMContentLoaded', function() {
// Image Preview Modal Functionality
const imageTab = document.querySelector('#tab-images');
if (imageTab) {
// Create modal container with proper Bootstrap modal structure
const modalContainer = document.createElement('div');
modalContainer.className = 'modal fade image-preview-modal';
modalContainer.id = 'imagePreviewModal';
modalContainer.tabIndex = '-1';
modalContainer.setAttribute('aria-hidden', 'true');
// Create modal content
modalContainer.innerHTML = `
Images may be subject to copyright. Please check the source for more information.
`;
document.body.appendChild(modalContainer);
// Initialize Bootstrap modal
const modal = new bootstrap.Modal(modalContainer);
// Get all preview images
const previewImages = imageTab.querySelectorAll('a[href="#"]');
let currentImageIndex = 0;
// Add click event to each preview image
previewImages.forEach((link, index) => {
link.addEventListener('click', (e) => {
e.preventDefault();
const imgElement = link.querySelector('img');
const imgSrc = imgElement.src;
// Add -big before the file extension
const bigImgSrc = imgSrc.replace(/(\.[^.]+)$/, '-big$1');
showPreview(bigImgSrc, link);
currentImageIndex = index;
updateNavigationButtons();
});
});
// Navigation buttons
const prevBtn = modalContainer.querySelector('#prevImage');
const nextBtn = modalContainer.querySelector('#nextImage');
prevBtn.addEventListener('click', (e) => {
e.stopPropagation();
if (currentImageIndex > 0) {
currentImageIndex--;
const link = previewImages[currentImageIndex];
const imgElement = link.querySelector('img');
const imgSrc = imgElement.src;
const bigImgSrc = imgSrc.replace(/(\.[^.]+)$/, '-big$1');
updatePreview(bigImgSrc, link);
updateNavigationButtons();
}
});
nextBtn.addEventListener('click', (e) => {
e.stopPropagation();
if (currentImageIndex < previewImages.length - 1) {
currentImageIndex++;
const link = previewImages[currentImageIndex];
const imgElement = link.querySelector('img');
const imgSrc = imgElement.src;
const bigImgSrc = imgSrc.replace(/(\.[^.]+)$/, '-big$1');
updatePreview(bigImgSrc, link);
updateNavigationButtons();
}
});
// Helper functions
function showPreview(imgSrc, link) {
updatePreview(imgSrc, link);
modal.show();
}
function updatePreview(imgSrc, link) {
const previewImg = modalContainer.querySelector('.img-preview');
previewImg.src = imgSrc;
// Update image information
modalContainer.querySelector('.image-title').textContent = link.dataset.imgTitle || '';
modalContainer.querySelector('.image-description').textContent = link.dataset.imgDescription || '';
modalContainer.querySelector('.image-category').textContent = link.dataset.imgCategory || '';
modalContainer.querySelector('.image-date').textContent = link.dataset.imgDate || '';
modalContainer.querySelector('.image-source').textContent = link.dataset.imgSource || '';
}
function hidePreview() {
modal.hide();
}
function updateNavigationButtons() {
prevBtn.classList.toggle('hidden', currentImageIndex === 0);
nextBtn.classList.toggle('hidden', currentImageIndex === previewImages.length - 1);
}
// Keyboard navigation
document.addEventListener('keydown', (e) => {
if (!modalContainer.classList.contains('show')) return;
switch(e.key) {
case 'Escape':
hidePreview();
break;
case 'ArrowLeft':
if (currentImageIndex > 0) prevBtn.click();
break;
case 'ArrowRight':
if (currentImageIndex < previewImages.length - 1) nextBtn.click();
break;
}
});
// Clean up modal backdrop when modal is hidden
modalContainer.addEventListener('hidden.bs.modal', function () {
const backdrop = document.querySelector('.modal-backdrop');
if (backdrop) {
backdrop.remove();
}
});
}
});