(function(d){"function"===typeof define&&define.amd?define(["jquery","modernizr"],d):window.Shuffle=d(window.jQuery,window.Modernizr)})(function(d,m,v){function l(a,c,b){for(var e=0,d=a.length;e<d&&c.call(b,a[e],e,a)!=={};e++);}function q(a,c,b){return setTimeout(d.proxy(a,c),b)}function r(a){return d.isNumeric(a)?a:0}function z(a){var c,b,e=a.length;if(!e)return a;for(;--e;)b=Math.floor(Math.random()*(e+1)),c=a[b],a[b]=a[e],a[e]=c;return a}if("object"!==typeof m)throw Error("Shuffle.js requires Modernizr.\nhttp://vestride.github.io/Shuffle/#dependencies");
var t=m.prefixed("transition"),w=m.prefixed("transitionDelay"),u=m.prefixed("transitionDuration"),s={WebkitTransition:"webkitTransitionEnd",transition:"transitionend"}[t],x=m.prefixed("transform"),A=function(a){return a?a.replace(/([A-Z])/g,function(a,b){return"-"+b.toLowerCase()}).replace(/^ms-/,"-ms-"):""}(x),B=m.csstransforms&&m.csstransitions,C=m.csstransforms3d,n=function(a,c){this.x=r(a);this.y=r(c)};n.equals=function(a,c){return a.x===c.x&&a.y===c.y};var D=0,y=d(window),b=function(a,c){c=c||
{};d.extend(this,b.options,c,b.settings);this.$el=d(a);this.element=a;this.unique="shuffle_"+D++;this._fire(b.EventType.LOADING);this._init();q(function(){this.initialized=!0;this._fire(b.EventType.DONE)},this,16)};b.EventType={LOADING:"loading",DONE:"done",LAYOUT:"layout",REMOVED:"removed"};b.ClassName={BASE:"shuffle",SHUFFLE_ITEM:"shuffle-item",FILTERED:"filtered",CONCEALED:"concealed"};b.options={group:"all",speed:250,easing:"ease-out",itemSelector:"",sizer:null,gutterWidth:0,columnWidth:0,delimeter:null,
buffer:0,initialSort:null,throttle:function(a,c,b){var e,h,k,g=null,p=0;b=b||{};var m=function(){p=!1===b.leading?0:d.now();g=null;k=a.apply(e,h);e=h=null};return function(){var l=d.now();p||!1!==b.leading||(p=l);var n=c-(l-p);e=this;h=arguments;0>=n||n>c?(clearTimeout(g),g=null,p=l,k=a.apply(e,h),e=h=null):g||!1===b.trailing||(g=setTimeout(m,n));return k}},throttleTime:300,sequentialFadeDelay:150,supported:B};b.settings={useSizer:!1,itemCss:{position:"absolute",top:0,left:0,visibility:"visible"},
revealAppendedDelay:300,lastSort:{},lastFilter:"all",enabled:!0,destroyed:!1,initialized:!1,_animations:[],styleQueue:[]};b.Point=n;b._getItemTransformString=function(a,c){return C?"translate3d("+a.x+"px, "+a.y+"px, 0) scale3d("+c+", "+c+", 1)":"translate("+a.x+"px, "+a.y+"px) scale("+c+")"};b._getNumberStyle=function(a,c){return b._getFloat(d(a).css(c))};b._getInt=function(a){return r(parseInt(a,10))};b._getFloat=function(a){return r(parseFloat(a))};b._getOuterWidth=function(a,c){var f=a.offsetWidth;
if(c)var e=b._getNumberStyle(a,"marginLeft"),d=b._getNumberStyle(a,"marginRight"),f=f+(e+d);return f};b._getOuterHeight=function(a,c){var f=a.offsetHeight;if(c)var e=b._getNumberStyle(a,"marginTop"),d=b._getNumberStyle(a,"marginBottom"),f=f+(e+d);return f};b._skipTransition=function(a,c,b){var e=a.style[u];a.style[u]="0ms";c.call(b);a.style[u]=e};b.prototype._init=function(){this.$items=this._getItems();if(this.sizer=this._getElementOption(this.sizer))this.useSizer=!0;this.$el.addClass(b.ClassName.BASE);
this._initItems();y.on("resize.shuffle."+this.unique,this._getResizeFunction());var a=this.$el.css(["position","overflow"]),c=b._getOuterWidth(this.element);this._validateStyles(a);this._setColumns(c);this.shuffle(this.group,this.initialSort);this.supported&&q(function(){this._setTransitions();this.element.style[t]="height "+this.speed+"ms "+this.easing},this)};b.prototype._getResizeFunction=function(){var a=d.proxy(this._onResize,this);return this.throttle?this.throttle(a,this.throttleTime):a};b.prototype._getElementOption=
function(a){return"string"===typeof a?this.$el.find(a)[0]||null:a&&a.nodeType&&1===a.nodeType?a:a&&a.jquery?a[0]:null};b.prototype._validateStyles=function(a){"static"===a.position&&(this.element.style.position="relative");"hidden"!==a.overflow&&(this.element.style.overflow="hidden")};b.prototype._filter=function(a,c){a=a||this.lastFilter;c=c||this.$items;var b=this._getFilteredSets(a,c);this._toggleFilterClasses(b.filtered,b.concealed);this.lastFilter=a;"string"===typeof a&&(this.group=a);return b.filtered};
b.prototype._getFilteredSets=function(a,c){var b=d(),e=d();"all"===a?b=c:l(c,function(c){c=d(c);this._doesPassFilter(a,c)?b=b.add(c):e=e.add(c)},this);return{filtered:b,concealed:e}};b.prototype._doesPassFilter=function(a,c){if(d.isFunction(a))return a.call(c[0],c,this);var b=c.data("groups"),b=this.delimeter&&!d.isArray(b)?b.split(this.delimeter):b;return-1<d.inArray(a,b)};b.prototype._toggleFilterClasses=function(a,c){a.removeClass(b.ClassName.CONCEALED).addClass(b.ClassName.FILTERED);c.removeClass(b.ClassName.FILTERED).addClass(b.ClassName.CONCEALED)};
b.prototype._initItems=function(a){a=a||this.$items;a.addClass([b.ClassName.SHUFFLE_ITEM,b.ClassName.FILTERED].join(" "));a.css(this.itemCss).data("point",new n).data("scale",1)};b.prototype._updateItemCount=function(){this.visibleItems=this._getFilteredItems().length};b.prototype._setTransition=function(a){a.style[t]=A+" "+this.speed+"ms "+this.easing+", opacity "+this.speed+"ms "+this.easing};b.prototype._setTransitions=function(a){a=a||this.$items;l(a,function(a){this._setTransition(a)},this)};
b.prototype._setSequentialDelay=function(a){this.supported&&l(a,function(a,b){a.style[w]="0ms,"+(b+1)*this.sequentialFadeDelay+"ms"},this)};b.prototype._getItems=function(){return this.$el.children(this.itemSelector)};b.prototype._getFilteredItems=function(){return this.$items.filter("."+b.ClassName.FILTERED)};b.prototype._getConcealedItems=function(){return this.$items.filter("."+b.ClassName.CONCEALED)};b.prototype._getColumnSize=function(a,c){var f;f=d.isFunction(this.columnWidth)?this.columnWidth(a):
this.useSizer?b._getOuterWidth(this.sizer):this.columnWidth?this.columnWidth:0<this.$items.length?b._getOuterWidth(this.$items[0],!0):a;0===f&&(f=a);return f+c};b.prototype._getGutterSize=function(a){return d.isFunction(this.gutterWidth)?this.gutterWidth(a):this.useSizer?b._getNumberStyle(this.sizer,"marginLeft"):this.gutterWidth};b.prototype._setColumns=function(a){a=a||b._getOuterWidth(this.element);var c=this._getGutterSize(a),f=this._getColumnSize(a,c),c=(a+c)/f;.3>Math.abs(Math.round(c)-c)&&
(c=Math.round(c));this.cols=Math.max(Math.floor(c),1);this.containerWidth=a;this.colWidth=f};b.prototype._setContainerSize=function(){this.$el.css("height",this._getContainerSize())};b.prototype._getContainerSize=function(){return Math.max.apply(Math,this.positions)};b.prototype._fire=function(a,c){this.$el.trigger(a+".shuffle",c&&c.length?c:[this])};b.prototype._resetCols=function(){var a=this.cols;for(this.positions=[];a--;)this.positions.push(0)};b.prototype._layout=function(a,c){l(a,function(a){this._layoutItem(a,
!!c)},this);this._processStyleQueue();this._setContainerSize()};b.prototype._layoutItem=function(a,c){var f=d(a),e=f.data(),h=e.point,k=e.scale,g={width:b._getOuterWidth(a,!0),height:b._getOuterHeight(a,!0)},g=this._getItemPosition(g);n.equals(h,g)&&1===k||(e.point=g,e.scale=1,this.styleQueue.push({$item:f,point:g,scale:1,opacity:c?0:1,skipTransition:c,callfront:function(){c||f.css("visibility","visible")},callback:function(){c&&f.css("visibility","hidden")}}))};b.prototype._getItemPosition=function(a){var c=
this._getColumnSpan(a.width,this.colWidth,this.cols),b=this._getColumnSet(c,this.cols),c=this._getShortColumn(b,this.buffer),e=new n(Math.round(this.colWidth*c),Math.round(b[c]));a=b[c]+a.height;for(var b=this.cols+1-b.length,d=0;d<b;d++)this.positions[c+d]=a;return e};b.prototype._getColumnSpan=function(a,b,f){a/=b;.3>Math.abs(Math.round(a)-a)&&(a=Math.round(a));return Math.min(Math.ceil(a),f)};b.prototype._getColumnSet=function(a,b){if(1===a)return this.positions;for(var f=b+1-a,e=[],d=0;d<f;d++){var k=
d,g;g=this.positions.slice(d,d+a);g=Math.max.apply(Math,g);e[k]=g}return e};b.prototype._getShortColumn=function(a,b){for(var d=Math.min.apply(Math,a),e=0,h=a.length;e<h;e++)if(a[e]>=d-b&&a[e]<=d+b)return e;return 0};b.prototype._shrink=function(a){a=a||this._getConcealedItems();l(a,function(a){var b=d(a);a=b.data();.001!==a.scale&&(a.scale=.001,this.styleQueue.push({$item:b,point:a.point,scale:.001,opacity:0,callback:function(){b.css("visibility","hidden")}}))},this)};b.prototype._onResize=function(){!this.enabled||
this.destroyed||this.isTransitioning||b._getOuterWidth(this.element)!==this.containerWidth&&this.update()};b.prototype._getStylesForTransition=function(a){var c={opacity:a.opacity};this.supported?c[x]=b._getItemTransformString(a.point,a.scale):(c.left=a.point.x,c.top=a.point.y);return c};b.prototype._transition=function(a){var b=this._getStylesForTransition(a);this._startItemAnimation(a.$item,b,a.callfront||d.noop,a.callback||d.noop)};b.prototype._startItemAnimation=function(a,b,f,e){function h(a){a.target===
a.currentTarget&&(d(a.target).off(s,h),e())}f();this.initialized?this.supported?(a.css(b),a.on(s,h)):(a=a.stop(!0).animate(b,this.speed,"swing",e),this._animations.push(a.promise())):(a.css(b),e())};b.prototype._processStyleQueue=function(a){var b=d();l(this.styleQueue,function(a){a.skipTransition?this._styleImmediately(a):(b=b.add(a.$item),this._transition(a))},this);0<b.length&&this.initialized?(this.isTransitioning=!0,this.supported?this._whenCollectionDone(b,s,this._movementFinished):this._whenAnimationsDone(this._movementFinished)):
a||q(this._layoutEnd,this);this.styleQueue.length=0};b.prototype._styleImmediately=function(a){b._skipTransition(a.$item[0],function(){a.$item.css(this._getStylesForTransition(a))},this)};b.prototype._movementFinished=function(){this.isTransitioning=!1;this._layoutEnd()};b.prototype._layoutEnd=function(){this._fire(b.EventType.LAYOUT)};b.prototype._addItems=function(a,b,d){this._initItems(a);this._setTransitions(a);this.$items=this._getItems();this._shrink(a);l(this.styleQueue,function(a){a.skipTransition=
!0});this._processStyleQueue(!0);b?this._addItemsToEnd(a,d):this.shuffle(this.lastFilter)};b.prototype._addItemsToEnd=function(a,b){var d=this._filter(null,a).get();this._updateItemCount();this._layout(d,!0);b&&this.supported&&this._setSequentialDelay(d);this._revealAppended(d)};b.prototype._revealAppended=function(a){q(function(){l(a,function(a){a=d(a);this._transition({$item:a,opacity:1,point:a.data("point"),scale:1})},this);this._whenCollectionDone(d(a),s,function(){d(a).css(w,"0ms");this._movementFinished()})},
this,this.revealAppendedDelay)};b.prototype._whenCollectionDone=function(a,b,f){function e(a){a.target===a.currentTarget&&(d(a.target).off(b,e),h++,h===k&&f.call(g))}var h=0,k=a.length,g=this;a.on(b,e)};b.prototype._whenAnimationsDone=function(a){d.when.apply(null,this._animations).always(d.proxy(function(){this._animations.length=0;a.call(this)},this))};b.prototype.shuffle=function(a,b){this.enabled&&!this.isTransitioning&&(a||(a="all"),this._filter(a),this._updateItemCount(),this._shrink(),this.sort(b))};
b.prototype.sort=function(a){if(this.enabled&&!this.isTransitioning){this._resetCols();a=a||this.lastSort;var b=this._getFilteredItems().sorted(a);this._layout(b);this.lastSort=a}};b.prototype.update=function(a){this.enabled&&!this.isTransitioning&&(a||this._setColumns(),this.sort())};b.prototype.layout=function(){this.update(!0)};b.prototype.appended=function(a,b,d){this._addItems(a,!0===b,!1!==d)};b.prototype.disable=function(){this.enabled=!1};b.prototype.enable=function(a){this.enabled=!0;!1!==
a&&this.update()};b.prototype.remove=function(a){function c(){a.remove();this.$items=this._getItems();this._updateItemCount();this._fire(b.EventType.REMOVED,[a,this]);a=null}a.length&&a.jquery&&(this._toggleFilterClasses(d(),a),this._shrink(a),this.sort(),this.$el.one(b.EventType.LAYOUT+".shuffle",d.proxy(c,this)))};b.prototype.destroy=function(){y.off("."+this.unique);this.$el.removeClass("shuffle").removeAttr("style").removeData("shuffle");this.$items.removeAttr("style").removeData("point").removeData("scale").removeClass([b.ClassName.CONCEALED,
b.ClassName.FILTERED,b.ClassName.SHUFFLE_ITEM].join(" "));this.element=this.sizer=this.$el=this.$items=null;this.destroyed=!0};d.fn.shuffle=function(a){var c=Array.prototype.slice.call(arguments,1);return this.each(function(){var f=d(this),e=f.data("shuffle");e?"string"===typeof a&&e[a]&&e[a].apply(e,c):(e=new b(this,a),f.data("shuffle",e))})};d.fn.sorted=function(a){var b=d.extend({},d.fn.sorted.defaults,a);a=this.get();var f=!1;if(!a.length)return[];if(b.randomize)return z(a);d.isFunction(b.by)&&
a.sort(function(a,h){if(f)return 0;var k=b.by(d(a)),g=b.by(d(h));return k===v&&g===v?(f=!0,0):k<g||"sortFirst"===k||"sortLast"===g?-1:k>g||"sortLast"===k||"sortFirst"===g?1:0});if(f)return this.get();b.reverse&&a.reverse();return a};d.fn.sorted.defaults={reverse:!1,by:null,randomize:!1};return b});