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