document.addEventListener('DOMContentLoaded', function() { let viewerOpen = false; const viewerOverlay = document.getElementById('image-viewer-overlay'); viewerOverlay.innerHTML = `
`; const imageView = document.getElementById('image-viewer'); function openImageViewer(element) { const parentImageDiv = element.closest('.image'); if (!parentImageDiv) return; const imgElement = parentImageDiv.querySelector('img.clickable'); const fullImageUrl = imgElement.dataset.proxyFull; // Use data-proxy-full for ProxyFull const thumbnailUrl = imgElement.src; // Use ProxyThumb for the thumbnail const title = imgElement.alt; const sourceUrl = parentImageDiv.querySelector('.img_source').href; // Source webpage URL if (!fullImageUrl || viewerOpen) { return; // Don't open if data is missing or viewer is already open } viewerOpen = true; const viewerImage = document.getElementById('viewer-image'); const viewerTitle = document.getElementById('viewer-title'); const viewerSourceButton = document.getElementById('viewer-source-button'); const fullSizeLink = imageView.querySelector('.full-size'); const proxySizeLink = imageView.querySelector('.proxy-size'); // Set the viewer image to ProxyFull viewerImage.src = fullImageUrl; viewerTitle.textContent = title; viewerSourceButton.href = sourceUrl; fullSizeLink.href = sourceUrl; // Link to the source website proxySizeLink.href = fullImageUrl; // Link to the proxied full-size image viewerOverlay.style.display = 'flex'; imageView.classList.remove('image_hide'); imageView.classList.add('image_show'); } // Attach event listener to the document body document.body.addEventListener('click', function(e) { let target = e.target; let clickableElement = target.closest('img.clickable, .img_title.clickable'); if (clickableElement) { e.preventDefault(); openImageViewer(clickableElement); } }); function closeImageViewer() { imageView.classList.remove('image_show'); imageView.classList.add('image_hide'); viewerOverlay.style.display = 'none'; viewerOpen = false; } // Close viewer on overlay or button click document.getElementById('viewer-close-button').addEventListener('click', closeImageViewer); viewerOverlay.addEventListener('click', function(e) { if (e.target === viewerOverlay) { closeImageViewer(); } }); // Handle keyboard events for closing and navigation document.addEventListener('keydown', function(e) { if (viewerOverlay.style.display === 'flex') { if (e.key === 'Escape') { closeImageViewer(); } } }); });