/*! imagelightbox - v0.1.0 - 2014-06-10
* https://github.com/victorhaggqvist/imagelightbox
* Copyright (c) 2014 Osvaldas Valutis (www.osvaldas.info); Licensed MIT
* Contributors Victor Häggqvist (victorhaggqvist.com) */
!function(a,b,c){"use strict";var d=function(){var a=c.body||c.documentElement,b=a.style;return""===b.WebkitTransition?"-webkit-":""===b.MozTransition?"-moz-":""===b.OTransition?"-o-":""===b.transition?"":!1},e=d()===!1?!1:!0,f=function(a,b,c){var e={},f=d();e[f+"transform"]="translateX("+b+")",e[f+"transition"]=f+"transform "+c+"s linear",a.css(e)},g="ontouchstart"in b,h=b.navigator.pointerEnabled||b.navigator.msPointerEnabled,i=function(a){if(g)return!0;if(!h||"undefined"==typeof a||"undefined"==typeof a.pointerType)return!1;if("undefined"!=typeof a.MSPOINTER_TYPE_MOUSE){if(a.MSPOINTER_TYPE_MOUSE!==a.pointerType)return!0}else if("mouse"!==a.pointerType)return!0;return!1};a.fn.imageLightbox=function(d){var d=a.extend({selector:'id="imagelightbox"',allowedTypes:"png|jpg|jpeg|gif",animationSpeed:250,preloadNext:!0,enableKeyboard:!0,quitOnEnd:!1,quitOnImgClick:!1,quitOnDocClick:!0,onStart:!1,onEnd:!1,onLoadStart:!1,onLoadEnd:!1},d),j=a([]),k=a(),l=a(),m=0,n=0,o=0,p=!1,q=function(b){return"a"===a(b).prop("tagName").toLowerCase()&&new RegExp(".("+d.allowedTypes+")$","i").test(a(b).attr("href"))},r=function(){if(!l.length)return!1;var c=.8*a(b).width(),d=.9*a(b).height(),e=new Image;e.src=l.attr("src"),e.onload=function(){if(m=e.width,n=e.height,m>c||n>d){var f=m/n>c/d?m/c:n/d;m/=f,n/=f}l.css({width:m+"px",height:n+"px",top:(a(b).height()-n)/2+"px",left:(a(b).width()-m)/2+"px"})}},s=function(b){if(p)return!1;if(b="undefined"==typeof b?!1:"left"===b?1:-1,l.length){if(b!==!1&&(j.length<2||d.quitOnEnd===!0&&(-1===b&&0===j.index(k)||1===b&&j.index(k)===j.length-1)))return u(),!1;var c={opacity:0};e?f(l,100*b-o+"px",d.animationSpeed/1e3):c.left=parseInt(l.css("left"))+100*b+"px",l.animate(c,d.animationSpeed,function(){t()}),o=0}p=!0,d.onLoadStart!==!1&&d.onLoadStart(),setTimeout(function(){l=a("").attr("src",k.attr("href")).load(function(){l.appendTo("body"),r();var c={opacity:1};if(l.css("opacity",0),e)f(l,-100*b+"px",0),setTimeout(function(){f(l,"0px",d.animationSpeed/1e3)},50);else{var g=parseInt(l.css("left"));c.left=g+"px",l.css("left",g-100*b+"px")}if(l.animate(c,d.animationSpeed,function(){p=!1,d.onLoadEnd!==!1&&d.onLoadEnd()}),d.preloadNext){var h=j.eq(j.index(k)+1);h.length||(h=j.eq(0)),a("").attr("src",h.attr("href")).load()}}).error(function(){d.onLoadEnd!==!1&&d.onLoadEnd()});var c=0,g=0,n=0;l.on(h?"pointerup MSPointerUp":"click",function(a){if(a.preventDefault(),d.quitOnImgClick)return u(),!1;if(i(a.originalEvent))return!0;var b=(a.pageX||a.originalEvent.pageX)-a.target.offsetLeft;k=j.eq(j.index(k)-(m/2>b?1:-1)),k.length||(k=j.eq(m/2>b?j.length:0)),s(m/2>b?"left":"right")}).on("touchstart pointerdown MSPointerDown",function(a){return!i(a.originalEvent)||d.quitOnImgClick?!0:(e&&(n=parseInt(l.css("left"))),void(c=a.originalEvent.pageX||a.originalEvent.touches[0].pageX))}).on("touchmove pointermove MSPointerMove",function(a){return!i(a.originalEvent)||d.quitOnImgClick?!0:(a.preventDefault(),g=a.originalEvent.pageX||a.originalEvent.touches[0].pageX,o=c-g,void(e?f(l,-o+"px",0):l.css("left",n-o+"px")))}).on("touchend touchcancel pointerup MSPointerUp",function(a){return!i(a.originalEvent)||d.quitOnImgClick?!0:void(Math.abs(o)>50?(k=j.eq(j.index(k)-(0>o?1:-1)),k.length||(k=j.eq(0>o?j.length:0)),s(o>0?"right":"left")):e?f(l,"0px",d.animationSpeed/1e3):l.animate({left:n+"px"},d.animationSpeed/2))})},d.animationSpeed+100)},t=function(){return l.length?(l.remove(),void(l=a())):!1},u=function(){return l.length?void l.animate({opacity:0},d.animationSpeed,function(){t(),p=!1,d.onEnd!==!1&&d.onEnd()}):!1};return a(b).on("resize",r),d.quitOnDocClick&&a(c).on(g?"touchend":"click",function(b){l.length&&!a(b.target).is(l)&&u()}),d.enableKeyboard&&a(c).on("keyup",function(a){return l.length?(a.preventDefault(),27===a.keyCode&&u(),void((37===a.keyCode||39===a.keyCode)&&(k=j.eq(j.index(k)-(37===a.keyCode?1:-1)),k.length||(k=j.eq(37===a.keyCode?j.length:0)),s(37===a.keyCode?"left":"right")))):!0}),a(c).on("click",this.selector,function(b){return q(this)?(b.preventDefault(),p?!1:(p=!1,d.onStart!==!1&&d.onStart(),k=a(this),void s())):!0}),this.each(function(){return q(this)?void(j=j.add(a(this))):!0}),this.switchImageLightbox=function(a){var b=j.eq(a);if(b.length){var c=j.index(k);k=b,s(c>a?"left":"right")}return this},this.quitImageLightbox=function(){return u(),this},this}}(jQuery,window,document);