(function(){var r=nspace("shadac.viz.map"),n="#FFF",t="#003B46",i="#003B46";r.mapConfig={mapColorsSequential:{0:{fill:"#FFFFFF",text:i,borderColor:"#C8C8C8"},1:{fill:"#BFE0E7",text:t,borderColor:"#FFFFFF"},2:{fill:"#41B2C5",text:t,borderColor:"#FFFFFF"},3:{fill:"#2D7988",text:n,borderColor:"#FFFFFF"},4:{fill:"#003B46",text:n,borderColor:"#FFFFFF"}},mapColorsQualitative:{0:{fill:"#FFFFFF",text:i,borderColor:"#C8C8C8"},1:{fill:"#003B46",text:n,borderColor:"#FFFFFF"},2:{fill:"#41B2C5",text:n,borderColor:"#FFFFFF"},3:{fill:"#BFE0E7",text:n,borderColor:"#FFFFFF"},4:{fill:"#E38431",text:n,borderColor:"#FFFFFF"},5:{fill:"#BD6127",text:n,borderColor:"#FFFFFF"},6:{fill:"#FAAF40",text:n,borderColor:"#FFFFFF"},7:{fill:"#CC8F34",text:n,borderColor:"#FFFFFF"},8:{fill:"#2D7988",text:n,borderColor:"#FFFFFF"},9:{fill:"#CBCDD1",text:n,borderColor:"#FFFFFF"},10:{fill:"#9EA0A3",text:n,borderColor:"#FFFFFF"},11:{fill:"#6C6C6C",text:n,borderColor:"#FFFFFF"}},shapeBorder:"#C8C8C8"}})(jQuery),function(){var n=nspace("shadac.viz.map");n.MapRouter=shadac.viz.VizRouterBase.extend({initialize:function(){_.bindAll(this,"updateUrl","updateDataDownload")},routes:{"":"initializeViews",":characteristicIds/:timeframeId/:dataTypeId":"initializeViews"},initializeViews:function(t,i,r){var u=this;this.selectorsModel=new n.models.MapSelectorsModel,this.selectorsModel.set({timeframeId:this.getValue(i),dataTypeId:this.getValue(r),characteristicIds:this.splitArrayValue(t)},{silent:!0}),this.dataModel=new n.models.MapDataModel({selectorsModel:this.selectorsModel}),this.mapView=new n.views.MapView({model:this.dataModel,el:".charts .content-wrapper .data"}),this.selectorsView=new n.views.MapSelectorsView({model:this.selectorsModel,el:".edit-panel"}),this.summaryView=new shadac.viz.selectors.summary.VizDataSummaryView({model:this.selectorsModel,el:".data-summary",selectorsView:this.selectorsView});this.selectorsModel.on("change",this.updateUrl);this.selectorsModel.on("change",this.updateDataDownload);this.selectorsModel.trigger("change")},updateUrl:function(){var n=[this.getArrayValueOrDefault(this.selectorsModel.get("characteristicIds")),this.getValueOrDefault(this.selectorsModel.get("timeframeId")),this.getValueOrDefault(this.selectorsModel.get("dataTypeId"))];window.shadacApp=window.shadacApp||{model:{}},shadacApp.model.attributes=this.selectorsModel.attributes,shadacApp.model.attributes.dataTypeIds=[shadacApp.model.attributes.dataTypeId],shadacApp.model.vizType="Map",this.navigate(n.join("/"),{replace:!0}),shadac.util.analyticsUtil.trackPageView()},updateDataDownload:function(){var n=new velir.collections.NameValueCollection,t;n.add(velir.datacenter.UrlParams.indicatorId,this.getValueOrDefault(this.selectorsModel.get("indicatorId")," ")),n.add(velir.datacenter.UrlParams.timeFrameId,this.getValueOrDefault(this.selectorsModel.get("timeframeId")," ")),n.add(velir.datacenter.UrlParams.dataFormatMapId,this.getValueOrDefault(this.selectorsModel.get("dataTypeId")," ")),n.add(velir.datacenter.UrlParams.characteristicId,this.getArrayValueOrDefault(this.selectorsModel.get("characteristicIds")," ")),t="/rawdata.axd?"+n.toQueryString(),$("#selected_data_download").attr("href",t)}})}(jQuery),function(n){var t=nspace("shadac.viz.map.managers");t.PolygonManager=function(t){this.mapView=t,this.$shapesLoaded=n.Deferred()},function(t){t.loadAndDrawLocations=function(){var t=this,i=this.mapView.model.get("shapes");n(this).trigger("rendering:locations"),this.locations={},_.each(i,function(n){t.locations[n.fips]=t.drawLocation(n)}),this.setHoverClickEvents(),n(this).trigger("rendered:locations"),this.$shapesLoaded.resolve()},t.drawLocation=function(n){var u=this,t={},i,r;return t.outline=this.mapView.paper.path(n.path).attr({fill:"#aaa",stroke:shadac.viz.map.mapConfig.shapeBorder,"stroke-width":1}).data("shape-id",n.fips).data("is-boundary",!0),n.rect&&(t.callout={},i=n.rect[0],r=n.rect[1],t.callout.box=this.mapView.paper.rect(i,r,24,24,0).attr({fill:"#aaa",stroke:shadac.viz.map.mapConfig.shapeBorder,"stroke-width":1}).data("shape-id",n.fips),t.callout.label=this.mapView.paper.text(i+12,r+12,n.abbr.toUpperCase()).attr({fill:"#000","font-size":"12px","font-weight":"bold"}).data("shape-id",n.fips)),t},t.colorLocations=function(t,i){var r=this;this.$shapesLoaded.done(function(){r._activeLocationId&&(r.clearLocationHighlight(r._activeLocationId),r._activeLocationId=undefined),n(r).trigger("coloring:locations");for(var u in r.locations)r.colorLocation(r.locations[u],t.dataPoints[u],i);n(this).trigger("colored:locations")})},t.colorLocation=function(n,t,i){var u=this,r;t||(t={choroplethGroup:0}),r=i[t.choroplethGroup||0],n.outline.animate({fill:r.fill},500),n.callout&&(n.callout.box.animate({fill:r.fill},500),n.callout.label.animate({fill:r.text},500))},t.setHoverClickEvents=function(){var t=this;n(".map-wrap",t.mapView.$el).mousemove(_.throttle(function(i){var u=i.pageX-n(window).scrollLeft(),f=i.pageY-n(window).scrollTop(),r=t.findShapeForPoint(u,f);if(r){r.data("shape-id")&&r.data("shape-id")!=t._activeLocationId?(t._activeLocationId&&n(t).trigger("mouseleave",t._activeLocationId),t._activeLocationId=r.data("shape-id"),n(t).trigger("mouseenter",[t._activeLocationId,t.getMousePosition(i)])):r.data("shape-id")&&r.data("shape-id")==t._activeLocationId&&n(t).trigger("mousemove",[t._activeLocationId,t.getMousePosition(i)]);return}t._activeLocationId&&n(t).trigger("mouseleave",t._activeLocationId),t._activeLocationId=null},25))},t.findShapeForPoint=function(t,i){var r=n(this.mapView.paper.canvas),u;return r.css("z-index",1e3),u=this.mapView.paper.getElementByPoint(t,i),r.css("z-index","auto"),u},t.highlightLocation=function(n){var t=this.locations[n];this.highlightElement(t.outline),t.callout&&this.highlightElement(t.callout.box)},t.clearLocationHighlight=function(n){var t=this.locations[n];this.clearElementHighlight(t.outline),t.callout&&this.clearElementHighlight(t.callout.box)},t.highlightElement=function(t){if(t.data("is-boundary")){var i=n.browser.msie&&parseInt(n.browser.version,10)<9;i?(t.st||(t.st=t.attr("stroke")),t.attr({stroke:"#E38431","stroke-width":4})):t.hl=t.clone().attr({stroke:"#E38431","stroke-width":8,"fill-opacity":0}),t.toFront()}else t.st||(t.st=t.attr("stroke")),t.attr({stroke:"#E38431","stroke-width":2})},t.clearElementHighlight=function(n){n&&n.hl&&n.hl.remove(),n&&n.st&&n.attr({stroke:n.st,"stroke-width":1})},t.getMousePosition=function(n){return{x:n.clientX-n.currentTarget.getBoundingClientRect().left,y:n.clientY-n.currentTarget.getBoundingClientRect().top}}}(t.PolygonManager.prototype)}(jQuery),function(){var n=nspace("shadac.viz.map.models");n.MapDataModel=Backbone.Model.extend({serviceUrl:"/api/mapdata/data",shapesUrl:"/js/shadac/viz/map/usmap-data.json",initialize:function(n){var t=this;this.selectorsModel=n.selectorsModel;this.selectorsModel.on("change",_.debounce(function(){t.fetchData()},25));this.$shapesLoaded=$.get(this.shapesUrl).done(function(n){t.set("shapes",n)})},fetchData:function(){var n=this;if(this.trigger("loading:data"),this.previousRequestObject&&this.previousRequestObject.dataTypeId===this.selectorsModel.get("dataTypeId")&&this.previousRequestObject.characteristicIds===this.selectorsModel.get("characteristicIds")){this.trigger("complete:data");return}this.previousRequestObject={dataTypeId:this.selectorsModel.get("dataTypeId"),characteristicIds:this.selectorsModel.get("characteristicIds")},$.get(this.serviceUrl,{indicators:n.selectorsModel.get("indicatorId"),dataFormatMaps:n.selectorsModel.get("dataTypeId"),characteristics:n.selectorsModel.get("characteristicIds").join(","),timeFrames:n.selectorsModel.get("timeframeId"),locationtype:2}).done(function(t){n.set("data",t),n.trigger("complete:data")}).error(function(){n.trigger("error:data")})},getCurrentDataSeries:function(){var i=this,t=this.get("data"),n;return t?(n=_.filter(t.series,function(n){return n.timeFrameId==+i.selectorsModel.get("timeframeId")}),n&&n.length==1?n[0]:undefined):undefined}})}(jQuery),function(){var n=nspace("shadac.viz.map.models");n.MapSelectorsModel=Backbone.Model.extend({})}(jQuery),function(){var n=nspace("shadac.viz.map.views");n.MapHoverBoxView=Backbone.View.extend({initialize:function(){var n=this,t=new velir.caching.AsyncTemplateCache("/js/shadac/templates/map");this.$templateLoading=t.get("map-hoverbox").done(function(t){n.template=t})},render:function(n,t){var i=this;this.$templateLoading.done(function(){var r=i.model.getCurrentDataSeries();r&&(i.$el.html(i.template({dataPoint:r.dataPoints[n]})),i.$el.show(),i.move(t))})},move:function(n){var t=n.x-this.$el.outerWidth()/2,i=n.y-this.$el.outerHeight()-10,r=this.$el.parent().outerWidth(),u=this.$el.parent().outerHeight();t<0?t=0:t+this.$el.outerWidth()>r&&(t=r-this.$el.outerWidth()),i<0?i=n.y+10:i+this.$el.outerHeight()>u&&(i=u-this.$el.outerHeight()),this.$el.css("left",t+"px").css("top",i+"px")},hide:function(){this.$el.hide()}})}(jQuery),function(){var n=nspace("shadac.viz.map.views");n.MapLegendView=Backbone.View.extend({initialize:function(){var n=this,t=new velir.caching.AsyncTemplateCache("/js/shadac/templates/map");this.$templateLoading=t.get("map-legend").done(function(t){n.template=t})},render:function(n){var t=this;this.$templateLoading.done(function(){var r=t.model.getCurrentDataSeries(),i;legendData=_.map(r.choroplethGroups,function(t,i){var r=n[i+1];return{color:r.fill,text:t.display,textColor:r.text,borderColor:r.borderColor}}),legendData.push({color:n[0].fill,text:"Not Available",textColor:n[0].text,borderColor:n[0].borderColor}),t.$el.html(t.template({legend:legendData})),i=_.reduce($("li",t.$el),function(n,t){return n+$(t).outerWidth()},0),$("ul",t.$el).width(i+1)})}})}(jQuery),function(){var n=nspace("shadac.viz.map.views");n.MapSelectorsView=Backbone.View.extend({initialize:function(){var n=this;this.constants=new shadac.viz.selectors.VizConstantsView({el:this.$el,model:this.model}),this.distributions=new shadac.viz.selectors.distributions.DistributionCollectionView({el:$(".distributions"),model:this.model}),this.timeFrameSelector=new shadac.viz.selectors.TimeFrameSingleSelectorView({el:$(".timeframe"),model:this.model}),this.dataTypeSelector=new shadac.viz.selectors.DataTypeSingleSelectorView({el:$(".dataType",this.$el),model:this.model}),this.constants.setUp(!0),this.timeFrameSelector.setUp(!0),this.distributions.setUpDistributionSelectors(""),this.dataTypeSelector.hideCount(),this.dataTypeSelector.setUp(!0),shadac.util.urlUtil.isCleanViz(!0)||this.$el.slideDown()},getDistributionSummary:function(){var n=this.distributions.getDataSummaries();return n.join("")},getTimeFrameSummary:function(){return this.timeFrameSelector.getDataSummary()},getDataTypeSummary:function(){return this.dataTypeSelector.getDataSummary()}})}(jQuery),function(){var n=nspace("shadac.viz.map.views");n.MapTimeSliderView=Backbone.View.extend({staggerLabelsAt:6,initialize:function(){var n=this,t=new velir.caching.AsyncTemplateCache("/js/shadac/templates/map");this.$templateLoading=t.get("map-slider").done(function(t){n.template=t});this.model.on("change:data",function(){n.render()});this.model.selectorsModel.on("change:timeframeId",function(){n.updateSlider()});this.playState="stopped";this.$el.on("click",".trigger-play",function(){n.play()});this.$el.on("click",".trigger-stop",function(){n.stopPlaying()})},render:function(){var n=this;this.$templateLoading.done(function(){var o,i,e,u,r,s,f,h,t;if(n.$el.empty(),n.$el.append(n.template()),o=n.$el.find(".dc-map-play-labels"),i=n.model.get("data"),i.series.length<2){n.$el.hide();return}for(shadac.util.urlUtil.isCleanViz(!0)||n.$el.show(),u=0,r=0;r<i.series.length;r++)t=$("<span><\/span>"),o.append(t),i.series.length>=n.staggerLabelsAt&&r%2==0?(f="",t.addClass("small")):f=i.series[r].timeFrameName.replace(" ","&nbsp;"),t.append(f),s=100/(i.series.length-1)*r,t.css("left",s+"%"),t.css("whitespace","nowrap"),h=t.width()>0?t.width():40,t.css("margin-left","-"+h/2+"px"),f&&(e&&shadac.util.collisionDetection.collides(t,e)?(t.css("padding-top",43+u),u+=20):(u=0,e=t));n.$el.find(".ui-slider").slider({min:0,max:i.series.length-1,step:1,change:function(n,t){var r=$(".edit-panel .timeframe select"),u=i.series[t.value].timeFrameId.toString();r.val()!=u&&(r.val(u).multiselect("refresh"),r.trigger("change"))}}),n.updateSlider()})},updateSlider:function(){var t=this.model.get("data"),i=+this.model.selectorsModel.get("timeframeId"),n=0;_.each(t.series,function(t,r){t.timeFrameId==i&&(n=r)}),this.$el.find(".ui-slider").slider({value:n})},play:function(){$(".dc-play").removeClass("trigger-play").addClass("trigger-stop");var n=this.model.get("data");this.playState="playing",this.$el.find(".ui-slider").slider({value:0}),this.toggleSliderEnabled(!1),this.playYear(1,n.series.length)},playYear:function(n,t){if(n==t||this.playState=="stopped"){this.stopPlaying();return}var i=this;setTimeout(function(){i.playState!="stopped"&&(i.$el.find(".ui-slider").slider({value:n}),i.playYear(n+1,t))},1500)},stopPlaying:function(){$(".dc-play").removeClass("trigger-stop").addClass("trigger-play"),this.playState="stopped",this.toggleSliderEnabled(!0)},toggleSliderEnabled:function(n){this.$el.find(".ui-slider").slider({disabled:!n})}})}(jQuery),function(){var n=nspace("shadac.viz.map.views");n.MapView=shadac.viz.AsyncBaseView.extend({completeEvent:"complete:data",errorEvent:"error:data",loadingEvent:"loading:data",initialize:function(){var t=this;return $(".async-content",this.$el).empty(),this.legendView=new n.MapLegendView({el:$('<div class="map-legend" />').appendTo($(".async-content",this.$el)),model:this.model}),this.$canvas=$('<div class="map-container" />').appendTo($('<div class="map-wrap" />').appendTo($(".async-content",this.$el))),this.$canvas.height(this.$canvas.width()*.6325),this.hoverView=new n.MapHoverBoxView({el:$('<div class="map-hover" />').appendTo($(".map-wrap",this.$el)),model:this.model}),this.timeSliderView=new n.MapTimeSliderView({el:$('<div class="map-slider" />').appendTo($(".map-wrap",this.$el)),model:this.model}),this.paper=Raphael(this.$canvas[0],950,600),this.paper.setViewBox(0,0,950,600,!0),this.paper.setSize(t.$canvas.width(),t.$canvas.height()),this.polygonManager=new shadac.viz.map.managers.PolygonManager(this),this.model.$shapesLoaded.done(function(){t.polygonManager.loadAndDrawLocations()}),$(this.polygonManager).bind("mouseenter",function(n,i,r){t.polygonManager.highlightLocation(i),t.hoverView.render(i,r)}),$(this.polygonManager).bind("mousemove",function(n,i,r){t.hoverView.move(r)}),$(this.polygonManager).bind("mouseleave",function(n,i){t.polygonManager.clearLocationHighlight(i),t.hoverView.hide()}),this.setup({fadeContentWhenLoading:!0})},render:function(){var n=this;this.model.$shapesLoaded.done(function(){var r=n.model.getCurrentDataSeries(),i,t;n.hoverView.hide(),i=n.model.get("data"),t=i.dataFormatPrimitive==="Text"?shadac.viz.map.mapConfig.mapColorsQualitative:shadac.viz.map.mapConfig.mapColorsSequential,n.polygonManager.colorLocations(r,t),n.legendView.render(t),setTimeout(function(){shadac.util.imageDownload.fireDataReady()},500)})}})}(jQuery)