1
(function(){
2
'use strict';
3
function initGallery(root){
4
if(!root || root.dataset.freshOoInit==='1') return;
5
root.dataset.freshOoInit='1';
6
const thumbs=[...root.querySelectorAll('[data-gallery-thumb]')];
7
if(!thumbs.length) return;
8
const mainImg=root.querySelector('[data-gallery-main-image]');
9
const mainBtn=root.querySelector('[data-gallery-main]');
10
const current=root.querySelector('[data-gallery-current]');
11
const caption=root.querySelector('[data-gallery-caption]');
12
const thumbRail=root.querySelector('[data-gallery-thumbs]');
13
const dialog=root.querySelector('[data-gallery-lightbox]');
14
const dialogImg=root.querySelector('[data-gallery-lightbox-image]');
15
const dialogCaption=root.querySelector('[data-gallery-lightbox-caption]');
16
let index=0;
17
function data(i){ return thumbs[i].dataset; }
18
function select(i, focusThumb){
19
if(!thumbs.length) return;
20
index=(i+thumbs.length)%thumbs.length;
21
const d=data(index);
22
root.classList.add('is-changing');
23
mainImg.src=d.main||d.full;
24
mainImg.srcset=d.srcset||'';
25
mainImg.sizes=d.sizes||'';
26
mainImg.alt=d.alt||'';
27
if(caption) caption.textContent=d.caption||'';
28
if(current) current.textContent=String(index+1);
29
thumbs.forEach((t,n)=>{const active=n===index;t.classList.toggle('is-active',active);t.setAttribute('aria-current',active?'true':'false');});
30
thumbs[index].scrollIntoView({behavior:'smooth',block:'nearest',inline:'nearest'});
31
if(focusThumb) thumbs[index].focus({preventScroll:true});
32
window.setTimeout(()=>root.classList.remove('is-changing'),180);
33
if(dialog && dialog.open) syncLightbox();
34
}
35
function syncLightbox(){
36
const d=data(index); if(!dialogImg) return;
37
dialogImg.src=d.full||d.main; dialogImg.alt=d.alt||'';
38
if(dialogCaption) dialogCaption.textContent=d.caption||'';
39
}
40
function openLightbox(){ if(!dialog || root.dataset.lightbox!=='1') return; syncLightbox(); if(typeof dialog.showModal==='function') dialog.showModal(); else dialog.setAttribute('open','open'); }
41
function closeLightbox(){ if(!dialog) return; if(typeof dialog.close==='function') dialog.close(); else dialog.removeAttribute('open'); }
42
thumbs.forEach((t,i)=>t.addEventListener('click',()=>select(i,false)));
43
root.querySelectorAll('[data-gallery-prev],[data-gallery-lightbox-prev]').forEach(el=>el.addEventListener('click',e=>{e.stopPropagation();select(index-1,false);}));
44
root.querySelectorAll('[data-gallery-next],[data-gallery-lightbox-next]').forEach(el=>el.addEventListener('click',e=>{e.stopPropagation();select(index+1,false);}));
45
const thumbsPrev=root.querySelector('[data-gallery-thumbs-prev]'), thumbsNext=root.querySelector('[data-gallery-thumbs-next]');
46
if(thumbsPrev&&thumbRail) thumbsPrev.addEventListener('click',()=>thumbRail.scrollBy({left:-Math.max(thumbRail.clientWidth*.75,160),behavior:'smooth'}));
47
if(thumbsNext&&thumbRail) thumbsNext.addEventListener('click',()=>thumbRail.scrollBy({left:Math.max(thumbRail.clientWidth*.75,160),behavior:'smooth'}));
48
if(mainBtn) mainBtn.addEventListener('click',e=>{ if(e.target.closest('[data-gallery-prev],[data-gallery-next]')) return; openLightbox(); });
49
const close=root.querySelector('[data-gallery-lightbox-close]'); if(close) close.addEventListener('click',closeLightbox);
50
if(dialog) dialog.addEventListener('click',e=>{if(e.target===dialog) closeLightbox();});
51
root.addEventListener('keydown',e=>{
52
if(e.key==='ArrowLeft'){select(index-1,false);e.preventDefault();}
53
if(e.key==='ArrowRight'){select(index+1,false);e.preventDefault();}
54
if(e.key==='Escape'&&dialog&&dialog.open) closeLightbox();
55
});
56
let touchX=null;
57
if(mainBtn){
58
mainBtn.addEventListener('touchstart',e=>{touchX=e.changedTouches[0].clientX;},{passive:true});
59
mainBtn.addEventListener('touchend',e=>{if(touchX===null)return;const dx=e.changedTouches[0].clientX-touchX;touchX=null;if(Math.abs(dx)>45)select(index+(dx<0?1:-1),false);},{passive:true});
60
}
61
}
62
function init(scope){(scope||document).querySelectorAll('[data-fresh-oo-gallery]').forEach(initGallery);}
63
document.addEventListener('DOMContentLoaded',()=>init(document));
64
if(window.elementorFrontend&&window.elementorFrontend.hooks){window.elementorFrontend.hooks.addAction('frontend/element_ready/fresh-oo-property-gallery.default',function($scope){init($scope[0]||$scope);});}
65
})();
66