/* Waterfall( Fixed Width ) component for jQuery ( AMD Module ) | MIT Licenses | 2013-12-28 */
define(function(){var m={colWidth:252,container:window,create:null,reservedWidth:20,init:null,load:null,maxCols:null,maxHeight:800,minCols:2,spaceY:14,specialPosition:null,serialize:function(a){return a}},e={throttle:function(a){var b;return function(){var c=this,d=arguments;clearTimeout(b);b=setTimeout(function(){a.apply(c,d)},50)}},initHeights:function(a){var b=a.initHeight,c=a.cols,d=0,e,f;e=a.heights=[];for(f=a.margins=[];d<c;d++)e[d]=0,f[d]=0-b;a.specialPosition&&(a="left"===a.specialPosition?
0:c-1,e[a]=b,f[a]=0)},getColHeights:function(a){var b=a.prevHeights,c=a.heights,d=c.length,e=a.colElem,f=0,g;for(a.fixedHeight=!0;f<d;f++)g=b[f]+e.eq(f).outerHeight(),g!==c[f]&&(a.fixedHeight=!1,c[f]=g)},getLoadOffset:function(a){a.loadOffset=a.target.outerHeight()+a.targetTop-Math.round(1.5*a.containerHeight)},getChangeOffset:function(a,b){var c=b.outerHeight()+b.offset().top;b.data("changeOffset",c+a.containerHeight/2)},createGroup:function(a){return $('<div class="wf_group"/>').appendTo(a.target)},
createCol:function(a){for(var b=a.margins,c="",d=0;d<a.cols;d++)c+='<ul class="wf_col"'+(b[d]?'style="margin-top:'+b[d]+'px"':"")+"/>";return $(c).appendTo(a.mainFragment)},getExtreme:function(a,b){for(var c=a.length,d=a[0],e=!1,f=0,g=1,h;g<c;g++)if(h=a[g],e=b?h<d:h>d)d=h,f=g;return{index:f,item:d}},addCell:function(a,b,c){var d=b.elem||$(a.create(b)),k=e.getExtreme(a.heights,!0);b=Math.min(b.height,a.maxHeight);var f=k.index;void 0!==c&&a.wfCache[c].push({height:b,elem:d});a.heights[f]=k.item+b+
a.spaceY;a.colElem.eq(f).append(d)},addCol:function(a,b,c){var d=a.heights,k=a.margins,f,g;b.empty().append(a.mainFragment);e.getChangeOffset(a,b);if(!a.fixedHeight&&void 0!==c){f=a.wfCache[c];g=f.length;for(c=0;c<g;c++)f[c].height=f[c].elem.outerHeight();e.getColHeights(a)}f=e.getExtreme(d,!1).item;for(c=0;c<a.cols;c++)k[c]=d[c]-f;a.initOffset&&(b=b.outerHeight()+b.offset().top,b<a.initOffset?e.loadData(a):a.initOffset=0)},offlineSort:function(a,b,c){var d=a.heights,k=a.margins,f=e.getExtreme(d,
!1).item,g=a.groupElems.eq(b);b=a.fragments[b];var h=0;$(b).empty();b.appendChild(a.mainFragment);g.css("height",f-c+"px");for(e.getChangeOffset(a,g);h<a.cols;h++)k[h]=d[h]-f},removeGroup:function(a){var b=a.removeElem,c=b.next(),d=a.target[0].ownerDocument.createDocumentFragment();b.addClass("wf_empty").css({visibility:"hidden",height:b.height()+"px"});d=a.target[0].ownerDocument.createDocumentFragment();b.children().appendTo(d);a.fragments.push(d);a.revertElem=b;c.length&&c.hasClass("wf_group")&&
(a.removeElem=c)},revertGroup:function(a){var b=a.revertElem,c=b.prev();b.removeClass("wf_empty").append(a.fragments.pop()).css({visibility:"",height:""});a.removeElem=b;c.length&&c.hasClass("wf_group")&&(a.revertElem=c,a.scrollTop<c.data("changeOffset")&&e.revertGroup(a))},loadComplete:function(a,b){var c=a.data=a.serialize(b),d=a.wfCache.length,k=!1,f=a.target;c&&c.length&&(f.trigger("likecreatebefore",[b]),c.length>=a.cols?(a.groupElem=e.createGroup(a),a.groupElems=f.children("div.wf_group"),a.wfCache[d]=
[],a.colElem=e.createCol(a)):(d--,k=!0),a.prevHeights=a.heights.concat(),$.each(c,function(b,c){e.addCell(a,c,d)}),k||e.addCol(a,a.groupElem,d),e.getLoadOffset(a),void 0===a.removeElem&&(a.removeElem=a.groupElem),a.reLoading=!0,f.trigger("likecreateafter",[b]))},loadData:function(a){var b=a.load();a.reLoading=!1;setTimeout(function(){b&&("function"===typeof b.done?b.done(function(b){e.loadComplete(a,b)}):e.loadComplete(a,b))},50)},bindScroll:function(a){a.container.on("scroll.waterfall",function(){var b=
a.container.scrollTop(),c=a.scrollTop;a.scrollTop=b;a.reLoading&&~a.loadOffset&&b>=a.loadOffset&&e.loadData(a);b>c?(c=a.removeElem.data("changeOffset"),b>c&&e.removeGroup(a)):void 0!==a.revertElem&&(c=a.revertElem.data("changeOffset"),b<c&&e.revertGroup(a))})},bindResize:function(a){a.container.on("resize.waterfall",e.throttle(function(){var b=a.groupElems.filter(".wf_empty").last(),c=b.next(),d=a.containerHeight,k=a.colWidth,f=a.target,g,h;a.containerWidth=a.container.width()-a.reservedWidth;a.containerHeight=
Math.max(a.container.height(),600);h=a.containerWidth-a.containerWidth%k;g=h/k;g<a.minCols&&(g=a.minCols,h=g*k);g!==a.cols&&(!a.maxCols||g<=a.maxCols)?(a.cols=g,f.css("width",h+"px"),e.initHeights(a),$.each(a.wfCache,function(b,c){var d=a.groupElems.eq(b),f=e.getExtreme(a.heights,!1).item;a.colElem=e.createCol(a);$.each(c,function(b,c){e.addCell(a,c)});d.hasClass("wf_empty")?e.offlineSort(a,b,f):e.addCol(a,d)}),c.length?(a.scrollTop=c.offset().top+Math.round(c.height()/2),a.container.scrollTop(a.scrollTop),
a.removeElem=c,a.revertElem=b):(a.removeElem=a.groupElems.eq(0),delete a.revertElem),e.getLoadOffset(a),f.trigger("likeresize",[h])):a.containerHeight!==d&&e.getLoadOffset(a)}))},init:function(a){$.isWindow(a.container[0])?a.container[0].document.documentElement.style.overflowY="scroll":a.container[0].style.overflowY="scroll";var b,c=a.target,d=a.colWidth,k=a.maxCols,f=a.minCols,g,h,l=function(){a.initHeight=Math.min(c.height(),a.maxHeight);e.initHeights(a);e.loadData(a)};c.css({fontSize:"0px",lineHeight:"0px"});
a.containerWidth=a.container.width()-a.reservedWidth;a.containerHeight=Math.max(a.container.height(),600);a.initOffset=Math.round(1.5*a.containerHeight);g=a.containerWidth-a.containerWidth%d;h=g/d;k&&h>k?(g=k*d,h=k):h<a.minCols&&(g=f*d,h=f);a.cols=h;c.css("width",g+"px");a.init&&(b=a.init(g));b&&"function"===typeof b.done?b.done(l):l();e.bindScroll(a);a.maxCols!==a.minCols&&e.bindResize(a)}},l=function(a,b){a=$(a).eq(0);b=b||{};if(a.length){var c=$.extend({},m,b);this.__o__=c;c.container=$(c.container);
$.extend(c,{target:a,mainFragment:a[0].ownerDocument.createDocumentFragment(),initHeight:0,targetTop:a.offset().top,scrollTop:0,prevHeights:null,reLoading:!1,fixedHeight:!1,wfCache:[],fragments:[],loadOffset:-1,initOffset:0,heights:null,margins:null,offsets:{},containerWidth:0,containerHeight:0});e.init(c)}};l.prototype={on:function(a,b){if(this.__o__){var c=this;this.__o__.target.on("like"+a,function(d,e){d.type=a;e&&("number"===typeof e?d.width=e:d.extraData=e);b.call(c,d);d.stopPropagation()})}return this},
un:function(a){this.__o__&&this.__o__.target.off("like"+a);return this},reload:function(){var a=this.__o__;a.wfCache=[];a.fragments=[];a.reLoading=!1;a.groupElems.remove();delete a.init;delete a.cols;delete a.colElem;delete a.groupElem;delete a.groupElems;delete a.removeElem;delete a.revertElem;delete a.initHeight;e.init(a)},loadEnd:function(){this.__o__.reLoading=!1}};$.ui||($.ui={});$.ui.Waterfall=l});