var qualifyURL=function(n){var t=document.createElement("img");return t.src=n,n=t.src,t.src=null,n};(function(n,t,r){function e(t,i){this.element=t;this.$element=n(t);this.settings=n.extend({},f,i);this.settings.gridSize||(this.settings.gridSize=5);this._defaults=f;this._name=u;this.tiles=[];this._loadedImages=0;this.edges=[];this.currentWidth=0;this.currentImageSizeFactor=1;this.ajaxComplete=!1;this.isLoading=!1;this.currentPage=1;this.init()}n.fn.visible=function(i){if(!n(this).offset())return!0;var u=n(this),f=n(t),e=f.scrollTop(),s=e+f.height(),r=u.offset().top,o=r+u.height(),h=i===!0?o:r,c=i===!0?r:o;return c<=s&&h>=e};var u="finalTilesGallery",f={margin:10,minTileWidth:200,ignoreImageAttributes:!0,imageSizeFactor:[[4e3,.9],[1024,.8],[800,.7],[600,.6],[480,.5],[320,.3]],gridSize:50,allowEnlargement:!0,autoLoadURL:null,autoLoadOffset:50,onComplete:function(){},onUpdate:function(){},debug:!1};n.extend(e.prototype,{print:function(n){this.settings.debug&&console.log(n)},setCurrentImageSizeFactor:function(){var r,i;for(this.currentImageSizeFactor=1,r=n(t).width(),i=0;i<this.settings.imageSizeFactor.length;i++)this.settings.imageSizeFactor[i][0]>=r&&(this.currentImageSizeFactor=this.settings.imageSizeFactor[i][1]);this.currentImageSizeFactor||(this.currentImageSizeFactor=1);this.print("current image size factor: "+this.currentImageSizeFactor+" ("+r+")")},init:function(){var i=this,u;this.$element.find(".ftg-items").css({position:"relative"});this.tiles=this.$element.find(".tile").not(".ftg-hidden");this.tiles.css({transition:"all .3s"});this.currentWidth=this.$element.width();this.print("this.currentWidth: "+this.currentWidth);u=0;this.setCurrentImageSizeFactor();n(t).resize(function(){u=setTimeout(function(){i.currentWidth!=i.$element.width()&&(clearTimeout(u),i.print("this.currentWidth",this.currentWidth),i.currentWidth=i.$element.width(),i.setCurrentImageSizeFactor(),i.refresh())},500)});i.settings.autoLoadURL&&n(t).scroll(function(){i.ajaxComplete||i.isLoading||n(t).scrollTop()>=n(r).height()-n(t).height()-i.settings.autoLoadOffset&&(i.isLoading=!0,n.get(i.settings.autoLoadURL,{page:++i.currentPage},function(t){n.trim(t).length==0?i.ajaxComplete=!0:(i.$element.find(".ftg-items").append(t),i.tiles=i.$element.find(".tile"),i.loadImage())}))});this.setupFilters();this.edges.push({left:0,top:0,width:this.currentWidth,index:0});this.loadImage()},setupFilters:function(){var t=this;t.$element.find(".ftg-filters a").click(function(i){i.preventDefault();t.$element.find(".ftg-filters a").removeClass("selected");n(this).addClass("selected");var r=n(this).attr("href").replace("#ftg-set-","");r=="ftgall"?t.$element.find(".tile").removeClass("ftg-hidden"):t.$element.find(".tile").not(".ftg-set-"+r).addClass("ftg-hidden").end().filter(".ftg-set-"+r).removeClass("ftg-hidden");t.refresh()})},printEdges:function(){for(this.$element.find(".edge").remove(),i=0;i<this.edges.length;i++){var t=n("<div class='edge' />");t.append("top: "+this.edges[i].top+"<br>");t.append("left: "+this.edges[i].left+"<br>");t.append("width: "+this.edges[i].width+"<br>");t.css({left:this.edges[i].left,top:this.edges[i].top,marginTop:-25,marginLeft:20});this.$element.append(t)}},printEdge:function(t){var i=n("<div class='edge enlarged-"+t.enlarged+"' />");i.append("<b>"+t.index+" "+t.case+"<\/b><br>");i.append("t: "+Math.round(t.top)+" l: "+t.left+"<br>");i.append("width: "+t.width+"<br>");i.append("idx: "+t.tileIndex+"<br>");i.css({left:t.left,top:t.top,marginTop:-25,marginLeft:20});this.$element.append(i)},refresh:function(){this.$element.find(".edge").remove();this.edges=[{left:0,top:0,width:this.currentWidth}];this.tiles.removeClass("ftg-loaded ftg-enlarged");this.tiles=this.$element.find(".tile").not(".ftg-hidden");this._loadedImages=0;this.loadImage()},loadImage:function(){function u(){if(t.add(t._loadedImages),++t._loadedImages<t.tiles.length)t.loadImage();else{var n=t.lowerEdgeTop();t.print("lower edge top: "+n);t.$element.find(".ftg-items").height(n);t.isLoading=!1;t.settings.onComplete()}}var t=this,r=this.tiles.eq(this._loadedImages),n,i;r.children("iframe").length&&r.children("iframe").addClass("ftg-item");n=r.find(".ftg-item");switch(n.get(0).tagName.toLowerCase()){case"img":i=new Image;i.onload=function(){var f=t.currentImageSizeFactor;r.data("ftg-ignore-size-factor")&&(f=1);n.attr("src",this.src);n.data("width",i.width*f);n.data("height",i.height*f);n.data("owidth",i.width);n.data("oheight",i.height);u()};i.onerror=function(){t.print("error loading image: "+i.src);u()};i.src=n.data("src");r.data("ftg-type","image");break;case"iframe":n.data("width",parseInt(n.attr("width")));n.data("height",parseInt(n.attr("height")));n.data("owidth",parseInt(n.attr("width")));n.data("oheight",parseInt(n.attr("height")));r.data("ftg-type","iframe");u();break;default:n.data("width",n.data("width"));n.data("height",n.data("height"));n.data("owidth",n.data("width"));n.data("oheight",n.data("height"));r.data("ftg-type","generic");u()}},higherEdge:function(){for(var t=1e5,i=0,n=0;n<this.edges.length;n++)this.edges[n].top<t&&(i=n,t=this.edges[n].top);return this.edges[i]},lowerEdgeTop:function(){for(var t=0,n=0;n<this.edges.length;n++)this.edges[n].top>t&&(t=this.edges[n].top);return t},alignEdge:function(n){for(var t=0;t<this.edges.length;t++)if(this.edges[t].left+this.edges[t].width+this.settings.margin==n.left&&(this.print("found edge on left",t),n.top==this.edges[t].top))return this.print("edges can be aligned [1]"),{side:"left",edge:this.edges[t]};for(t=0;t<this.edges.length;t++)if(this.edges[t].left-this.settings.margin==n.left+n.width&&(this.print("found edge on right",t),n.top==this.edges[t].top))return this.print("edges can be aligned [2]"),{side:"right",edge:this.edges[t]};return null},removeEdge:function(n){for(var i=[],t=0;t<this.edges.length;t++)this.edges[t]!=n&&i.push(this.edges[t]);this.edges=i},add:function(n){var u=this.tiles.eq(n),o=u.find(".ftg-item"),r=o.data("width"),i=o.data("height"),t=this.higherEdge(),f,s,l,c,h,e,a,v;if(this.print(t),t.tileIndex=n,this.print(n+" ["+u.data("ftg-type")+"] ("+r+"x"+i+")"),t.top>0&&(t.top+=this.settings.margin),u.css({left:t.left,top:t.top,position:"absolute"}),t.enlarged=!1,t.width<r+this.settings.margin)t.case="Te",this.print("Te",t.width),f=t.width,s=i/r*f,f+t.left-this.settings.margin==this.currentWidth&&(this.print("END"),f-=this.settings.margin,s=i/r*f),r=f,i=s;else if(t.width>r)if(this.print("tE"),t.width-r>=this.settings.minTileWidth)t.case="tE",this.print("tE1",t.width,t.left,this.currentWidth),l={left:t.left+r+this.settings.margin,top:t.top-(t.top>0?this.settings.margin:0),width:t.width-r-this.settings.margin,marginLeft:!0,"case":"NEW",index:t.index+1},this.edges.push(l);else{t.case="tE2";this.print("tE2");this.print("enlargement",t.width,t.left,this.currentWidth);var y=t.left+t.width==this.currentWidth?0:this.settings.margin,f=t.width,s=this.settings.allowEnlargement?i/r*f:i;this.settings.allowEnlargement?(u.addClass("ftg-enlarged"),t.enlarged=!0):u.find(".ftg-item").css({width:r,height:i});r=f;i=s}t.top+=i;c=t.top%this.settings.gridSize;t.top-=c;i-=c;t.left=t.left;t.width=r;h=!0;e=this.alignEdge(t,n);e&&(e.side=="left"?(this.removeEdge(t),e.edge.width+=r+this.settings.margin,i=i-(t.top-e.edge.top),t.top-=i,h=!1):(this.removeEdge(e.edge),t.width+=this.settings.margin+e.edge.width,h=!1),u.height(i));this.$element.find(".ftg-items").height()<t.top&&this.$element.find(".ftg-items").height(t.top);this.settings.debug&&h&&this.printEdge(t);u.data("ftg-type")=="iframe"&&u.find("iframe").height(i);this.print(r+"x"+i);this.print("----");u.css({width:r,height:i});a=r/o.data("width");v=o.data("height")*a-i;o.css({top:0-v/2});u.addClass("ftg-loaded")}});n.fn[u]=function(t){return this.each(function(){n.data(this,"plugin_"+u)||n.data(this,"plugin_"+u,new e(this,t))}),this};n(function(){n(".ftg-social a").click(function(i){var e,f,u;i.preventDefault();var o=n(this).data("social"),h=n(this).parents(".tile").first(),s=h.data("big");s||(s=h.find(".ftg-item").attr("src"));e=n.trim(h.find(".caption").text());e.length||(e=r.title);o=="facebook"&&(f="https://www.facebook.com/dialog/feed?app_id=1685564315011866&link="+encodeURIComponent(location.href)+"&display=popup&name="+encodeURIComponent(r.title)+"&caption=&description="+encodeURIComponent(e)+"&picture="+encodeURIComponent(qualifyURL(s))+"&ref=share&actions={%22name%22:%22View%20the%20gallery%22,%20%22link%22:%22"+encodeURIComponent(location.href)+"%22}&redirect_uri=http://koa.com",u=t.open(f,"ftgw","location=1,status=1,scrollbars=1,width=600,height=400"),u.moveTo(screen.width/2-300,screen.height/2-200));o=="twitter"&&(u=t.open("https://twitter.com/intent/tweet?url="+encodeURI(location.href.split("#")[0])+"&text="+encodeURI(e),"ftgw","location=1,status=1,scrollbars=1,width=600,height=400"),u.moveTo(screen.width/2-300,screen.height/2-200));o=="pinterest"&&(f="http://pinterest.com/pin/create/button/?url="+encodeURIComponent(location.href)+"&description="+encodeURI(e),f+="&media="+encodeURIComponent(qualifyURL(s)),u=t.open(f,"ftgw","location=1,status=1,scrollbars=1,width=600,height=400"),u.moveTo(screen.width/2-300,screen.height/2-200));o=="google-plus"&&(f="https://plus.google.com/share?url="+encodeURI(location.href),u=t.open(f,"ftgw","location=1,status=1,scrollbars=1,width=600,height=400"),u.moveTo(screen.width/2-300,screen.height/2-200))})})})(jQuery,window,document)