define(["jquery","core/ajax"],function(a,b){"use strict";var c,d=!1,e={PAGE:"#page",TILE:".tile",TILEID:"#tile-",TILE_COLLAPSED:".tile-collapsed",TILES:".format-tiles.jsenabled ul.tiles",ACTIVITY:".activity",SPACER:".spacer",SECTION_ID:"#section-",OPEN_SECTION:".moveablesection:visible",SECTION_ZERO:"#section-0",CONTENT_SECTIONS:".moveablesection"},f=null,g=function(){var d=a(window).width(),g=new a.Deferred,h=a(e.TILES),i={standard:260,min:225,mobileMin:160};try{var j=h.parent().innerWidth(),k=a(h.find(e.TILE)[0]),l=k.width()?k.width():i.standard,m=14;l+=m;var n="inherit",o=Math.floor(j/i.min),p=a(e.TILE).not(e.SPACER).length;if(j<2*i.mobileMin)g.reject("Too narrow to resize");else if(p<=3&&j>p*i.mobileMin)n=4*i.standard;else if(j<3*i.min)n=2*(i.standard-m);else if(o<4)n=i.standard*o;else{var q=function(a,b){var c=[];for(a;a<=b;a+=1)c.push(a);return c},r=Math.min(Math.floor(j/l),p);if(p<=r&&p>5)n=(Math.floor(p/2)+1)*l;else if(r>3&&p/r<=3){var s=Math.floor(j/l),t=q(s,r).reverse(),u=t.map(function(a){return p%a});n=u.indexOf(0)!==-1?l*t[u.indexOf(0)]:pg&&(g=h),c=e)}),f.css("display","block"),i.each(function(c,e){b=a(e).attr("data-section"),0===d.length||d[d.length-1].sections.length>=g?(d.length>=1&&(d[d.length-1].displayAfterTile=d[d.length-1].sections[d[d.length-1].sections.length-1]),d.push({displayAfterTile:"",sections:[b]})):d[d.length-1].sections.push(b)}),d[d.length-1].displayAfterTile=d[d.length-1].sections[d[d.length-1].sections.length-1],d},moveContentSectionsToPlaces:function(b,c){b.forEach(function(c){c.sections.forEach(function(d){c.displayAfterTile===b[b.length-1].displayAfterTile?a(e.SECTION_ID+d).detach().insertAfter(a("ul.tiles .tile").last()):a(e.SECTION_ID+d).detach().insertAfter(a("#tile-"+c.displayAfterTile))})}),c.forEach(function(a){"function"==typeof a&&a()})},runReOrg:function(b){var c=new a.Deferred;d===!0&&c.reject("Re-org locked"),d=!0;var e=function(){h.moveContentSectionsToPlaces(h.getContentSectionPositions(),[function(){a("body").removeClass("modal-open"),c.resolve("Finished organising tiles"),d=!1}])};return b===!0?setTimeout(function(){e(),c.resolve("Re-org complete")},1e3):(e(),c.resolve("Re-org complete")),c.promise()}},i=function(){a(".block-hider-hide").click(function(){h.runReOrg(!0)}),a(".block-hider-show").click(function(){h.runReOrg(!0)}),a('.navbar button[data-action="toggle-drawer"]').click(function(){setTimeout(function(){h.runReOrg(!0),g()},600)})},j=function(){a(e.TILES).animate({opacity:1},"fast"),a(e.TILE).not(e.SPACER).each(function(b,c){c=a(c),setTimeout(function(){c.animate({opacity:c.hasClass("tile-hidden")||c.hasClass("tile-restricted")?.5:1},"fast")},10*b)}),a(e.SECTION_ZERO).animate({opacity:1},"fast"),a("#page-loading-icon").fadeOut(500).remove()};return{init:function(b,d,f,k){c=b,a(document).ready(function(){i(),"1"===a(e.TILES).css("opacity")&&h.runReOrg().done(function(){0!==d&&a(e.TILEID+d).click()});var b=function(){h.runReOrg().done(function(){0!==d&&0===a(e.OPEN_SECTION).length&&a(e.TILEID+d).click(),j()})};f&&!k?g().done(function(){b()}).fail(function(){b()}):b()})},resizeTilesDivWidth:function(){return g()},runReOrg:function(a){return h.runReOrg(a)}}});