define(["jquery","cilantro/rest/datasource","cilantro/rest/renderer","cilantro/report/templates"],function(a,b,c,d){function e(){var c=a("body"),d=a("#columns"),e=a("#active-columns"),f=a("#columns-dialog"),g=a("#search"),h=a("form",f),i=a("#data-columns");f.cache={},f.get=function(a){if(!f.cache[a]){var b="[data-model=column][data-id="+a+"]";f.cache[a]={src:d.find(b),tgt:e.find(b)}}return f.cache[a]},f.bind("addall.column",function(b,c){var d=a("[data-model=category][data-id="+c+"]"),e=d.find(".active:not(.filtered)");d.hide();for(var g=0;g<e.length;g++)f.trigger("add.column",[a(e[g]).attr("data-id")]);return!1}),f.bind("add.column",function(a,b){map=f.get(b),map.src.removeClass("active");var c=map.src.siblings(".active:not(.filtered)");return c.length==0&&map.src.parents("[data-model=category]").hide(),e.append(map.tgt.detach().addClass("active")),!1}),f.bind("remove.column",function(a,b){return map=f.get(b),map.tgt.removeClass("active"),map.src.addClass("active").parents("[data-model=category]").show(),!1}),f.bind("removeall.column",function(a){for(var b in f.cache)f.trigger("remove.column",[b]);return!1}),f.bind("search.column",function(a,b){return g.trigger("search",b),!1}),f.bind("save.column",function(b){var d=e.children(".active"),f=a.map(d,function(b,c){return parseInt(a(b).attr("data-id"))});return c.trigger("update.perspective",[{columns:f}]),!1}),f.bind("filter.column",function(a,b){map=f.get(b),map.src.addClass("filtered");var c=map.src.siblings(".active:not(.filtered)");c.length==0&&map.src.parents("[data-model=category]").hide()}),f.bind("filterall.column",function(b){var c=d.find("[data-model=column]");for(var e=c.length;e--;)f.trigger("filter.column",[a(c[e]).attr("data-id")]);return!1}),f.bind("unfilter.column",function(a,b){return map=f.get(b),map.src.removeClass("filtered"),map.src.parents("[data-model=category]").show(),!1});var j={perspective:new b.ajax({ajax:{url:App.endpoints.session.perspective,success:function(a){if(a.store){f.trigger("removeall.column");var b=a.store.columns;for(var c=0;c<b.length;c++)f.trigger("add.column",[b[c]])}}}})};j.perspective.get(),App.hub.subscribe("report/revert",function(){j.perspective.get(null,!0),c.trigger("update.perspective")});var k=a('<div id="description"></div>').appendTo("body"),l=null;a("#columns").delegate("li","mouseenter",function(){clearTimeout(l);var b=this;l=setTimeout(function(){var c=a(b),d=c.offset(),e=c.outerWidth(),f=c.children(".description").html();k.html(f),k.css({left:d.left+e+20,top:d.top}).show()},700)}).delegate("li","mouseleave",function(){clearTimeout(l),k.hide()}),g.autocomplete2({success:function(a,b){g.trigger("filterall.column");for(var c=0;c<b.length;c++)g.trigger("unfilter.column",[b[c]])}},null,50),d.delegate(".add-column","click",function(a){clearTimeout(l),k.hide();var b=this.hash.substr(1);return f.trigger("add.column",[b]),!1}),d.delegate(".add-all","click",function(a){var b=this.hash.substr(1);return f.trigger("addall.column",[b]),!1}),e.delegate(".remove-column","click",function(a){var b=this.hash.substr(1);return f.trigger("remove.column",[b]),!1}),f.delegate(".remove-all","click",function(a){return f.trigger("removeall.column"),!1}),f.dialog({autoOpen:!1,draggable:!0,resizable:!1,title:"Add or Remove Columns from this Report",height:600,width:900,buttons:{Cancel:function(){f.dialog("close")},"Update Columns":function(){f.trigger("save.column"),f.dialog("close")}}}),e.sortable({placeholder:"placeholder",forcePlaceholderSize:!0,forceHelperSize:!0,opacity:.5,cursor:"move",tolerance:"intersect"}).disableSelection(),i.bind("click",function(a){f.dialog("open")})}return{init:e}})