define(["cilantro/rest/datasource","cilantro/rest/renderer","cilantro/report/templates"],function(a,b,c){function e(){var e=!0,f=$("body"),g=$("#content"),h=$("#report"),i=$("#report-info"),j=$("#actions"),k=$("#details"),l=h.find(".toolbar"),m=$("#table"),n=m.find("thead tr"),o=m.find("tbody"),p=$(".per-page"),q=$(".page-links"),r=$("#report-unique"),s=$("#report-count"),t={table_header:new b.template({target:n,template:c.header}),table_rows:new b.template({target:o,template:c.row}),pages:new b.template({target:q,template:c.pages})},u={scope_info:new a.ajax({ajax:{url:App.urls.session.scope,success:function(a){if(a.conditions){var b="";if(typeof a.conditions=="string")b="<li>"+a.text+"</li>";else{var c=a.conditions;for(var d=0;d<c.length;d++)b+="<li>"+c[d].condition+"</li>"}k.html(b)}}}}),table_header:new a.ajax({ajax:{url:App.urls.session.perspective,success:function(a){t.table_header.render(a.header)}}}),table_rows:new a.ajax({ajax:{url:App.urls.session.report,data:{data:1},beforeSend:function(){h.block()},complete:function(){h.unblock()},success:function(a){a.unique!==null&&(a.unique.toString()!==r.text()&&r.parent().effect("highlight",1e3),r.text(a.unique)),a.count!==null&&(a.count.toString()!==s.text()&&s.parent().effect("highlight",1e3),s.text(a.count));if(a.count===0){$(".content").html(d).css("text-align","center"),j.hide();return}t.table_rows.render(a.rows),a.pages?t.pages.render(a.pages):(e&&p.hide(),t.pages.target.hide()),a.per_page&&p.val(a.per_page),setTimeout(function(){h.trigger("resize-report")},300),e&&(e=!1,setTimeout(function(){i.show(),l.show(),m.show()},500))}}})};u.scope_info.get(),u.table_header.get(),u.table_rows.get(),h.bind("resize-report",function(a){var b=900,c=h.innerWidth(),d=m.outerWidth(!0)+4,e=Math.min(d,$(window).width()-30);e=Math.max(b,e);if(e==c)return;g.animate({width:e}),h.animate({width:e})});var v;$(window).resize(function(){clearTimeout(v),v=setTimeout(function(){h.trigger("resize-report")},200)}),f.bind("update.report",function(a,b){b&&(b.data=1);var c={data:b};return u.table_rows.get(c,!0),!1}),f.bind("update.perspective",function(a,b){return b={replace:b},$.patchJSON(App.urls.session.perspective,JSON.stringify(b),function(){f.trigger("update.report"),"columns"in b.replace&&u.table_header.get(null,!0)}),!1}),h.delegate(".per-page","change",function(a){return this.value&&f.trigger("update.report",{size:this.value}),!1}),n.delegate(".header","click",function(a){var b,c=$(this),d=c.attr("data-id"),e=c.siblings();return e.removeClass("asc").removeClass("desc"),c.hasClass("asc")?(c.removeClass("asc").addClass("desc"),b="desc"):c.hasClass("desc")?(c.removeClass("desc"),b=""):(c.addClass("asc"),b="asc"),f.trigger("update.perspective",[{ordering:[[d,b]]}]),!1}),q.delegate("a","click",function(a){return f.trigger("update.report",{page:this.hash.substr(1)}),!1})}var d='<div style="text-align: center; display: inline;" class="message warning">No results match your conditions. <a href="'+App.urls.define+'">Refine your conditions</a>.</div>';return{init:e}})