/*! yalb - v0.1.2 - 2015-06-24 * https://github.com/Fuzzyma/yalb * Copyright (c) 2015 Ulrich-Matthias Schäfer; Licensed MIT */ !function(){function a(a,c){b(a,c),a.className+=" "+c}function b(a,b){a.className=a.className.replace(new RegExp("(\\s|^)"+b+"(\\s|$)"),"")}var c="transition"in document.createElement("p").style,d=null,e=function(f,g){if(!(this instanceof e))return d&&d.yalb.dispatchEvent(new Event("close")),new e(f,g);d=this;var h={};g=g||{};for(var i in e.defaults)h[i]=g[i]||e.defaults[i];var j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A=[],B=h.current,C=document.createElement("div"),D=document.createElement("div");j=function(a){var b=document.createElement("span");return b.addEventListener("click",function(){C.dispatchEvent(new Event(a))},!1),b.className=a,b},k=function(){a(C.querySelector(".loader"),"fadeOut"),a(C.querySelector(".error"),"fadeOut")},l=function(){h.loop&&f.length>1||(B===f.length-1?a(C.querySelector(".next"),"hide"):b(C.querySelector(".next"),"hide"),0===B?a(C.querySelector(".prev"),"hide"):b(C.querySelector(".prev"),"hide"))},m=function(){b(C.querySelector(".loader"),"fadeOut")},n=function(){a(C.querySelector(".image"),"fadeOut"),a(C.querySelector(".loader"),"fadeOut"),b(C.querySelector(".error"),"fadeOut"),q([B-1,B+1])},o=function(a){if(!a||a.target===C||"width"===a.propertyName){var c=A[B].img;c.className="image fadeOut",c.addEventListener("transitionend",function(){0==window.getComputedStyle(c).getPropertyValue("opacity")&&C.removeChild(c)},!1),C.appendChild(c),setTimeout(function(){b(c,"fadeOut")},0),C.dispatchEvent(new CustomEvent("change",{detail:{index:B}})),q([B-1,B+1])}},p=function(a){if("string"==typeof a)return a;if(a instanceof Node&&0===h.src.indexOf("data-"))return a.getAttribute(h.src);for(var b=h.src.split("."),c=0,d=b.length;d>c;++c)a=a[b[c]];return a},q=function(a){a=a||[B];for(var b=a.length;b--;)h.loop&&a[b]<0&&(a[b]+=A.length),a[b]>=A.length||a[b]<0||A[a[b]].loaded||A[a[b]].pending||A[a[b]].error||(A[a[b]].img.src=p(f[a[b]]),A[a[b]].pending=!0)},r=function(){l(),A[B].loaded?s():A[B].error?n():(m(),q())},s=function(){if(k(),!C.querySelector(".image")||C.querySelector(".image").src!==A[B].img.src){C.querySelector(".image")&&a(C.querySelector(".image"),"fadeOut");var b=A[B].img.naturalWidth/A[B].img.naturalHeight,d=A[B].img.naturalHeight,e=A[B].img.naturalWidth,f=h.height||window.innerHeight-40,g=h.width||window.innerWidth-40;d>f&&(d=f,e=b*f),e>g&&(e=g,d=g/b),Math.abs(parseInt(window.getComputedStyle(C).getPropertyValue("width"))-e)>1||Math.abs(parseInt(window.getComputedStyle(C).getPropertyValue("height"))-d)>1?(C.style.width=e+"px",C.style.height=d+"px",C.style.bottom=(d-window.innerHeight)/2+"px",c||o()):o()}},t=function(a){return function(){A[a].error=!0,a===B&&r()}},u=function(a){return function(){A[a].loaded=!0,a===B&&r()}},v=function(){B>0?(--B,r()):h.loop&&(B=A.length-1,r())},w=function(){Bi;++i)A[i]={img:new Image,loaded:!1,pending:!1,error:!1},A[i].img.onload=u(i),A[i].img.onerror=t(i);return h.open&&(y(),r()),e};e.prev=function(){return d?(d.yalb.dispatchEvent(new Event("prev")),e):e},e.next=function(){return d?(d.yalb.dispatchEvent(new Event("next")),e):e},e.close=function(){return d?(d.yalb.dispatchEvent(new Event("close")),e):e},e.show=function(a){return d?(d.yalb.dispatchEvent(new CustomEvent("show",{detail:{index:a}})),e):e},e.open=function(){return d?(d.yalb.dispatchEvent(new Event("open")),e):e},e.get=function(){return d?d.yalb:e},e.defaults={src:"src",current:0,"class":"yalb",loop:!0,open:!0,width:0,height:0},window.yalb=e}(),function(){function a(a,b){b=b||{bubbles:!1,cancelable:!1,detail:void 0};var c=document.createEvent("CustomEvent");return c.initCustomEvent(a,b.bubbles,b.cancelable,b.detail),c}a.prototype=window.Event.prototype,window.CustomEvent=a,window.Event=a}();