/**
 * Mosaiqy for jQuery 1.0.0
 * @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.
 * Please don't remove this comment
 */
(function(d){var a=(function(h,l){var k=document.createElement("div"),i=function(m){return m.replace(/([A-Z])/g,function(n,o){return"-"+o.toLowerCase()})},g,j={msie:"MsTransition",opera:"OTransition",mozilla:"MozTransition",webkit:"WebkitTransition"};for(var f in j){if(j.hasOwnProperty(f)){if(h[f]){g=j[f]}}}return{isEnabled:(function(m){return !!(m[l]||g in m||(h.opera&&parseFloat(h.version)>10.49))}(k.style)),transitionEnd:(function(){return(h.opera)?"oTransitionEnd":(h.webkit)?"webkitTransitionEnd":"transitionend"}()),duration:i(g)+"-duration"}}(d.browser,"transition")),b=function(g){var l,h,m,k,f=[];l=g;for(h=0;h<l;h++){f[h]=h}while(--l){h=~~(Math.random()*(l+1));m=f[l];k=f[h];f[l]=k;f[h]=m}return f},c=function(o){var A={animationDelay:3000,animationSpeed:800,avoidDuplicates:false,cols:2,fadeSpeed:750,indexData:0,loadTimeout:7500,loop:true,rows:2,scrollZoom:true,template:""},B,i,h,k,l=[],q=[],C={},s=false,r=false,u={},y=(o.browser.opera)?o("html"):o("html,body"),j,g=function(E){var D="";if(typeof C[E]==="undefined"){C[E]=A.template.replace(/\$\{([^\}]+)\}/gm,function(H,F){var G=(function(){var J=F.split("."),I=0,K;if(J.length){K=A.data[E];J=J.reverse();I=J.length;while(I--){K=K[J[I]]||{}}return(typeof K==="string")?K:""}return A.data[E][F]}());if(typeof G==="undefined"){return F}return G})}D=C[E];if(typeof window.innerShiv==="function"){D=window.innerShiv(D,false)}return o(D)},x=function(){var D=h.eq(0);u={w:D.outerWidth(true),h:D.outerHeight(true)};h.each(function(E,F){o(F).css({top:u.h*(~~(E/A.cols)),left:u.w*(E%A.cols)})});i.css({minHeight:u.h*A.rows,width:u.w*A.cols});B.css({minHeight:u.h*A.rows,width:u.w*A.cols})},f=function(){var G,F,E,D={col:"li:nth-child($0n+$1)",row:"li:nth-child(n+$0):nth-child(-n+$1)"};for(F=0;F<A.cols;F=F+1){E=D.col.replace(/\$(\d)/g,function(H,I){return[A.cols,F+1][I]});l.push({prop:"top",selector:E,node:F,position:{top:-(u.h),left:u.w*F}});l.push({prop:"top",selector:E,node:A.cols*(A.rows-1)+F,position:{top:u.h*A.rows,left:u.w*F}})}for(G=0,F=0;F<A.rows;F=F+1){E=D.row.replace(/\$(\d)/g,function(H,I){return[G+1,G+A.cols][I]});l.push({prop:"left",selector:E,node:G,position:{top:u.h*F,left:-(u.w)}});l.push({prop:"left",selector:E,node:G+=A.cols,position:{top:u.h*F,left:u.w*A.cols}})}l[l.length-1].node-=1},z=function(){var H,G,F,I,E,J,D=o.Deferred();H=q.pop();J=((H&1)===0);E=B.find(l[H].selector);F=h.eq(l[H].node);I=(H<l.length-1)?o("<li />").insertBefore(F):o("<li />").insertAfter(F);I.data("mosaiqy-index",A.indexData);G=g(A.indexData);G.appendTo(I.css(l[H].position));o.when(I.find("img").mosaiqyImagesLoad(A.loadTimeout)).fail(function(){I.remove();D.reject()}).done(function(){var O=l[H].prop,N=(O==="left")?u.w:u.h,M=E.add(I),K=M.length,L={};L[O]="+="+(J?N:-N)+"px";M.animate(L,A.animationSpeed,function(){var P;if(--K){return}if(J){E.last().remove()}else{E.first().remove()}E=(J)?E.slice(0,E.length-1):E.slice(1,E.length);if(O==="top"){P=E.length;E.each(function(Q){var R,U,T,S;h=B.find("li:not(.mosaiqy-zoom)");R=o(this);U=h.index(R);T=(J)?A.cols:-(A.cols-((1===P-Q)?0:1));if(!!T){S=U+T;if(S<h.length){R.insertBefore(h.eq(S))}else{R.appendTo(i)}}})}D.resolve()})});return D.promise()},m=function(){if(!s&&!r){r=true;if(q.length===0){q=b(l.length)}w();o.when(z()).then(function(){A.indexData=A.indexData+1;r=false}).fail(function(){m()}).done(function(){h=i.find("li:not(.mosaiqy-zoom)");j=setTimeout(function(){m()},A.animationDelay)})}else{j=setTimeout(function(){m()},A.animationDelay*2)}},p=function(){s=true},v=function(){s=false},w=function(){var E=A.data.length,D=[];if(A.indexData===A.data.length){if(!A.loop){return p()}else{A.indexData=0}}if(A.avoidDuplicates){h.each(function(){var F=o(this).data("mosaiqy-index");D[F]=F});while(E--){if(typeof D[A.indexData]!=="undefined"){A.indexData=A.indexData+1;if(A.indexData===A.data.length){if(!A.loop){return p()}else{A.indexData=0}}continue}break}}},t=function(G){var H,P,K,J,F,M,D,Q,E,O;function I(){var R=o.Deferred();if((H||{}).length){M.stop(true)._animate({opacity:"0"},A.fadeSpeed/4);D.stop(true)._animate({opacity:"0"},A.fadeSpeed/2);h.removeClass("zoom");o.when(H.stop(true)._animate({height:"0"},A.fadeSpeed)).then(function(){H.remove();H=null;R.resolve()})}else{R.resolve()}return R.promise()}function N(){var S,T,R;F=H.find("figure");M=H.find("figcaption");S=o('<img class="mosaiqy-zoom-image" />').attr({src:P.find("a").attr("href")});S.appendTo(F);if(S.get(0).height===0){S.hide()}R=(!!S.get(0).complete)?S.height():200;H._animate({height:R+"px"},A.fadeSpeed);T=P.find("img").prop("longDesc");if(!!T){S.wrap(o("<a />").attr({href:T,target:"new"}))}D=o('<a class="mosaiqy-zoom-close">Close</a>').attr({href:"#"}).bind("click.mosaiqy",function(U){o.when(I()).then(function(){B.removeClass("zoom");J=false;v()});U.preventDefault()}).appendTo(F);o.when(S.mosaiqyImagesLoad(A.loadTimeout,function(U){setTimeout(function(){var V=(!!S.get(0).height)?A.fadeSpeed:0;U.fadeIn(V,function(){D._animate({opacity:"1"},A.fadeSpeed/2);M.html(P.find("figcaption").html())._animate({opacity:"1"},A.fadeSpeed)})},A.fadeSpeed/1.2)})).done(function(){if(R<S.height()){H._animate({height:S.height()+"px"},A.fadeSpeed)}}).fail(function(){D.trigger("click.mosaiqy")})}function L(R){J=true;o.when(R()).done(function(){var S;B.addClass("zoom");P.addClass("zoom");h=B.find("li:not(.mosaiqy-zoom)");E=P.offset().top;Q=(document.body.scrollTop!==0)?document.body.scrollTop:document.documentElement.scrollTop;if(A.scrollZoom){O=Math.abs(Q-E);S=(O>0)?((O*1.5)+400):0}else{E=Q;S=0}H='<li class="mosaiqy-zoom"><figure><figcaption></figcaption></figure></li>';H=(typeof window.innerShiv==="function")?o(innerShiv(H,false)):o(H);if(K<h.length){H.insertBefore(h.eq(K))}else{H.appendTo(i)}o.when(y.stop()._animate({scrollTop:E},S)).done(function(){J=false;N()})})}G.live("click.mosaiqy",function(R){if(!r&&!J){p();P=o(this);K=A.cols*(Math.ceil((h.index(P)+1)/A.cols));if(!P.hasClass("zoom")){L(I)}}R.preventDefault()})},n=function(E){var D;while(E--){D=g(A.indexData);D.appendTo(o("<li />").appendTo(i));A.indexData=A.indexData+1}};return{init:function(E,D){var F=0;A=o.extend(A,D);if(!((A.data||[]).length)){throw new Error("Data object is empty")}if(!!A.template&&o("#"+A.template).is("script")){A.template=o("#"+A.template).text()||o("#"+A.template).html()}else{throw new Error("User template is not defined")}B=E;i=E.find("ul");h=E.find("li:not(.mosaiqy-zoom)");F=(A.cols*A.rows)-h.length;if(F){if(A.data.length>=F){A.indexData=h.length;n(F);h=E.find("li:not(.mosaiqy-zoom)")}else{throw new Error("Mosaiqy can't find missing images on JSON data.")}}if(A.avoidDuplicates){h.each(function(G){var H=o(this);if(typeof H.data("mosaiqy-index")==="undefined"){o(this).data("mosaiqy-index",G)}})}k=E.find("img");x();f();B.delegate("ul","mouseenter.mosaiqy",function(){p()}).delegate("ul","mouseleave.mosaiqy",function(){if(!B.hasClass("zoom")){v()}});o.when(k.mosaiqyImagesLoad(A.loadTimeout,function(G){G.animate({opacity:"1"},A.fadeSpeed)})).done(function(){B.removeClass("loading");t(h);j=setTimeout(function(){m()},A.animationDelay)}).fail(function(){return false});return this}}},e=d.sub();e.fn.mosaiqyImagesLoad=function(l,j){var f=d.Deferred(),i=this.length,h=[],g=[],k=l||8419.78;if(i){this.each(function(){var m=this;d.when((function n(){var o=d.Deferred(),p=setTimeout(function(){d(m).trigger("error.mosaiqy")},k);d(m).one("load.mosaiqy",function(){clearInterval(p);o.resolve()}).bind("error.mosaiqy",function(){clearInterval(p);o.reject()}).attr("src",m.src);if(m.complete){setTimeout(function(){d(m).trigger("load.mosaiqy")},10)}return o.promise()}())).done(function(){h.push(m.src);if(j){j(d(m))}}).fail(function(){g.push(m.src)}).always(function(){i=i-1;if(i===0){if(g.length){f.reject()}else{f.resolve()}}})})}return f.promise()};e.fn.extend({_animate:d.fn.animate,animate:function(g,h,j,i){var f=(h&&typeof h==="object")?d.extend({},h):{duration:h,complete:i||!i&&j||d.isFunction(h)&&h,easing:i&&j||j&&!d.isFunction(j)&&j};return d(this).each(function(){var n=e(this),o=n.position(),l={},k;if(a.isEnabled){if(typeof g==="object"){for(var m in g){if(m==="left"||m==="top"){k=g[m].match(/^(?:\+|\-)=(\-?\d+)/);if(k&&k.length){l[m]=o[m]+parseInt(k[1],10)}}}}n.bind(a.transitionEnd,function(){if(d.isFunction(f.complete)){f.complete()}}).css(l).css(a.duration,(h/1000)+"s")}else{n._animate(g,f)}})}});d.fn.mosaiqy=function(f){if(this.length){return this.each(function(){var g=new c(e);g.init(e(this),f);d.data(this,"mosaiqy",g)})}}}(jQuery));
