(function(v,F){typeof exports=="object"&&typeof module<"u"?F(require("jquery")):typeof define=="function"&&define.amd?define(["jquery"],F):(v=typeof globalThis<"u"?globalThis:v||self,F(v.jQuery))})(this,function(v){"use strict";function F(e,n,o){const i=`${n}=${o}`;let r=`?${i}`;if(e){const t=new RegExp(`([?&])${n}=[^&]*`);t.exec(e)!==null?r=e.replace(t,`$1${i}`):r=`${e}&${i}`}return r}function z(e){const n=new RegExp(`[?&]${e}(=([^&#]*)|&|#|$)`).exec(document.location.search);if((n==null?void 0:n[2])!==void 0)return decodeURIComponent(n[2].replace(/\+/g," "))}function K(e,n){let o=e;if(o){const i=new RegExp(`\\?${n}=[^&]*`),r=new RegExp(`&${n}=[^&]*`);o=o.replace(i,"?"),o=o.replace(r,"")}return o}var E=(e=>(e[e.Left=1]="Left",e[e.None=0]="None",e[e.Right=-1]="Right",e))(E||{});function G(e,n,o){if(z("imageLightboxSet")!==e)return;const i=z("imageLightboxIndex");if(i===void 0)return;let r=n.findIndex(t=>t.dataset.ilb2Id===i);r<0&&(r=parseInt(i,10)),o(r,!0)}function Y(e,n,o,i,r,t,l){const f=e.state;if(!f||Object.keys(f).length===0){t(!0);return}if(f.imageLightboxSet!==n)return;const u=f.imageLightboxIndex;if(u===void 0){t(!0);return}let m=o.findIndex(L=>L.dataset.ilb2Id===u);if(m<0&&(m=parseInt(u,10)),i===null){r(m,!0);return}l(m,m>i?E.Right:E.Left,!0)}function Z(){let e=K(document.location.search,"imageLightboxIndex");e=K(e,"imageLightboxSet"),window.history.pushState({},"",document.location.pathname+e)}function W(e,n,o){const i=o[e].dataset.ilb2Id??e.toString();let r=F(document.location.search,"imageLightboxIndex",i);const t={imageLightboxIndex:i,imageLightboxSet:""};n!==void 0&&(t.imageLightboxSet=n,r=F(r,"imageLightboxSet",n)),window.history.pushState(t,"",document.location.pathname+r)}const g=document.createElement("div");g.setAttribute("id","ilb-container");let N=null;function _(e,n,o){g.classList.remove("ilb-overlay"),document.body.classList.add("ilb-body"),document.body.appendChild(g),n&&(N=i=>{i.stopPropagation(),o()},g.addEventListener("click",N),g.addEventListener("touchend",N)),g.style.transition=`opacity ${e.toString()}ms ease`,setTimeout(()=>{g.style.opacity="1"},50)}function ee(){g.classList.add("ilb-overlay")}function y(){return g}function te(){N!==null&&(g.removeEventListener("click",N),g.removeEventListener("touchend",N)),g.remove(),g.textContent="",document.body.classList.remove("ilb-body")}function ne(){g.style.opacity="0"}const H=document.createElement("div");H.setAttribute("id","ilb-activity-indicator"),H.appendChild(document.createElement("div"));function j(){y().appendChild(H)}function X(){H.remove()}const I=document.createElement("div");I.classList.add("ilb-button","ilb-arrow"),I.setAttribute("id","ilb-arrow-left");const C=document.createElement("div");C.classList.add("ilb-button","ilb-arrow"),C.setAttribute("id","ilb-arrow-right");let k=null,O=null;function ie(e,n){k=o=>{o.stopPropagation(),e()},O=o=>{o.stopPropagation(),n()},I.addEventListener("click",k),I.addEventListener("touchend",k),C.addEventListener("click",O),C.addEventListener("touchend",O),y().append(I,C)}function oe(){I.remove(),k!==null&&(I.removeEventListener("click",k),I.removeEventListener("touchend",k)),O!==null&&(C.removeEventListener("click",O),C.removeEventListener("touchend",O)),k=null,O=null}const x=document.createElement("caption");x.setAttribute("id","ilb-caption"),x.textContent=" ",x.addEventListener("click",e=>{e.stopPropagation()});function re(e,n){e!==""?(y().appendChild(x),x.style.transition=`opacity ${n.toString()}ms ease`,setTimeout(()=>{x.style.opacity="1"},1),x.textContent=e):(x.style.opacity="0",setTimeout(()=>{x.remove()},n))}const P=document.createElement("button");P.classList.add("ilb-button"),P.setAttribute("id","ilb-close-button");let Q=null;function ae(e){Q=n=>{n.stopPropagation(),e()},P.addEventListener("click",Q),y().appendChild(P)}function se(){Q!==null&&P.removeEventListener("click",Q),P.remove()}const le=document.fullscreenEnabled||(document.webkitFullscreenEnabled??!1);function de(){return document.fullscreenElement??document.webkitFullscreenElement??null}const ce=e=>{(e.requestFullscreen||e.webkitRequestFullScreen).call(e)},ue=()=>{(document.exitFullscreen||document.webkitExitFullscreen).call(document)};function fe(){de()===null?ce(y()):ue()}const R=document.createElement("button");R.classList.add("ilb-button"),R.setAttribute("id","ilb-fullscreen-button"),R.innerHTML="⛶",R.addEventListener("click",e=>{e.stopPropagation(),fe()});function me(){y().appendChild(R)}function ge(){R.remove()}function be(e,n,o){let i=0,r=0,t=document.createElement("img");t.addEventListener("error",d=>{y().dispatchEvent(new CustomEvent("ilb:error",{bubbles:!0,detail:{target:d.target}}))}),t.setAttribute("id","ilb-image"),t.setAttribute("src",e.getAttribute("href")??""),t.setAttribute("srcset",e.dataset.ilb2Srcset??""),t.setAttribute("sizes",e.dataset.ilb2Sizes??"");const l=document.createElement("div");l.classList.add("ilb-image-container");let f;const u=e.dataset.ilb2VideoId;let m=e.dataset.ilb2Video!==void 0&&u!==void 0;if(m){const d=o.get(u);d!==void 0?[t,f]=d.element():m=!1}l.appendChild(t);function L(d,b,h,D){if(m){const a=o.get(u);if((a==null?void 0:a.shouldAutoplay())===!0){const[s,c]=a.element();c?s.play():s.autoplay=!0}}else t.addEventListener("click",a=>{if(a.stopPropagation(),n.quitOnImgClick){D();return}(a.pageX-t.offsetLeft)/t.width<=1/3?b():h()});t.addEventListener("touchstart",a=>{i=a.touches[0].pageX,t.style.transitionProperty="opacity"}),t.addEventListener("touchmove",a=>{r=a.touches[0].pageX-i,t.style.left=`${r.toString()}px`}),t.addEventListener("touchend",a=>{a.stopPropagation(),t.style.transitionProperty="left, opacity",r>50&&b(),r<-50&&h(),t.style.left="0"}),t.addEventListener("touchcancel",a=>{a.stopPropagation(),t.style.transitionProperty="left, opacity",t.style.left="0"}),d()}function S(d,b){y().appendChild(l);const h=Math.abs(100-n.gutter);t.style.maxHeight=`${h.toString()}%`,t.style.maxWidth=`${h.toString()}%`,t.style.left=`${(-100*d).toString()}px`,t.style.transition=`all ease ${n.animationSpeed.toString()}ms`,setTimeout(b,50)}function M(d,b){t.addEventListener("error",b),f===!0?d():(t.addEventListener("load",d),t.addEventListener("loadedmetadata",d))}function p(d,b,h,D){t.style.left="0",t.style.opacity="1",setTimeout(()=>{L(d,b,h,D)},n.animationSpeed)}function A(d,b){if(d!==E.None){const h=parseInt(t.style.left,10)||0;t.style.left=`${(h+100*d).toString()}px`}t.style.opacity="0",setTimeout(()=>{b()},n.animationSpeed)}function $(){l.remove()}return{addToDOM:S,removeFromDOM:$,startLoading:M,transitionIn:p,transitionOut:A}}let V=null;function ve(e,n,o,i){e.enableKeyboard&&(V=r=>{r.key==="Escape"&&(r.preventDefault(),n()),r.key==="ArrowLeft"&&(r.preventDefault(),o()),r.key==="ArrowRight"&&(r.preventDefault(),i())},document.addEventListener("keyup",V))}function pe(){V!==null&&document.removeEventListener("keyup",V)}const w=document.createElement("div");w.classList.add("ilb-navigation");function J(e,n,o,i){for(let r=0;r{const f=n();if(t.classList.contains("ilb-navigation-active")||f===null||t.parentNode===null)return;const u=Array.prototype.indexOf.call(t.parentNode.childNodes,t),m=u{t.stopPropagation()}),w.addEventListener("touchend",t=>{t.stopPropagation()})}function U(e){var n,o;for(let i=0;i{t=!0});function f(){return i}function u(){return[r,t]}function m(){return l}return{element:u,id:f,shouldAutoplay:m}}function Ee(){const e=[];function n(i){for(const r of i){const t=r.dataset.ilb2Video;t!==void 0&&e.push(ye(r,JSON.parse(t)))}}function o(i){return e.find(r=>r.id()===i)}return{add:n,get:o}}function Le(e,n,o){const i=[],r=Ee();let t=null,l=null;function f(){return n}function u(){return i}function m(a,s){if(l===null)return;const c=l;c.transitionOut(a,()=>{c.removeFromDOM(),s==null||s()})}function L(){l==null||l.transitionIn(X,$,d,p)}function S(a){l==null||l.addToDOM(a,()=>{var c;if(t===null)return;const s=i[t];if(e.caption&&re(s.dataset.ilb2Caption??((c=s.getElementsByTagName("img").item(0))==null?void 0:c.alt)??"",e.animationSpeed),L(),e.preloadNext&&t+1{S(s)},()=>{X()})}function p(a=!1){t!==null&&(e.activity&&j(),pe(),e.history&&!a&&Z(),y().dispatchEvent(new Event("ilb:quit",{bubbles:!0})),ne(),m(E.None,()=>{t=null,l=null,oe(),se(),ge(),te()}))}function A(a,s,c=!1){t!==null&&(e.history&&!c&&W(a,f(),u()),e.activity&&j(),U(a),m(s),M(a,s))}function $(){if(t===null)return;let a=t-1;if(t===0){if(e.quitOnEnd){p();return}a=i.length-1}i[a].dispatchEvent(new Event("ilb:previous",{bubbles:!0})),A(a,E.Left)}function d(){if(t===null)return;let a=t+1;if(t===i.length-1){if(e.quitOnEnd){p();return}a=0}i[a].dispatchEvent(new Event("ilb:next",{bubbles:!0})),A(a,E.Right)}function b(a,s=!1){t===null&&(_(e.animationSpeed,e.quitOnDocClick,p),e.activity&&j(),ve(e,p,$,d),e.arrows&&ie($,d),e.button&&ae(p),e.fullscreen&&le&&me(),e.overlay&&ee(),e.history&&!s&&W(a,f(),u()),i[a].dispatchEvent(new Event("ilb:start",{bubbles:!0})),M(a,E.None),e.navigation&&he(u(),()=>t,A,e.animationSpeed))}function h(a){const s=i.indexOf(a);s<0||b(s)}function D(a){const s=a.filter(c=>!i.includes(c)).filter(c=>c.tagName.toLowerCase()==="a"&&(new RegExp(`.(${e.allowedTypes})$`,"i").test(c.href)||c.dataset.ilb2Srcset!==void 0||c.dataset.ilb2Video!==void 0));r.add(s),i.push(...s);for(const c of s)c.addEventListener("click",q=>{q.preventDefault(),q.stopPropagation(),h(c)});J(s,()=>t,A,e.animationSpeed)}return D(o),e.history&&window.addEventListener("popstate",a=>{Y(a,f(),u(),t,b,p,A)}),{addImages:D,close:p,images:u,next:d,open:b,openWithImage:h,previous:$,set:f}}class xe{constructor(n,o){const i={activity:!0,allowedTypes:"",animationSpeed:250,arrows:!0,button:!0,caption:!1,enableKeyboard:!0,fullscreen:!0,gutter:10,history:!1,navigation:!1,overlay:!0,preloadNext:!0,quitOnDocClick:!0,quitOnEnd:!0,quitOnImgClick:!1,...o};this.s=Le(i,n.length>0?n[0].dataset.imagelightbox??"":"",Array.from(n)),i.history&&this.openHistory()}addImages(n){this.s.addImages(Array.from(n))}close(){this.s.close()}next(){this.s.next()}open(n){n!==void 0?this.s.openWithImage(n):this.s.open(0)}openHistory(){G(this.s.set(),this.s.images(),(n,o)=>{this.s.open(n,o)})}previous(){this.s.previous()}}v.fn.imageLightbox=function(e){const n=new xe(this.get(),e);return this.addToImageLightbox=o=>{n.addImages(o.get())},this.openHistory=()=>{n.openHistory()},this.loadPreviousImage=()=>{n.previous()},this.loadNextImage=()=>{n.next()},this.quitImageLightbox=function(){return n.close(),this},this.startImageLightbox=o=>{n.open(o==null?void 0:o.get(0))},this};const T=v(document);T.on("ilb:start",e=>{v("#ilb-container").trigger("start.ilb2",e.target)}),T.on("ilb:quit",()=>{v("#ilb-container").trigger("quit.ilb2")}),T.on("ilb:loaded",()=>{v("#ilb-container").trigger("loaded.ilb2")}),T.on("ilb:previous",e=>{v("#ilb-container").trigger("previous.ilb2",e.target)}),T.on("ilb:next",e=>{v("#ilb-container").trigger("next.ilb2",e.target)}),T.on("ilb:error",e=>{v("#ilb-container").trigger("error.ilb2",e.detail)})}); //# sourceMappingURL=imagelightbox.jquery.umd.cjs.map