(()=>{window.addEventListener("load",()=>{g.init()});var g={lightBox:null,lightBoxView:null,lightBoxCounter:null,lightBoxCaption:null,lightBoxThumblist:null,lightBoxImg:null,lightBoxTimer:null,lightBoxButtons:{},gallery:null,galleryId:null,galleryIndex:null,galleryData:{},init(){this.lightBox=document.querySelector(".cm4all-gallery-lightbox"),this.lightBox||(document.body.insertAdjacentHTML("beforeend",` `),this.lightBox=document.querySelector(".cm4all-gallery-lightbox"),this.lightBoxView=this.lightBox.querySelector(".cm4all-gallery-lightbox-view"),this.lightBoxCounter=this.lightBox.querySelector(".cm4all-gallery-lightbox-counter"),this.lightBoxCaption=this.lightBox.querySelector(".cm4all-gallery-lightbox-caption"),this.lightBoxThumblist=this.lightBox.querySelector(".cm4all-gallery-lightbox-thumblist"),this.addNavigation(),this.addKeyListeners(),this.addMouseListener(),this.addTouchListeners()),this.update()},update(){this.galleryData={},[...document.querySelectorAll(".cm4all-gallery")].forEach((t,i)=>{let e=t.dataset.showLightbox==="true",s=t.dataset.showCaption==="true",o=t.dataset.showThumblist==="true";e&&(this.galleryData[i]={showLightbox:e,showCaption:s,showThumblist:o,images:[]},[...t.querySelectorAll(".cm4all-gallery-item")].forEach((a,h)=>{this.galleryData[i].images.push({id:a.dataset.lightboxId,alt:a.dataset.lightboxAlt,link:a.dataset.lightboxLink,caption:a.dataset.lightboxCaption,thumbUrl:a.dataset.lightboxThumb,mediumUrl:a.dataset.lightboxMedium,largeUrl:a.dataset.lightboxLarge,fullUrl:a.dataset.lightboxFull}),a.addEventListener("click",r=>{this.show(i,h),r.stopPropagation()},!1)}))})},click(l){switch(l){case"screen":this.stop(),this.lightBoxImg.requestFullscreen();break;case"link":this.stop(),this.link();break;case"play":this.play();break;case"stop":this.stop();break;case"back":this.stop(),this.back();break;case"next":this.stop(),this.next();break;case"close":this.stop(),this.show();break;default:break}},show(l,t){if(typeof t=="number"){if(t=Math.min(Math.max(t,0),this.galleryData[l].images.length-1),l===this.galleryId&&t===this.galleryIndex)return;let i=l!==this.galleryId;this.galleryId=l,this.galleryIndex=t,this.gallery=this.galleryData[this.galleryId],i&&(this.createView(),this.createThumblist()),this.updateView(),this.updateNavigation(),this.updateCounter(),this.updateCaption(),this.updateThumblist(),this.lightBox.classList.remove("hidden"),document.body.classList.add("noScroll")}else this.lightBox.classList.add("hidden"),document.body.classList.remove("noScroll"),this.galleryId=null,this.galleryIndex=null,this.gallery=null,this.stop()},back(){this.show(this.galleryId,this.galleryIndex-1)},next(){this.show(this.galleryId,this.galleryIndex+1)},play(){this.lightBoxTimer?(clearInterval(this.lightBoxTimer),this.lightBoxTimer=null):(this.next(),this.lightBoxTimer=setInterval(()=>{this.gallery.images.length-1===this.galleryIndex?this.stop():this.next()},3e3)),this.updatePlayButton()},stop(){this.lightBoxTimer&&this.play()},link(){window.open(this.gallery.images[this.galleryIndex].link,"cm4all-gallery")},addNavigation(){[...this.lightBox.querySelectorAll(".cm4all-gallery-lightbox-button")].forEach(t=>{let i=t.getAttribute("aria-label");t.addEventListener("click",e=>{this.click(i),e.stopPropagation()},!1),this.lightBoxButtons[i]=t})},addKeyListeners(){document.addEventListener("keydown",l=>{if(this.gallery)switch(l.key){case"ArrowLeft":this.back();break;case"ArrowRight":this.next();break;case"Escape":this.show();break;case"Enter":l.target.ariaLabel==="thumb"&&document.activeElement.click();break;case" ":this.play();break;default:break}l.stopPropagation()},!1)},addMouseListener(){this.lightBox.addEventListener("click",l=>{(l.target===this.lightBox||l.target===this.lightBoxView)&&(this.click("close"),l.stopPropagation())},!1)},addTouchListeners(){let l=null;document.addEventListener("touchstart",t=>{l=t.changedTouches[0]}),document.addEventListener("touchend",t=>{let i=t.changedTouches[0];i.screenXl.screenX&&this.back()})},createView(){for(this.lightBoxView.removeChild(this.lightBoxCaption);this.lightBoxView.firstChild;)this.lightBoxView.removeChild(this.lightBoxView.lastChild);this.galleryData[this.galleryId].showThumblist?this.lightBox.classList.add("showThumblist"):this.lightBox.classList.remove("showThumblist"),this.gallery.images.forEach((t,i)=>{let e=new window.Image;e.id=`image-${this.galleryId}-${i}`,e.loading="lazy",e.tabIndex="-1",e.role="none",e.ariaLabel="image",this.lightBoxView.appendChild(e),e.src=t.largeUrl}),this.lightBoxView.appendChild(this.lightBoxCaption)},updateView(){let l=this.lightBoxView.querySelector(".active");l&&l.classList.remove("active");let t=this.lightBoxView.querySelector(`#image-${this.galleryId}-${this.galleryIndex-1}`);t&&t.classList.add("loaded");let i=this.lightBoxView.querySelector(`#image-${this.galleryId}-${this.galleryIndex+1}`);i&&i.classList.add("loaded");let e=this.lightBoxView.querySelector(`#image-${this.galleryId}-${this.galleryIndex}`);e.classList.contains("loaded")?e.classList.add("active"):(e.classList.add("loaded"),e.onload=()=>{e.classList.add("active")}),this.lightBoxImg=e},createThumblist(){for(;this.lightBoxThumblist.firstChild;)this.lightBoxThumblist.removeChild(this.lightBoxThumblist.lastChild);this.gallery.showThumblist&&this.gallery.images.forEach((l,t)=>{let i=new window.Image;i.id=`thumb-${this.galleryId}-${t}`,i.loading="lazy",i.tabIndex="0",i.role="button",i.ariaLabel="thumb",this.lightBoxThumblist.appendChild(i),i.addEventListener("click",e=>{this.show(this.galleryId,t),e.stopPropagation()},!1),i.onload=function(){this.galleryIndex===t&&(i.classList.add("active"),i.scrollIntoView({behavior:"smooth",block:"center"}))},i.src=l.thumbUrl})},updateThumblist(){if(!this.gallery.showThumblist)return;let l=this.lightBoxThumblist.querySelector(".active");l&&l.classList.remove("active");let t=this.lightBoxThumblist.querySelector(`#thumb-${this.galleryId}-${this.galleryIndex}`);t&&(t.classList.add("active"),t.scrollIntoView({behavior:"smooth",block:"center"}))},updateNavigation(){let l=this.lightBoxButtons.back,t=this.lightBoxButtons.next;l&&(l.dataset.disabled=this.galleryIndex===0),t&&(t.dataset.disabled=this.galleryIndex===this.gallery.images.length-1),this.updatePlayButton()},updateCounter(){this.lightBoxCounter.textContent=`${this.galleryIndex+1}/${this.gallery.images.length}`},updatePlayButton(){let l=this.lightBoxButtons.play,t=this.lightBoxButtons.stop;l&&t&&(this.lightBoxTimer!==null?(t.classList.add("visible"),l.classList.remove("visible")):(l.classList.add("visible"),t.classList.remove("visible")))},updateCaption(){if(!this.gallery.showCaption){this.lightBoxCaption.textContent="";return}let l=this.gallery.images[this.galleryIndex];this.lightBoxCaption.textContent=l.caption||l.alt}};})();