/**
* Mosaiqy 1.0.1 for jQuery 1.6+
* @author Fabrizio Calderan, http://www.fabriziocalderan.it/mosaiqy
* Released under license Creative Commons, Attribution-NoDerivs 3.0
* (CC BY-ND 3.0) available at http://creativecommons.org/licenses/by-nd/3.0/
* Read the license carefully before using this plugin. Do not remove this comment.
*/
;(function(d){var t=function(b,a){var e=document.createElement("div"),w,c={msie:"MsTransition",opera:"OTransition",mozilla:"MozTransition",webkit:"WebkitTransition"},d;for(d in c)c.hasOwnProperty(d)&&b[d]&&(w=c[d]);return{isEnabled:!(!e.style[a]&&!(w in e.style||b.opera&&parseFloat(b.version)>10.49)),transitionEnd:b.opera?"oTransitionEnd":b.webkit?"webkitTransitionEnd":"transitionend",duration:function(a){return a.replace(/([A-Z])/g,function(a,b){return"-"+b.toLowerCase()})}(w)+"-duration"}}(d.browser, "transition"),A=function(b){var a,e,d,c=[];for(a=0;a<b;a++)c[a]=a;for(;--b;)a=~~(Math.random()*(b+1)),e=c[b],d=c[a],c[b]=d,c[a]=e;return c},r=function(b){var a={animationDelay:3E3,animationSpeed:800,avoidDuplicates:!1,cols:2,fadeSpeed:750,indexData:0,loadTimeout:7500,loop:!0,onCloseZoom:null,onOpenZoom:null,openZoom:!0,rows:2,scrollZoom:!0,template:""},e,d,c,n,g=[],l=[],m={},h=!1,k=!1,o,p,t=b.browser.opera?b("html"):b("html,body"),z=function(c){var i="";typeof m[c]==="undefined"&&(m[c]=a.template.replace(/\$\{([^\}]+)\}/gm, function(b,i){var d=function(){var b=i.split("."),d=0,e;if(b.length){e=a.data[c];b=b.reverse();for(d=b.length;d--;)e=e[b[d]]||{};return typeof e==="string"?e:""}return a.data[c][i]}();return typeof d==="undefined"?i:d}));i=m[c];typeof window.innerShiv==="function"&&(i=window.innerShiv(i,!1));return b(i)},r=function(){var f=c.eq(0);o=f.outerWidth(!0);p=f.outerHeight(!0);c.each(function(c,d){b(d).css({top:p*~~(c/a.cols),left:o*(c%a.cols)})});d.css({minHeight:p*a.rows,width:o*a.cols});e.css({minHeight:p* a.rows,width:o*a.cols})},B=function(){var b,c,d;for(c=0;c<a.cols;c+=1)d="li:nth-child($0n+$1)".replace(/\$(\d)/g,function(b,d){return[a.cols,c+1][d]}),g.push({prop:"top",selector:d,node:c,position:{top:-p,left:o*c}}),g.push({prop:"top",selector:d,node:a.cols*(a.rows-1)+c,position:{top:p*a.rows,left:o*c}});for(b=0,c=0;c<a.rows;c+=1)d="li:nth-child(n+$0):nth-child(-n+$1)".replace(/\$(\d)/g,function(c,d){return[b+1,b+a.cols][d]}),g.push({prop:"left",selector:d,node:b,position:{top:p*c,left:-o}}),g.push({prop:"left", selector:d,node:b+=a.cols,position:{top:p*c,left:o*a.cols}});g[g.length-1].node-=1},C=function(){var f,i,h,q,j,x=b.Deferred();f=l.pop();j=(f&1)===0;q=e.find(g[f].selector);i=c.eq(g[f].node);h=f<g.length-1?b("<li />").insertBefore(i):b("<li />").insertAfter(i);h.data("mosaiqy-index",a.indexData);z(a.indexData).appendTo(h.css(g[f].position));b.when(h.find("img").mosaiqyImagesLoad(a.loadTimeout)).fail(function(){h.remove();x.reject()}).done(function(){var i=g[f].prop,l=i==="left"?o:p,m=q.add(h),k=m.length, n={};n[i]="+="+(j?l:-l)+"px";m.animate(n,a.animationSpeed,function(){var h;if(!--k){j?q.last().remove():q.first().remove();q=j?q.slice(0,q.length-1):q.slice(1,q.length);if(i==="top")h=q.length,q.each(function(i){var f,v;c=e.find("li:not(.mosaiqy-zoom)");f=b(this);v=c.index(f);if(i=j?a.cols:-(a.cols-(1===h-i?0:1)))v+=i,v<c.length?f.insertBefore(c.eq(v)):f.appendTo(d)});x.resolve()}})});return x.promise()},s=function(){!h&&!k?(k=!0,l.length===0&&(l=A(g.length)),D(),b.when(C()).then(function(){a.indexData+= 1;k=!1}).fail(function(){s()}).done(function(){c=d.find("li:not(.mosaiqy-zoom)");setTimeout(function(){s()},a.animationDelay)})):setTimeout(function(){s()},a.animationDelay*2)},D=function(){var d=a.data.length,e=[];if(a.indexData===a.data.length)if(a.loop)a.indexData=0;else{h=!0;return}if(a.avoidDuplicates)for(c.each(function(){var a=b(this).data("mosaiqy-index");e[a]=a});d--;)if(typeof e[a.indexData]!=="undefined"){if(a.indexData+=1,a.indexData===a.data.length)if(a.loop)a.indexData=0;else{h=!0;break}}else break}, E=function(f){function i(){var d=b.Deferred();(j||{}).length?(p.stop(!0)._animate({opacity:"0"},a.fadeSpeed/4),s.stop(!0)._animate({opacity:"0"},a.fadeSpeed/2),c.removeClass("zoom"),b.when(j.stop(!0)._animate({height:"0"},a.fadeSpeed)).then(function(){j.remove();j=null;if(typeof a.onCloseZoom==="function")a.onCloseZoom(g);d.resolve()})):d.resolve();return d.promise()}function l(){var c,d,f;o=j.find("figure");p=j.find("figcaption");c=b('<img class="mosaiqy-zoom-image" />').attr({src:g.find("a").attr("href")}); c.appendTo(o);c.get(0).height===0&&c.hide();f=c.get(0).complete?c.height():200;j._animate({height:f+"px"},a.fadeSpeed,function(){if(typeof a.onOpenZoom==="function")a.onOpenZoom(g)});(d=g.find("img").prop("longDesc"))&&c.wrap(b("<a />").attr({href:d,target:"new"}));s=b('<a class="mosaiqy-zoom-close">Close</a>').attr({href:"#"}).bind("click.mosaiqy",function(a){b.when(i()).then(function(){e.removeClass("zoom");h=n=!1});a.preventDefault()}).appendTo(o);b.when(c.mosaiqyImagesLoad(a.loadTimeout,function(b){setTimeout(function(){var d= c.get(0).height?a.fadeSpeed:0;b.fadeIn(d,function(){s._animate({opacity:"1"},a.fadeSpeed/2);p.html(g.find("figcaption").html())._animate({opacity:"1"},a.fadeSpeed)})},a.fadeSpeed/1.2)})).done(function(){f<c.height()&&j._animate({height:c.height()+"px"},a.fadeSpeed)}).fail(function(){s.trigger("click.mosaiqy")})}function q(f){n=!0;b.when(f()).done(function(){var f;e.addClass("zoom");g.addClass("zoom");c=e.find("li:not(.mosaiqy-zoom)");u=g.offset().top;r=document.body.scrollTop!==0?document.body.scrollTop: document.documentElement.scrollTop;a.scrollZoom?(y=Math.abs(r-u),f=y>0?y*1.5+400:0):(u=r,f=0);j='<li class="mosaiqy-zoom"><figure><figcaption></figcaption></figure></li>';j=typeof window.innerShiv==="function"?b(innerShiv(j,!1)):b(j);m<c.length?j.insertBefore(c.eq(m)):j.appendTo(d);b.when(t.stop()._animate({scrollTop:u},f)).done(function(){n=!1;l()})})}var j,g,m,n,o,p,s,r,u,y;f.live("click.mosaiqy",function(d){!k&&!n?(h=!0,g=b(this),m=a.cols*Math.ceil((c.index(g)+1)/a.cols),a.openZoom&&!g.hasClass("zoom")&& (d.preventDefault(),q(i))):d.preventDefault()})},F=function(c){for(var e;c--;)e=z(a.indexData),e.appendTo(b("<li />").appendTo(d)),a.indexData+=1};return{init:function(f,g){var l=0;a=b.extend(a,g);if(!(a.data||[]).length)throw Error("Data object is empty");if(a.template&&b("#"+a.template).is("script"))a.template=b("#"+a.template).text()||b("#"+a.template).html();else throw Error("User template is not defined");e=f;d=f.find("ul");c=f.find("li:not(.mosaiqy-zoom)");if(l=a.cols*a.rows-c.length)if(a.data.length>= l)a.indexData=c.length,F(l),c=f.find("li:not(.mosaiqy-zoom)");else throw Error("Mosaiqy can't find missing images on JSON data.");a.avoidDuplicates&&c.each(function(a){typeof b(this).data("mosaiqy-index")==="undefined"&&b(this).data("mosaiqy-index",a)});n=f.find("img");r();B();e.delegate("ul","mouseenter.mosaiqy",function(){h=!0}).delegate("ul","mouseleave.mosaiqy",function(){e.hasClass("zoom")||(h=!1)});b.when(n.mosaiqyImagesLoad(a.loadTimeout,function(c){c.animate({opacity:"1"},a.fadeSpeed)})).done(function(){e.removeClass("loading"); E(c);setTimeout(function(){s()},a.animationDelay)}).fail(function(){return!1});return this}}},k=d.sub();k.fn.mosaiqyImagesLoad=function(b,a){var e=d.Deferred(),k=this.length,c=[],n=[],g=b||8419.78;k&&this.each(function(){var b=this;d.when(function(){var a=d.Deferred(),c=setTimeout(function(){d(b).trigger("error.mosaiqy")},g);d(b).one("load.mosaiqy",function(){clearInterval(c);a.resolve()}).bind("error.mosaiqy",function(){clearInterval(c);a.reject()}).attr("src",b.src);b.complete&&setTimeout(function(){d(b).trigger("load.mosaiqy")}, 10);return a.promise()}()).done(function(){c.push(b.src);a&&a(d(b))}).fail(function(){n.push(b.src)}).always(function(){k-=1;k===0&&(n.length?e.reject():e.resolve())})});return e.promise()};k.fn.extend({_animate:d.fn.animate,animate:function(b,a,e,r){var c=a&&typeof a==="object"?d.extend({},a):{duration:a,complete:r||!r&&e||d.isFunction(a)&&a,easing:r&&e||e&&!d.isFunction(e)&&e};return d(this).each(function(){var e=k(this),g=e.position(),l={},m;if(t.isEnabled){if(typeof b==="object")for(var h in b)if(h=== "left"||h==="top")(m=b[h].match(/^(?:\+|\-)=(\-?\d+)/))&&m.length&&(l[h]=g[h]+parseInt(m[1],10));e.bind(t.transitionEnd,function(){d.isFunction(c.complete)&&c.complete()}).css(l).css(t.duration,a/1E3+"s")}else e._animate(b,c)})}});d.fn.mosaiqy=function(b){if(this.length)return this.each(function(){var a=new r(k);a.init(k(this),b);d.data(this,"mosaiqy",a)})}})(jQuery);
