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