(function(a,b){"function"==typeof define&&define.amd?define([],b):"object"==typeof module&&module.exports?module.exports=b():a.SimpleLightbox=b()})(this,function(){function a(a){for(let b=1;b<arguments.length;b++){const c=arguments[b];if(c)for(const b in c)c.hasOwnProperty(b)&&(a[b]=c[b])}return a}function b(a,b){a&&b&&(a.className+=" "+b)}function c(a,b){a&&b&&(a.className=a.className.replace(new RegExp("(\\s|^)"+b+"(\\s|$)")," ").trim())}function d(a){const b=document.createElement("div");return b.innerHTML=a.trim(),b.childNodes[0]}function f(a,b){return(a.matches||a.matchesSelector||a.msMatchesSelector).call(a,b)}function g(){return"innerHeight"in window?window.innerHeight:document.documentElement.offsetHeight}function h(){this.init.apply(this,arguments)}return h.defaults={elementClass:"",elementLoadingClass:"slbLoading",htmlClass:"slbActive",closeBtnClass:"",nextBtnClass:"",prevBtnClass:"",loadingTextClass:"",closeBtnCaption:"Close",nextBtnCaption:"Next",prevBtnCaption:"Previous",loadingCaption:"Loading...",bindToItems:!0,closeOnOverlayClick:!0,closeOnEscapeKey:!0,nextOnImageClick:!0,showCaptions:!0,captionAttribute:"title",urlAttribute:"href",startAt:0,loadingTimeout:100,appendTarget:"body",beforeSetContent:null,beforeClose:null,afterClose:null,beforeDestroy:null,afterDestroy:null,videoRegex:new RegExp(/youtube.com|youtu.be|vimeo.com/)},a(h.prototype,{init(b){b=this.options=a({},h.defaults,b);const c=this;let d;b.$items&&(d=b.$items.get()),b.elements&&(d=[].slice.call("string"==typeof b.elements?document.querySelectorAll(b.elements):b.elements)),this.eventRegistry={lightbox:[],thumbnails:[]},this.items=[],this.captions=[],d&&d.forEach(function(a,d){c.items.push(a.getAttribute(b.urlAttribute)),c.captions.push(a.getAttribute(b.captionAttribute)),b.bindToItems&&c.addEvent(a,"click",function(a){a.preventDefault(),c.showPosition(d)},"thumbnails")}),b.items&&(this.items=b.items),b.captions&&(this.captions=b.captions)},addEvent(a,b,c,d){return this.eventRegistry[d||"lightbox"].push({element:a,eventName:b,callback:c}),a.addEventListener(b,c),this},removeEvents(a){return this.eventRegistry[a].forEach(function(a){a.element.removeEventListener(a.eventName,a.callback)}),this.eventRegistry[a]=[],this},next(){return this.showPosition(this.currentPosition+1)},prev(){return this.showPosition(this.currentPosition-1)},normalizePosition(a){return a>=this.items.length?a=0:0>a&&(a=this.items.length-1),a},showPosition(a){const b=this.normalizePosition(a);return"undefined"!=typeof this.currentPosition&&(this.direction=b>this.currentPosition?"next":"prev"),this.currentPosition=b,this.setupLightboxHtml().prepareItem(this.currentPosition,this.setContent).show()},loading(a){const d=this,e=this.options;a?this.loadingTimeout=setTimeout(function(){b(d.$el,e.elementLoadingClass),d.$content.innerHTML="<p class=\"slbLoadingText "+e.loadingTextClass+"\">"+e.loadingCaption+"</p>",d.show()},e.loadingTimeout):(c(this.$el,e.elementLoadingClass),clearTimeout(this.loadingTimeout))},getVideoURL(a){const b=[{rx:/^(?:https?:)?\/\/(?:www\.)?vimeo\.com\/([^\?&"]+).*$/g,tmpl:"//_URL_".replace("_URL_","player.vimeo.com/video/$1")},{rx:/^.*(?:https?:\/\/)?(?:www\.)?(?:youtube\.com|youtu\.be)\/(?:watch\?v=|embed\/|v\/|user\/.+\/)?([^\?&"]+).*$/g,tmpl:"//_URL_".replace("_URL_","www.youtube.com/embed/$1")},{rx:/^.*(?:https?:\/\/)?(?:www\.)?(?:youtube-nocookie\.com)\/(?:watch\?v=|embed\/|v\/|user\/.+\/)?([^\?&"]+).*$/g,tmpl:"//_URL_".replace("_URL_","www.youtube-nocookie.com/embed/$1")}];for(let c=0;c<b.length;c++)if(b[c].rx.test(a))return a.replace(b[c].rx,b[c].tmpl);return a},prepareItem(a,b){const c=this,e=this.items[a];if(this.loading(!0),this.options.videoRegex.test(e)){const a=this.getVideoURL(e);b.call(c,d("<div class=\"slbIframeCont\"><iframe class=\"slbIframe\" frameborder=\"0\" allowfullscreen src=\""+a+"\"></iframe></div>"))}else{const f=d("<div class=\"slbImageWrap\"><img class=\"slbImage\" src=\""+e+"\" /></div>");this.$currentImage=f.querySelector(".slbImage"),this.options.showCaptions&&this.captions[a]&&f.appendChild(d("<div class=\"slbCaption\">"+this.captions[a]+"</div>")),this.loadImage(e,function(){c.setImageDimensions(),b.call(c,f),c.loadImage(c.items[c.normalizePosition(c.currentPosition+1)])})}return this},loadImage(a,b){if(!this.options.videoRegex.test(a)){const c=new Image;b&&(c.onload=b),c.src=a}},setupLightboxHtml(){const a=this.options;return this.$el||(this.$el=d("<div class=\"slbElement "+a.elementClass+"\"><div class=\"slbOverlay\"></div><div class=\"slbWrapOuter\"><div class=\"slbWrap\"><div class=\"slbContentOuter\"><div class=\"slbContent\"></div><button type=\"button\" title=\""+a.closeBtnCaption+"\" class=\"slbCloseBtn "+a.closeBtnClass+"\">\xD7</button>"+(1<this.items.length?"<div class=\"slbArrows\"><button type=\"button\" title=\""+a.prevBtnCaption+"\" class=\"prev slbArrow"+a.prevBtnClass+"\">"+a.prevBtnCaption+"</button><button type=\"button\" title=\""+a.nextBtnCaption+"\" class=\"next slbArrow"+a.nextBtnClass+"\">"+a.nextBtnCaption+"</button></div>":"")+"</div></div></div></div>"),this.$content=this.$el.querySelector(".slbContent")),this.$content.innerHTML="",this},show(){return this.modalInDom||(document.querySelector(this.options.appendTarget).appendChild(this.$el),b(document.documentElement,this.options.htmlClass),this.setupLightboxEvents(),this.modalInDom=!0),this},setContent(a){const e="string"==typeof a?d(a):a;return this.loading(!1),this.setupLightboxHtml(),c(this.$content,"slbDirectionNext"),c(this.$content,"slbDirectionPrev"),this.direction&&b(this.$content,"next"===this.direction?"slbDirectionNext":"slbDirectionPrev"),this.options.beforeSetContent&&this.options.beforeSetContent(e,this),this.$content.appendChild(e),this},setImageDimensions(){this.$currentImage&&(this.$currentImage.style.maxHeight=g()+"px")},setupLightboxEvents(){const a=this;return this.eventRegistry.lightbox.length?this:(this.addEvent(this.$el,"click",function(b){const c=b.target;f(c,".slbCloseBtn")||a.options.closeOnOverlayClick&&f(c,".slbWrap")?a.close():f(c,".slbArrow")?f(c,".next")?a.next():a.prev():a.options.nextOnImageClick&&1<a.items.length&&f(c,".slbImage")&&a.next()}).addEvent(document,"keyup",function(b){a.options.closeOnEscapeKey&&27===b.keyCode&&a.close(),1<a.items.length&&((39===b.keyCode||68===b.keyCode)&&a.next(),(37===b.keyCode||65===b.keyCode)&&a.prev())}).addEvent(window,"resize",function(){a.setImageDimensions()}),this)},close(){this.modalInDom&&(this.runHook("beforeClose"),this.removeEvents("lightbox"),this.$el&&this.$el.parentNode.removeChild(this.$el),c(document.documentElement,this.options.htmlClass),this.modalInDom=!1,this.runHook("afterClose")),this.direction=void 0,this.currentPosition=this.options.startAt},destroy(){this.close(),this.runHook("beforeDestroy"),this.removeEvents("thumbnails"),this.runHook("afterDestroy")},runHook(a){this.options[a]&&this.options[a](this)}}),h.open=function(a){const b=new h(a);return a.content?b.setContent(a.content).show():b.showPosition(b.options.startAt)},h.registerAsJqueryPlugin=function(a){a.fn.simpleLightbox=function(b){let c;const d=this;return this.each(function(){a.data(this,"simpleLightbox")||(c=c||new h(a.extend({},b,{$items:d})),a.data(this,"simpleLightbox",c))})},a.SimpleLightbox=h},"undefined"!=typeof window&&window.jQuery&&h.registerAsJqueryPlugin(window.jQuery),h});