(function(n){var t=nspace("kidscount.customReport");t.CustomReportApp=function(){var t=this;this.model=new kidscount.customReport.CustomReportModel,n('a.create_custom_report, a[href="#customreport"], button.create_custom_report').click(function(t){var i,r,f,u;t.preventDefault(),n("#custom_rep_2").hide(),n("#custom_rep_1").show(),n("#loading-custom-indicators").hide(),i=n(this).attr("data-selection-type"),r=n(this).attr("data-selection-value"),i&&r&&(f="custrep-"+i+"-"+r,n("#custom_report_modal #custrep-locations input#"+f).click()),n(this).hasClass("btn--state-shortcut")&&(u=n(".custom-report-selector--parent select").val(),u&&(n("#custrep-parent-location option[value='"+u+"']").prop("selected",!0),n("#custrep-parent-location").trigger("change"))),kidscount.lightbox.openLightBox(n("#custom_report_modal")),n.browser.msie&&n.browser.version==7||(n(".embed_scroll_active:not(.mCustomScrollbar)",n("#custom_report_modal")).mCustomScrollbar("update"),n(".embedded_input",n("#custom_report_modal")).hasClass("mCustomScrollbar")||n(".embedded_input",n("#custom_report_modal")).mCustomScrollbar({set_width:160,set_height:175,theme:"dark-thick",scrollButtons:{enable:!0},advanced:{autoScrollOnFocus:!1,updateOnContentResize:!0}}))});n("#custom_report_modal").on("click",".next:not(.inactive)",function(){var r,i;if(t.model._currentAttributes.availableIndicators.indicatorCount>0){for(r=n.find(".errorWarning"),i=0;i<r.length;i++)r[i].style.visibility="hidden";n("#custom_rep_1").hide(),n("#custom_rep_2").show(),n.browser.msie&&n.browser.version==7||n(".file_form",n("#custom_report_modal")).mCustomScrollbar("update")}else n("#noIndErr")[0].style.visibility="visible"});n("#custom_report_modal .cancel").click(function(){for(var i=n.find(".errorWarning"),t=0;t<i.length;t++)i[t].style.visibility="hidden";n(".modal").hide(),n("#lightbox_cont").hide(),n("#loading-custom-indicators").hide()}),n("#custom_report_modal .prev").click(function(){for(var i=n.find(".errorWarning"),t=0;t<i.length;t++)i[t].style.visibility="hidden";n("#custom_rep_2").hide(),n("#custom_rep_1").show()}),n("#custom_report_modal .create").click(function(){var r,i,u,f,e;if(n("#custrep-viewall-ind").is(":checked")||_.some(n(".custrep-ind-chk"),function(n){return n.checked})){for(r=n.find(".errorWarning"),i=0;i<r.length;i++)r[i].style.visibility="hidden";n("#custom_rep_2").hide(),n(".close_modal").hide(),n("#custom_report_modal").css("min-height","150px"),setTimeout(n("#loading-custom-indicators").show(),100),u=t.model.get("selectedLocationIds"),e=t.model.get("allIndicatorsSelected"),f=e?"any":velir.util.Packer.pack(t.model.get("selectedIndicatorIds").sort()),setTimeout(function(){window.location="/data/customreports/"+u.toString()+"/"+f},1500)}else n(".custom_report_modal #indSelErr")[0].style.visibility="visible"});n("#custom_report_modal").off("click.plusminus").on("click.plusminus",".file_form dt",function(){n(this).toggleClass("open"),n(this).siblings("dd").toggle(),n.browser.msie&&n.browser.version==7||n(".file_form",n("#custom_report_modal")).mCustomScrollbar("update")});this.model.on("change:selectedLocationIds",function(){t.model.get("selectedLocationIds")?n("#custom_report_modal .next").removeClass("inactive"):n("#custom_report_modal .next").addClass("inactive")});this.parentLocationSelectionView=new kidscount.customReport.ParentLocationSelectionView({el:"#custrep-parent-location",model:this.model}),this.locationSelectionView=new kidscount.customReport.LocationSelectionView({el:"#custrep-locations",model:this.model}),this.indicatorSelectionView=new kidscount.customReport.IndicatorSelectionView({el:"#custrep-indicators",model:this.model})},function(n){n.start=function(){this.model.set("selectedParentLocationId",this.parentLocationSelectionView.selectedValue())}}(t.CustomReportApp.prototype)})(jQuery),function(n,t){var i=nspace("kidscount.customReport");i.CustomReportModel=Backbone.Model.extend({initialize:function(){t.bindAll(this,"getChildLocations","getAvailableIndicators");this.on("change:selectedParentLocationId",this.getChildLocations);this.on("change:selectedLocationIds",this.getAvailableIndicators)},defaults:{childLocations:null,availableIndicators:null,selectedParentLocationId:null,selectedLocationIds:[],selectedIndicatorIds:null,allIndicatorsSelected:!1},getChildLocations:function(){var t=this,i;n(this).trigger("busy:locations"),i="/api/customreportlocation",n.ajax({url:i,data:{id:t.get("selectedParentLocationId")},success:function(i){t.set("childLocations",i),n(t).trigger("idle:locations")}})},getAvailableIndicators:function(){var t=this,i;n(this).trigger("busy:indicators"),i="/api/customreportindicator",n.ajax({url:i,data:{locationIds:t.get("selectedLocationIds")},success:function(i){t.set("availableIndicators",i),n(t).trigger("idle:indicators")}})}})}(jQuery,_),$(function(){var n,t,i;$("#indicator-jump").change(function(){$("html, body").scrollTop($("#ind"+$(this).val()).offset().top-10)}),$("#comparison-checkbox").change(function(){var t=window.location.href.toLowerCase().replace(/[\/,]+$/,"").split("/"),n=$.trim(t[t.length-1]),i;n.match(/[a-zA-Z]/)&&n.indexOf("any")==-1?i=t.slice(0,t.length-1).join("/"):(i=t.join("/"),n=""),n.indexOf("compared")!=-1?n=n.replace(/compared/,"nocompare"):n.indexOf("nocompare")!=-1?n=n.replace(/nocompare/,"compared"):n+=n.length>0?",nocompare":"nocompare",window.location.href=i+"/"+n}),$("div.custom_report_table").each(function(){for(var t=$(this),i=t.find("input.num-merged-cols").val(),n=1;n<=i;n++)kidscount.vizUtil.mergeCells(t.find("table").first(),n)}),n=$("#custom_report_modal select#custrep-parent-location"),t=$("#cust-report-location-id").val(),t&&(i=$("option[data-abbr='"+t+"']",n).attr("value"),i&&(n.val(i),n.change()))}),function(n,t){var i=nspace("kidscount.customReport");i.IndicatorSelectionView=Backbone.View.extend({initialize:function(){var i=this;t.bindAll(this,"render");this.model.on("change:availableIndicators",this.render);n(this.model).on("busy:indicators",function(){i.$el.siblings(".loading-indicators").show(),i.$el.hide()});n(this.model).on("idle:indicators",function(){i.$el.siblings(".loading-indicators").hide(),i.$el.show()});this.$el.on("change","#custrep-viewall-ind",function(){n(this).is(":checked")?i.model.set("allIndicatorsSelected",!0):i.model.set("allIndicatorsSelected",!1)});this.$el.on("change",'.file_form input[type="checkbox"]',function(){n("#custrep-view-all-ind").is(":checked")||i.setIndicators()})},render:function(){var u=this,t=n("#customReportIndicatorTpl").html(),i={indicatorSelectNode:n("#customReportIndicatorSelectNodeTpl").html()},r=Mustache.to_html(t,{availableIndicators:this.model.get("availableIndicators"),name:this.model.get("childLocations").currentLocation.name},i);this.$el.html(r),n.browser.msie&&n.browser.version==7||n(".file_form").mCustomScrollbar({theme:"dark-thick",set_height:300,scrollButtons:{enable:!0},advanced:{autoScrollOnFocus:!1,updateOnContentResize:!0}})},setIndicators:function(){var t=n.map(n('.file_form input[type="checkbox"]:checked',this.$el),function(t){return n(t).val()});this.model.set("selectedIndicatorIds",t)}})}(jQuery,_),function(n,t){var i=nspace("kidscount.customReport");i.LocationSelectionView=Backbone.View.extend({initialize:function(){var i=this;t.bindAll(this,"render");this.model.on("change:childLocations",this.render);n(this.model).on("busy:locations",function(){i.$el.siblings(".loading-indicators").show(),i.$el.hide()});n(this.model).on("idle:locations",function(){i.$el.siblings(".loading-indicators").hide(),i.$el.show()})},render:function(){var u=this,e=n("#customReportLocationTpl").html(),i=this.model.get("childLocations"),f,r,t;i.currentLocation.labelPrefix=i.currentLocation.id===1?"For the ":"For ",i.currentLocation.id===1?n(".location-context").show():n(".location-context").hide(),f=Mustache.to_html(e,i),this.$el.html(f),r=function(t){var i=[],r,f;(n(t).attr("type")==="radio"&&n.each(n("input[name='loc']"),function(){n(this).attr("checked",!1)}),n(this).siblings(".form_toggle_div").length===0?i.push(n(t).val()):n.each(n("input[name='loc']:checked"),function(){i.push(n(t).val())}),r=!0,i.forEach(function(n){isNaN(n)&&(r=!1)}),i.length!==0&&r)&&(f=i.join(","),u.model.set("selectedLocationIds",f))},n("input[name='loct']",this.$el).click(function(){r(this)}),n("input[name='loc']",this.$el).change(function(){var t=[],i,r;(n.each(n("input[name='loc']:checked"),function(){t.push(n(this).val())}),i=!0,t.forEach(function(n){isNaN(n)&&(i=!1)}),t.length!==0&&i)&&(r=t.join(","),u.model.set("selectedLocationIds",r))}),t=n("input[name='loct']:checked"),t.length==2&&t.first().text()==t.last().text()&&(t=t.last()),r(t)}})}(jQuery,_),function(n){var t=nspace("kidscount.customReport");t.ParentLocationSelectionView=Backbone.View.extend({initialize:function(){var t=this;this.$el.change(function(){t.model.set("selectedParentLocationId",n(this).val())})},selectedValue:function(){return this.$el.val()}})}(jQuery,_)