/* Copyright (C) YOOtheme GmbH, YOOtheme Proprietary Use License (http://www.yootheme.com/license) */

(function(b,a){var f=function(a,b,f){var d;return function(){var g=this,h=arguments;d?clearTimeout(d):f&&a.apply(g,h);d=setTimeout(function(){f||a.apply(g,h);d=null},b||150)}};jQuery.fn[a]=function(b){return b?this.bind("resize",f(b)):this.trigger(a)}})(jQuery,"smartresize");
(function(b){b.Gal=function(a,f){this.element=b(f);this._init(a)};b.Gal.settings={selector:".item",width:225,gutter:20,animate:!1,animationOptions:{speed:200,duration:300,effect:"fadeInOnAppear",queue:!0,complete:function(){}}};b.Gal.prototype={_init:function(a){var f=this;this.name=this._setName(5);this.gridArr=[];this.gridArrAppend=[];this.gridArrPrepend=[];this.setGrid=this.setArr=!1;this.setOptions;this.prependCount=this.itemCount=this.cols=0;this.isPrepending=!1;this.appendCount=0;this.ifCallback=
this.resetCount=!0;this.box=this.element;this.options=b.extend(!0,{},b.Gal.settings,a);this.gridArr=b.makeArray(this.box.find(this.options.selector));this.isResizing=!1;this.w=0;this.boxArr=[];this._setCols();this._renderGrid("append");b(this.box).addClass("gridalicious");var c=0;b(window).smartresize(function(){var a=b(window).width();b(window).width()!=c&&(f.resize(),c=a)})},_setName:function(a,b){b=b?b:"";return a?this._setName(--a,"0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz".charAt(Math.floor(60*
Math.random()))+b):b},_setCols:function(){this.cols=Math.floor(this.box.width()/this.options.width);diff=(this.box.width()-this.cols*this.options.width-this.options.gutter)/this.cols;this.w=w=100*((this.options.width+diff)/this.box.width());this.cols||(this.cols=1);for(var a=0;a<this.cols;a++)this.box.append(b("<div></div>").addClass("galcolumn").attr("id","item"+a+this.name).css({width:w+"%",paddingLeft:this.options.gutter,paddingBottom:this.options.gutter,"float":"left","-webkit-box-sizing":"border-box",
"-moz-box-sizing":"border-box","-o-box-sizing":"border-box","box-sizing":"border-box"}));this.box.find(b("#clear"+this.name)).remove();this.box.append(b("<div></div>").css({clear:"both",height:"0",width:"0",display:"block"}).attr("id","clear"+this.name))},_renderGrid:function(a,f,c){var i=[],e=[],d=0,g=this.appendCount,h=this.options.gutter,j=this.cols,k=this.name;b(".galcolumn").width();if(f){if(e=f,"append"==a&&(g+=c,d=this.appendCount),"prepend"==a&&(this.isPrepending=!0,d=Math.round(c%j),0>=d&&
(d=j)),"renderAfterPrepend"==a)g+=c,d=c}else e=this.gridArr,g=b(this.gridArr).size();b.each(e,function(c,f){var e=b(f),l="100%";e.hasClass("not-responsive")&&(l="auto");e.css({marginBottom:h,zoom:"1",opacity:"0"}).find("img, object, embed, iframe").css({width:l,height:"auto",display:"block","margin-left":"auto","margin-right":"auto"});"prepend"==a?(d--,b("#item"+d+k).prepend(e),i.push(e),0==d&&(d=j)):(b("#item"+d+k).append(e),i.push(e),d++,d>=j&&(d=0),g>=j&&(g-=j))});this.appendCount=g;this.itemCount=
d;"append"==a||"prepend"==a?("prepend"==a&&this._updateAfterPrepend(this.gridArr,e),this._renderItem(i),this.isPrepending=!1):this._renderItem(this.gridArr)},_collectItems:function(){var a=[];b(this.box).find(this.options.selector).each(function(){a.push(b(this))});return a},_renderItem:function(a){var f=this.options.animationOptions.speed,c=this.options.animationOptions.effect,i=this.options.animationOptions.duration,e=this.options.animationOptions.queue,d=this.options.animationOptions.complete,
g=0,h=0;!0===this.options.animate&&!this.isResizing?(!0===e&&"fadeInOnAppear"==c?(this.isPrepending&&a.reverse(),b.each(a,function(c,e){setTimeout(function(){b(e).animate({opacity:"1.0"},i);h++;h==a.length&&d.call(void 0,a)},g*f);g++})):!1===e&&"fadeInOnAppear"==c&&(this.isPrepending&&a.reverse(),b.each(a,function(e,c){b(c).animate({opacity:"1.0"},i);h++;h==a.length&&this.ifCallback&&d.call(void 0,a)})),!0===e&&!c&&b.each(a,function(c,e){b(e).css({opacity:"1"});h++;h==a.length&&this.ifCallback&&d.call(void 0,
a)})):(b.each(a,function(a,e){b(e).css({opacity:"1"})}),this.ifCallback&&d.call(a))},_updateAfterPrepend:function(a,f){var c=this.gridArr;b.each(f,function(a,b){c.unshift(b)});this.gridArr=c},resize:function(){this.box.find(".galcolumn").remove();this._setCols();this.ifCallback=!1;this.isResizing=!0;this._renderGrid("append");this.ifCallback=!0;this.isResizing=!1},append:function(a){var f=this.gridArr,c=this.gridArrPrepend;b.each(a,function(a,b){f.push(b);c.push(b)});this._renderGrid("append",a,b(a).size())},
prepend:function(a){this.ifCallback=!1;this._renderGrid("prepend",a,b(a).size());this.ifCallback=!0}};b.fn.gridalicious=function(a,f){"string"===typeof a?this.each(function(){var c=b.data(this,"gridalicious");c[a].apply(c,[f])}):this.each(function(){b.data(this,"gridalicious",new b.Gal(a,this))});return this}})(jQuery);
