(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(var b,c=1;c<arguments.length;c++)if(b=arguments[c],b)for(var d in b)b.hasOwnProperty(d)&&(a[d]=b[d]);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){var 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:function(b){b=this.options=a({},h.defaults,b);var c,d=this;b.$items&&(c=b.$items.get()),b.elements&&(c=[].slice.call("string"==typeof b.elements?document.querySelectorAll(b.elements):b.elements)),this.eventRegistry={lightbox:[],thumbnails:[]},this.items=[],this.captions=[],c&&c.forEach(function(a,c){d.items.push(a.getAttribute(b.urlAttribute)),d.captions.push(a.getAttribute(b.captionAttribute)),b.bindToItems&&d.addEvent(a,"click",function(a){a.preventDefault(),d.showPosition(c)},"thumbnails")}),b.items&&(this.items=b.items),b.captions&&(this.captions=b.captions)},addEvent:function(a,b,c,d){return this.eventRegistry[d||"lightbox"].push({element:a,eventName:b,callback:c}),a.addEventListener(b,c),this},removeEvents:function(a){return this.eventRegistry[a].forEach(function(a){a.element.removeEventListener(a.eventName,a.callback)}),this.eventRegistry[a]=[],this},next:function(){return this.showPosition(this.currentPosition+1)},prev:function(){return this.showPosition(this.currentPosition-1)},normalizePosition:function(a){return a>=this.items.length?a=0:0>a&&(a=this.items.length-1),a},showPosition:function(a){var 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:function(a){var 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:function(a){for(var b="//_URL_",c=[{rx:/^(?:https?:)?\/\/(?:www\.)?vimeo\.com\/([^\?&"]+).*$/g,tmpl:b.replace("_URL_","player.vimeo.com/video/$1")},{rx:/^.*(?:https?:\/\/)?(?:www\.)?(?:youtube\.com|youtu\.be)\/(?:watch\?v=|embed\/|v\/|user\/.+\/)?([^\?&"]+).*$/g,tmpl:b.replace("_URL_","www.youtube.com/embed/$1")},{rx:/^.*(?:https?:\/\/)?(?:www\.)?(?:youtube-nocookie\.com)\/(?:watch\?v=|embed\/|v\/|user\/.+\/)?([^\?&"]+).*$/g,tmpl:b.replace("_URL_","www.youtube-nocookie.com/embed/$1")}],d=0;d<c.length;d++)if(c[d].rx.test(a))return a.replace(c[d].rx,c[d].tmpl);return a},prepareItem:function(a,b){var c=this,e=this.items[a];if(this.loading(!0),this.options.videoRegex.test(e)){var f=this.getVideoURL(e);b.call(c,d("<div class=\"slbIframeCont\"><iframe class=\"slbIframe\" frameborder=\"0\" allowfullscreen src=\""+f+"\"></iframe></div>"))}else{var g=d("<div class=\"slbImageWrap\"><img class=\"slbImage\" src=\""+e+"\" /></div>");this.$currentImage=g.querySelector(".slbImage"),this.options.showCaptions&&this.captions[a]&&g.appendChild(d("<div class=\"slbCaption\">"+this.captions[a]+"</div>")),this.loadImage(e,function(){c.setImageDimensions(),b.call(c,g),c.loadImage(c.items[c.normalizePosition(c.currentPosition+1)])})}return this},loadImage:function(a,b){if(!this.options.videoRegex.test(a)){var c=new Image;b&&(c.onload=b),c.src=a}},setupLightboxHtml:function(){var 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:function(){return this.modalInDom||(document.querySelector(this.options.appendTarget).appendChild(this.$el),b(document.documentElement,this.options.htmlClass),this.setupLightboxEvents(),this.modalInDom=!0),this},setContent:function(a){var 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:function(){this.$currentImage&&(this.$currentImage.style.maxHeight=g()+"px")},setupLightboxEvents:function(){var a=this;return this.eventRegistry.lightbox.length?this:(this.addEvent(this.$el,"click",function(b){var 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:function(){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:function(){this.close(),this.runHook("beforeDestroy"),this.removeEvents("thumbnails"),this.runHook("afterDestroy")},runHook:function(a){this.options[a]&&this.options[a](this)}}),h.open=function(a){var 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){var c,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});