define(["cilantro/rest/datasource","cilantro/rest/renderer","cilantro/report/templates"],function(a,b,c){function d(){var d=!0,e=$("body"),f=$("#content"),g=$("#report"),h=$("#report-info"),i=$("#actions"),j=$("#details"),k=g.find(".toolbar"),l=$("#table"),m=l.find("thead tr"),n=l.find("tbody"),o=$(".per-page"),p=$(".page-links"),q=$("#report-unique"),r=$("#report-count"),s=$("#no-results"),t={table_header:new b.template({target:m,template:c.header}),table_rows:new b.template({target:n,template:c.row}),pages:new b.template({target:p,template:c.pages})},u={scope_info:new a.ajax({ajax:{url:App.endpoints.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>"}j.html(b)}}}}),table_header:new a.ajax({ajax:{url:App.endpoints.session.perspective,success:function(a){t.table_header.render(a.header)}}}),table_rows:new a.ajax({ajax:{url:App.endpoints.session.report,data:{data:1},beforeSend:function(){g.block()},complete:function(){g.unblock()},success:function(a){a.unique!==null&&(a.unique.toString()!==q.text()&&q.parent().effect("highlight",1e3),q.text(a.unique)),a.count!==null&&(a.count.toString()!==r.text()&&r.parent().effect("highlight",1e3),r.text(a.count));if(a.count===0){l.hide(),k.hide(),s.show(),i.hide();return}s.hide(),l.show(),i.show(),k.show(),t.table_rows.render(a.rows),a.pages?(t.pages.render(a.pages),t.pages.target.show()):t.pages.target.hide(),a.per_page&&o.val(a.per_page),g.trigger("resize-report")}}})};u.scope_info.get(),u.table_header.get(),u.table_rows.get(),g.bind("resize-report",function(a){var b=900,c=g.innerWidth(),d=l.outerWidth(!0)+4,e=Math.min(d,$(window).width()-30);e=Math.max(b,e);if(e==c)return;f.animate({width:e}),g.animate({width:e})});var v;$(window).resize(function(){clearTimeout(v),v=setTimeout(function(){g.trigger("resize-report")},200)}),e.bind("update.report",function(a,b){b&&(b.data=1);var c={data:b};return u.table_rows.get(c,!0),!1}),e.bind("update.perspective",function(a,b){return b?(b={replace:b},$.patchJSON(App.endpoints.session.perspective,JSON.stringify(b),function(){e.trigger("update.report"),"columns"in b.replace&&u.table_header.get(null,!0)})):$.getJSON(App.endpoints.session.perspective,function(){e.trigger("update.report"),u.scope_info.get(null,!0),u.table_header.get(null,!0)}),!1}),g.delegate(".per-page","change",function(a){return this.value&&e.trigger("update.report",{size:this.value}),!1}),m.delegate(".header","click",function(a){var b,c=$(this),d=c.attr("data-id"),f=c.siblings();return f.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"),e.trigger("update.perspective",[{ordering:[[d,b]]}]),!1}),p.delegate("a","click",function(a){return e.trigger("update.report",{page:this.hash.substr(1)}),!1})}return{init:d}})