(function(a){a.fn.freetile=function(b){if(typeof FreeTile[b]==="function"){return FreeTile[b].apply(this,Array.prototype.slice.call(arguments,1))}else{if(typeof b==="object"||!b){return FreeTile.init.apply(this,arguments)}else{a.error("Method "+b+" does not exist on jQuery.FreeTile")}}return this};FreeTile={init:function(d){var c=this;var b=FreeTile.setupOptions(c,d);if(!b.tiled){FreeTile.setupContainerBindings(c,b)}if(b.tiled&&!a.isEmptyObject(b.contentToAppend)){c.append(b.contentToAppend);b.contentToAppend.imagesLoaded(function(){FreeTile.positionAll(c,b)})}else{c.imagesLoaded(function(){FreeTile.positionAll(c,b)})}return c},append:function(d){var c=this;var b=FreeTile.setupOptions(c,d);if(b.tiled&&!a.isEmptyObject(b.contentToAppend)){c.append(b.contentToAppend);b.contentToAppend.imagesLoaded(function(){FreeTile.positionAll(c,b)})}return c},layout:function(c){var b=FreeTile.setupOptions(this,c);FreeTile.positionAll(this,b);return this},setupOptions:function(d,b){var c=d.data("freetiledata");var e=a.extend(true,{},this.defaults,c,this.reset,b);d.data("freetiledata",e);e.tiled=(c!=undefined);e._animate=e.animate&&e.tiled&&a.isEmptyObject(e.contentToAppend);this.reset.callbk=e.persistentCallback&&e.callbk?e.callbk:function(){};return e},setupContainerBindings:function(c,b){if(b.containerResize){var e=a(window),d=e.width(),f=e.height();e.resize(function(){clearTimeout(c.data("FreeTileTimeout"));c.data("FreeTileTimeout",setTimeout(function(){var h=e.width(),g=e.height();if(h!=d||g!=f){d=h,f=g;c.freetile("layout")}},400))})}if(b.customEvents){c.bind(b.customEvents,function(){clearTimeout(c.data("FreeTileTimeout"));c.data("FreeTileTimeout",setTimeout(function(){c.freetile("layout")},400))})}return c},positionOne:function(g,m,w,r){var t=m.outerWidth(true),l=m.outerHeight(true),u=g.width(),c=r._currentLeft,s=r._currentTop,b=s.length,k=0,e=b,h=Math.max.apply(Math,s);for(var p=0;(p<b)&&(u>=t+c[p]);p++){var d=s[p];var n=p+1;for(;n<b;n++){if(c[n]-c[p]>=t){break}d=Math.max(s[n],d)}if(d<h){k=p;h=d}if(p==k){e=n}}var f={left:c[k]+r._xPad,top:h+r._yPad},v=m.position();if(v.top+"px"!=f.top||v.left+"px"!=f.left){if(w._animate&&!m.hasClass("noanim")&&(v.top+l>r.viewportY&&v.top<r.viewportYH||f.top+l>r.viewportY&&f.top<r.viewportYH)){setTimeout(function(){f.left+="px";f.top+="px";m.animate(f,a.extend(true,{},w.animationOptions))},r.currentDelay);r.currentDelay+=w.elementDelay}else{f.left+="px";f.top+="px";m.css(f);if(--(r._i)<=0){w.callbk(r)}}}else{if(--(r._i)<=0){w.callbk(r)}}var q=1,o=s[e-1];if(e<b){q=c[e]-c[k]-t}r._currentTop[k]=h+l;r._currentLeft.splice(k+1,e-k-1);r._currentTop.splice(k+1,e-k-1);if(q>0&&c[k]+t<u){r._currentLeft.splice(k+1,0,c[k]+t);r._currentTop.splice(k+1,0,o)}},positionAll:function(e,c){var f={};var h=!a.isEmptyObject(c.contentToAppend)?c.contentToAppend:c.elementClass===undefined?e.children():e.children(c.elementClass);if(!h.length){return(false)}h.css({position:"absolute"});f._l=h.length;var b=e.css("display");var g=e.css("visibility");e.css({display:"block",width:"",visibility:"hidden"});_savedLeft=e.data("FreeTileLeft");_savedTop=e.data("FreeTileTop");f._currentLeft=!a.isEmptyObject(c.contentToAppend)&&_savedLeft?_savedLeft:[];f._currentTop=!a.isEmptyObject(c.contentToAppend)&&_savedTop?_savedTop:[];if(!f._currentLeft.length){f._currentLeft.push(0)}if(!f._currentTop.length){f._currentTop.push(0)}f._xPad=parseInt(e.css("padding-left"),10);f._yPad=parseInt(e.css("padding-top"),10);f.viewportY=a(window).scrollTop();f.viewportYH=f.viewportY+a(window).height();f._left=0;f._i=h.length;f.currentDelay=0;c.animationOptions.complete=function(){if(--(f._i)<=0){c.callbk(f)}};h.each(function(){FreeTile.positionOne(e,a(this),c,f)});var d={display:b,visibility:g};if(e.css("position")=="static"){d.position="relative"}if(c.forceWidth){d.width=c.containerWidthStep?c.containerWidthStep*(parseInt(e.width()/c.containerWidthStep)):f._columns*f._colWidth}e.data("FreeTileLeft",f._currentLeft);e.data("FreeTileTop",f._currentTop);e.css(d);d={height:Math.max.apply(Math,f._currentTop)};if(c._animate){e.animate(d,a.extend(true,{},c.animationOptions))}else{e.css(d)}h.addClass("tiled");return e},defaults:{elementClass:undefined,animate:false,elementDelay:0,containerResize:true,customEvents:undefined,persistentCallback:false,forceWidth:false,containerWidthStep:undefined,cornerFactor:0.03},reset:{animationOptions:{complete:function(){}},callbk:function(){},contentToAppend:{}}};a.fn.imagesLoaded=function(j){var i=this,d=i.find("img:not(.load-complete)").add(i.filter("img:not(.load-complete)")),g=d.length,b=[],f="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";function c(){j.call(i,d)}function h(k){if(--g<=0){g=d.length;d.unbind("load error",h).bind("load error",e).each(function(){this.src=b.shift()})}}function e(k){if(--g<=0&&k.target.src!==f){setTimeout(c);d.unbind("load error",e).addClass("load-complete")}}if(!g){c()}d.bind("load error",h).each(function(){b.push(this.src);this.src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw=="});return i};a.fn.minOuterWidth=function(b){var c=a(this).outerWidth(b);a(this).each(function(){var d=a(this).outerWidth(b);c=d<c?d:c});return c}})(jQuery);
