/*
 COPYRIGHT 2009 ESRI

 TRADE SECRETS: ESRI PROPRIETARY AND CONFIDENTIAL
 Unpublished material - all rights reserved under the
 Copyright Laws of the United States and applicable international
 laws, treaties, and conventions.

 For additional information, contact:
 Environmental Systems Research Institute, Inc.
 Attn: Contracts and Legal Services Department
 380 New York Street
 Redlands, California, 92373
 USA

 email: contracts@esri.com
 */
//>>built
define("esri/dijit/geoenrichment/AgePyramid",["../../declare","require","dojo/_base/lang","./BaseSelectComparison","dojo/dom-construct","dojo/number","dojo/dom-class","./utils","./theme","dojox/charting/Chart","dojox/charting/axis2d/Default","dojox/charting/plot2d/Bars","dojox/charting/plot2d/Lines","dojox/charting/action2d/Tooltip","dojox/charting/action2d/Highlight","dojox/charting/SimpleTheme","dojo/i18n!../../nls/jsapi"],function(_1,_2,_3,_4,_5,_6,_7,_8,_9,_a,_b,_c,_d,_e,_f,_10,nls){nls=nls.geoenrichment.dijit.AgePyramid;var _11=_1("esri.dijit.geoenrichment.AgePyramid",[_4],{_widthExpanded:460,_widthCollapsed:200,_currentTheme:null,_themeChangeHandle:null,updateUI:function(){this.inherited(arguments);if(!this._themeChangeHandle){this._themeChangeHandle=_9.on("change",_3.hitch(this,this._onThemeChange));}if(this._currentTheme){this._doUpdateUI();}else{_9.load("AgePyramid").then(_3.hitch(this,this._onThemeLoad));}},_onThemeChange:function(){this._currentTheme=null;this._destroyChart();this.updateUI();},_onThemeLoad:function(_12){this._currentTheme=_12;this._currentTheme.theme=new _10(_12.theme);this._doUpdateUI();},_doUpdateUI:function(){this.ensureChart();var _13=[];for(var i=0;i<this._data.getColCount();i++){var col=this._data.getCol(i);if(col.name=="NAME"){continue;}_13.push(i);}this.maleIndices=[];this.femaleIndices=[];var _14=_13.length/2;var max=Number.NEGATIVE_INFINITY;var min=Number.POSITIVE_INFINITY;var _15,_16;var _17=true;var _18=true;for(var i=0;i<_13.length;i++){var _19=i<_14;if(_19){this.maleIndices.push(_13[i]);}else{this.femaleIndices.push(_13[i]);}var _1a=this._data.getRow(0)[_13[i]];if(_1a>max){max=_1a;_15=this._data.getCol(_13[i]).alias;_17=_19;}else{if(_1a<min){min=_1a;_16=this._data.getCol(_13[i]).alias;_18=_19;}}}var _1b=this.setSeriesData(this.chart.getSeries("male_bars"),0,this.maleIndices,-1);var _1c=this.setSeriesData(this.chart.getSeries("female_bars"),0,this.femaleIndices,1);var _1d,_1e;if(this.expanded){var _1f=this._getComparisonRow();_1d=this.setSeriesData(this.chart.getSeries("male_lines"),_1f,this.maleIndices,-1);_1e=this.setSeriesData(this.chart.getSeries("female_lines"),_1f,this.femaleIndices,1);}else{this.chart.getSeries("male_lines").update([]);this.chart.getSeries("female_lines").update([]);_1d=Number.NEGATIVE_INFINITY;_1e=Number.NEGATIVE_INFINITY;}this.extreme=_8.getCeiling(Math.max(_1b,_1c,_1d,_1e),true);this.chart.removeAxis("y");this.chart.addAxis("y",{type:_b,min:-this.extreme,max:this.extreme,minorTicks:false,labelFunc:_3.hitch(this,this.getAxisLabel)});this.chart.render();if(this.expanded){if(_17){_7.replace(this.max,"AgePyramid_TextMale","AgePyramid_TextFemale");}else{_7.replace(this.max,"AgePyramid_TextFemale","AgePyramid_TextMale");}if(_18){_7.replace(this.min,"AgePyramid_TextMale","AgePyramid_TextFemale");}else{_7.replace(this.min,"AgePyramid_TextFemale","AgePyramid_TextMale");}this.max.innerHTML=_15;this.min.innerHTML=_16;}},getAxisLabel:function(_20,_21,_22){_21=Math.abs(_21);if(_21!=this.extreme){return _6.format(_21,{places:0});}else{return _6.format(_21/100,{places:0,type:"percent"});}},ensureChart:function(){if(this.chart){return;}var _23=this._currentTheme;var _24=this.chart=new _a(this.chartDiv);_24.setTheme(_23.theme);_24.addPlot("lines",{type:_d,markers:true});_24.addPlot("bars",{type:_c,maxBarSize:(this.expanded?_23.maxBarSize:_23.maxBarSizeCollapsed)});_24.addSeries("male_bars",[],_3.mixin({plot:"bars"},_23.male));_24.addSeries("female_bars",[],_3.mixin({plot:"bars"},_23.female));_24.addSeries("male_lines",[],_3.mixin({plot:"lines"},_23.line));_24.addSeries("female_lines",[],_3.mixin({plot:"lines"},_23.line));var p={text:_3.hitch(this,this.getTooltip)};new _e(_24,"bars",p);new _e(_24,"lines",p);new _f(_24,"bars",{duration:1});new _f(_24,"lines",{duration:1,highlight:_23.highlight});},getTooltip:function(a){var _25=this._currentTheme;var _26;var _27;switch(a.run.name){case "male_bars":_26=this.maleIndices[a.index];_27=0;break;case "female_bars":_26=this.femaleIndices[a.index];_27=0;break;case "male_lines":_26=this.maleIndices[a.index];_27=this._getComparisonRow();break;case "female_lines":_26=this.femaleIndices[a.index];_27=this._getComparisonRow();break;default:return "";}var _28=this._data.getValue(_27,"NAME");var _29=this._data.getCol(_26).alias;var _2a=_6.format(this._data.getValue(_27,_26),{places:0});var pct=_6.format(Math.abs(a.x)/100,{places:1,type:"percent"});return "<div class='AgePyramid_Tooltip_Content'><span style='font:"+_25.font+"; color:"+_25.color+";'><b>"+_28+"</b > <br / > "+_29+"<br/>"+_2a+" ("+pct+")</span></div>";},setSeriesData:function(_2b,_2c,_2d,_2e){var _2f=[];var _30=0;for(var i=0;i<_2d.length;i++){var _31=this._data.getValue(_2c,_2d[i]);_2f.push(_31);_30+=_31;}var max=Number.NEGATIVE_INFINITY;for(var i=0;i<_2d.length;i++){var _31=_2f[i]/_30*100;_2f[i]=_31*_2e;if(_31>max){max=_31;}}if(_2b.plot=="lines"){for(var i=0;i<_2f.length;i++){_2f[i]={x:_2f[i],y:i+1};}}_2b.update(_2f);return max;},createUI:function(_32){this.inherited(arguments);_32.contentClass.push("AgePyramid_ContentPane");this.chartDiv=_32.addContent("div",{"class":"AgePyramid_Chart"});},createUIExpanded:function(_33){this.inherited(arguments);var _34=_33.addContent("div",{"class":"AgePyramid_MinMax"});_5.create("div",{innerHTML:nls.maxLabel},_34);this.max=_5.create("div",{"class":"AgePyramid_Text"},_34);_5.create("div",{"class":"AgePyramid_MinLabel",innerHTML:nls.minLabel},_34);this.min=_5.create("div",{"class":"AgePyramid_Text"},_34);var _35=_33.addContent("div",{"class":"AgePyramid_Comparison"});_5.create("div",{"class":"AgePyramid_ComparisonLabel",innerHTML:nls.compLabel},_35);this._createComboBox(_35);},createUICollapsed:function(_36){this.inherited(arguments);_5.create("div",{"class":"MenLabel",innerHTML:nls.menLabel},this.chartDiv);_5.create("div",{"class":"WomenLabel",innerHTML:nls.womenLabel},this.chartDiv);},destroy:function(){this._destroyChart();if(this._themeChangeHandle){this._themeChangeHandle.remove();this._themeChangeHandle=null;}this.inherited(arguments);},_destroyChart:function(){if(this.chart){this.chart.destroy();this.chart=null;}}});var _37={};return _11;});