document.addEventListener("DOMContentLoaded", function (){
let globalModal=null;
let globalModalImg=null;
let globalCaptionDiv=null;
let globalThumbs=null;
let globalCurrentIndex=0;
let globalImageList=[];
document.addEventListener('keydown', handleKeydown);
function handleKeydown(e){
if(!globalModal||globalModal.style.display!=='flex') return;
if(e.key==='Escape'){
globalModal.style.display='none';
window.hasLightboxKeyListener=false;
}else if(e.key==='ArrowLeft'){
showImage(globalCurrentIndex - 1);
}else if(e.key==='ArrowRight'){
showImage(globalCurrentIndex + 1);
}}
function showImage(newIndex){
if(newIndex < 0) newIndex=globalImageList.length - 1;
if(newIndex >=globalImageList.length) newIndex=0;
globalCurrentIndex=newIndex;
globalModalImg.src=globalImageList[globalCurrentIndex].full;
globalCaptionDiv.innerHTML=globalImageList[globalCurrentIndex].caption||'';
globalThumbs.querySelectorAll('img').forEach((thumb, i)=> {
thumb.classList.toggle('active', i===globalCurrentIndex);
});
}
function openModal(index, imageList){
globalCurrentIndex=index;
globalImageList=imageList;
if(!globalModal){
globalModal=document.createElement('div');
globalModal.className='custom-lightbox';
globalModal.innerHTML=`
<span class="close">&times;</span>
<div class="position__relative">
<img class="modal-content" id="lightbox-img">
<div class="arrow prev"><svg width="13" height="20" viewBox="0 0 13 20" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M11.7305 19.5452C12.0234 19.2639 12.188 18.8824 12.188 18.4847C12.188 18.0869 12.0234 17.7055 11.7305 17.4242L3.99615 9.9992L11.7305 2.5742C12.0151 2.29129 12.1726 1.91239 12.1691 1.51909C12.1655 1.1258 12.0012 0.749579 11.7115 0.471467C11.4218 0.193355 11.0299 0.0356033 10.6202 0.0321857C10.2105 0.0287681 9.81584 0.17996 9.52115 0.453197L0.682088 8.9387C0.389165 9.21999 0.224609 9.60145 0.224609 9.9992C0.224609 10.3969 0.389165 10.7784 0.682088 11.0597L9.52115 19.5452C9.81416 19.8264 10.2115 19.9844 10.6258 19.9844C11.0402 19.9844 11.4375 19.8264 11.7305 19.5452Z" fill="#1D3133"></path></svg></div>
<div class="arrow next"><svg width="13" height="20" viewBox="0 0 13 20" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M1.26947 19.5452C0.976551 19.2639 0.811996 18.8824 0.811996 18.4847C0.811996 18.0869 0.976551 17.7055 1.26947 17.4242L9.00385 9.9992L1.26947 2.5742C0.984853 2.29129 0.827362 1.91239 0.830922 1.51909C0.834482 1.1258 0.998808 0.749579 1.28851 0.471467C1.57821 0.193355 1.9701 0.0356033 2.37979 0.0321857C2.78947 0.0287681 3.18416 0.17996 3.47885 0.453197L12.3179 8.9387C12.6108 9.21999 12.7754 9.60145 12.7754 9.9992C12.7754 10.3969 12.6108 10.7784 12.3179 11.0597L3.47885 19.5452C3.18584 19.8264 2.78848 19.9844 2.37416 19.9844C1.95984 19.9844 1.56249 19.8264 1.26947 19.5452Z" fill="#1D3133"></path></svg></div>
</div>
<div class="lightbox-caption" id="lightbox-caption"></div>
<ul class="lightbox-thumbnails"></ul>
`;
document.body.appendChild(globalModal);
globalModalImg=globalModal.querySelector('#lightbox-img');
globalCaptionDiv=globalModal.querySelector('#lightbox-caption');
globalThumbs=globalModal.querySelector('.lightbox-thumbnails');
globalModal.querySelector('.close').addEventListener('click', ()=> {
globalModal.style.display='none';
});
globalModal.querySelector('.prev').addEventListener('click', ()=> {
showImage(globalCurrentIndex - 1);
});
globalModal.querySelector('.next').addEventListener('click', ()=> {
showImage(globalCurrentIndex + 1);
});
}
globalModal.style.display='flex';
if(!window.hasLightboxKeyListener){
}
showImage(index);
globalThumbs.innerHTML='';
imageList.forEach((img, i)=> {
const li=document.createElement('li');
const figure=document.createElement('figure');
const thumb=document.createElement('img');
thumb.src=img.thumb;
if(i===index) thumb.classList.add('active');
thumb.addEventListener('click', ()=> openModal(i, imageList));
figure.appendChild(thumb);
li.appendChild(figure);
globalThumbs.appendChild(li);
});
}
const galleries=document.querySelectorAll('.gallery, .wp-block-gallery');
const fullscreenBtnTemplate=document.createElement('button');
fullscreenBtnTemplate.innerHTML='<svg width="20" height="20" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg"><g clip-path="url(#clip0_34191_500)"><path d="M18.3333 0H6.66675C5.75024 0 5 0.750244 5 1.66675V13.3333C5 14.2498 5.75024 15 6.66675 15H18.3333C19.2498 15 20 14.2498 20 13.3333V1.66675C20 0.750244 19.2498 0 18.3333 0ZM15.415 7.5L15.4157 5.46699L9.21548 11.6667L8.33325 10.7812L14.5305 4.58501L12.4993 4.58325V3.33325H15.8325C16.2933 3.33423 16.6658 3.70674 16.665 4.16675L16.6668 7.50073L15.415 7.5Z" fill="#fff47d"/><path d="M8.33325 16.6667V18.3333H1.66675V11.6667H3.33325V10H0.833252C0.375244 10 0 10.3752 0 10.8333V19.1667C0 19.624 0.375244 20 0.833252 20H9.16675C9.62476 20 10 19.624 10 19.1667V16.6667H8.33325Z" fill="#fff47d"/></g><defs><clipPath id="clip0_34191_500"><rect width="20" height="20" fill="white"/></clipPath></defs></svg>';
fullscreenBtnTemplate.classList.add('lightbox-fullscreen-btn');
galleries.forEach((gallery)=> {
const imgs=gallery.querySelectorAll('a, figure a, figure');
if(imgs.length===0) return;
const imageList=[];
const addedSrcs=new Set();
imgs.forEach((el)=> {
const img=el.querySelector('img');
if(img){
let baseSrc=img.src.split('?')[0];
const anchor=el.tagName==='A' ? el:el.querySelector('a');
if(anchor&&anchor.href){
baseSrc=anchor.href.split('?')[0];
}
if(!addedSrcs.has(baseSrc)){
let caption='';
const fig=el.closest('figure');
if(fig){
const figcaption=fig.querySelector('figcaption');
if(figcaption) caption=figcaption.innerHTML;
}else if(img.alt){
caption=img.alt;
}
imageList.push({ full: baseSrc, thumb: img.src, caption });
addedSrcs.add(baseSrc);
}
img.addEventListener('click', (e)=> {
e.preventDefault();
const index=imageList.findIndex(item=> item.thumb===img.src);
if(index!==-1){
openModal(index, imageList);
}});
}});
if(imageList.length===0) return;
if(gallery.classList.contains('galeria-embutida')){
gallery.innerHTML='';
const mainImg=document.createElement('img');
mainImg.src=imageList[0].full;
mainImg.classList.add('lightbox-main');
const captionDiv=document.createElement('div');
captionDiv.classList.add('lightbox-caption-embutida');
const captionText=imageList[0].caption||'';
if(captionText.trim()===''){
captionDiv.classList.add('caption-vazio');
}else{
captionDiv.classList.remove('caption-vazio');
}
captionDiv.innerHTML=`<span>${captionText}</span>`;
const prev=document.createElement('div');
prev.classList.add('lightbox-arrow', 'left');
prev.innerHTML=`
<svg width="13" height="20" viewBox="0 0 13 20" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M11.7305 19.5452C12.0234 19.2639 12.188 18.8824 12.188 18.4847C12.188 18.0869 12.0234 17.7055 11.7305 17.4242L3.99615 9.9992L11.7305 2.5742C12.0151 2.29129 12.1726 1.91239 12.1691 1.51909C12.1655 1.1258 12.0012 0.749579 11.7115 0.471467C11.4218 0.193355 11.0299 0.0356033 10.6202 0.0321857C10.2105 0.0287681 9.81584 0.17996 9.52115 0.453197L0.682088 8.9387C0.389165 9.21999 0.224609 9.60145 0.224609 9.9992C0.224609 10.3969 0.389165 10.7784 0.682088 11.0597L9.52115 19.5452C9.81416 19.8264 10.2115 19.9844 10.6258 19.9844C11.0402 19.9844 11.4375 19.8264 11.7305 19.5452Z" fill="#1D3133"/>
</svg>`;
const next=document.createElement('div');
next.classList.add('lightbox-arrow', 'right');
next.innerHTML=`<svg width="13" height="20" viewBox="0 0 13 20" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M1.26947 19.5452C0.976551 19.2639 0.811996 18.8824 0.811996 18.4847C0.811996 18.0869 0.976551 17.7055 1.26947 17.4242L9.00385 9.9992L1.26947 2.5742C0.984853 2.29129 0.827362 1.91239 0.830922 1.51909C0.834482 1.1258 0.998808 0.749579 1.28851 0.471467C1.57821 0.193355 1.9701 0.0356033 2.37979 0.0321857C2.78947 0.0287681 3.18416 0.17996 3.47885 0.453197L12.3179 8.9387C12.6108 9.21999 12.7754 9.60145 12.7754 9.9992C12.7754 10.3969 12.6108 10.7784 12.3179 11.0597L3.47885 19.5452C3.18584 19.8264 2.78848 19.9844 2.37416 19.9844C1.95984 19.9844 1.56249 19.8264 1.26947 19.5452Z" fill="#1D3133"/>
</svg>
`;
const thumbsContainer=document.createElement('ul');
thumbsContainer.classList.add('lightbox-thumbnails');
let currentIndex=0;
function updateMainImage(index){
currentIndex=index;
mainImg.src=imageList[index].full;
const captionText=imageList[index].caption||'';
captionDiv.innerHTML=`<span>${captionText}</span>`;
if(captionText.trim()===''){
captionDiv.classList.add('caption-vazio');
}else{
captionDiv.classList.remove('caption-vazio');
}
thumbsContainer.querySelectorAll('img').forEach((thumb, i)=> {
thumb.classList.toggle('active', i===index);
});
}
imageList.forEach((img, i)=> {
const li=document.createElement('li');
const figure=document.createElement('figure');
const thumb=document.createElement('img');
thumb.src=img.thumb;
if(i===0) thumb.classList.add('active');
thumb.addEventListener('click', ()=> updateMainImage(i));
figure.appendChild(thumb);
li.appendChild(figure);
thumbsContainer.appendChild(li);
});
prev.addEventListener('click', ()=> {
const newIndex=(currentIndex - 1 + imageList.length) % imageList.length;
updateMainImage(newIndex);
});
next.addEventListener('click', ()=> {
const newIndex=(currentIndex + 1) % imageList.length;
updateMainImage(newIndex);
});
const fsBtn=fullscreenBtnTemplate.cloneNode(true);
fsBtn.addEventListener('click', ()=> openModal(currentIndex, imageList));
gallery.appendChild(prev);
gallery.appendChild(mainImg);
gallery.appendChild(captionDiv);
gallery.appendChild(next);
gallery.appendChild(thumbsContainer);
gallery.appendChild(fsBtn);
}
else if(gallery.classList.contains('galeria-lightbox')){
imgs.forEach((el, i)=> {
const anchor=el.tagName==='A' ? el:el.querySelector('a');
const clickable=anchor||el;
const baseSrc=anchor?.href?.split('?')[0]||el?.src?.split('?')[0];
const imgIndex=imageList.findIndex((item)=> item.full===baseSrc);
if(clickable&&imgIndex >=0){
clickable.addEventListener('click', (e)=> {
e.preventDefault?.();
openModal(imgIndex, imageList);
});
}});
}});
});