/*
 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
 */

if(!dojo._hasResource["esri.dijit.Scalebar"]){dojo._hasResource["esri.dijit.Scalebar"]=true;dojo.provide("esri.dijit.Scalebar");dojo.require("esri.geometry");dojo.require("esri.map");dojo.require("esri.WKIDUnitConversion");(function(){var _1=[dojo.moduleUrl("esri.dijit","css/Scalebar.css")];var _2=document.getElementsByTagName("head").item(0),_3;for(var i=0,il=_1.length;i<il;i++){_3=document.createElement("link");_3.type="text/css";_3.rel="stylesheet";_3.href=_1[i].toString();_2.appendChild(_3);}}());dojo.declare("esri.dijit.Scalebar",null,{map:null,mapUnit:null,scalebarUnit:null,unitsDictionary:[],domNode:null,screenPt1:null,screenPt2:null,constructor:function(_4,_5){this.domNode=dojo.create("div",{innerHTML:"<div class='esriScalebarRuler'><div class='esriScalebarRulerBlock upper_firstpiece'></div><div class='esriScalebarRulerBlock upper_secondpiece'></div><div class='esriScalebarRulerBlock lower_firstpiece'></div><div class='esriScalebarRulerBlock lower_secondpiece'></div></div><div class='scaleLabelDiv'><div class='esriScalebarLabel' style='left: -3%'>0</div><div class='esriScalebarLabel esriScalebarFirstNumber'></div><div class='esriScalebarLabel esriScalebarSecondNumber'></div></div>"});_4=_4||{};if(!_4.map){console.error("scalebar: unable to find the 'map' property in parameters");return;}if(!_4.scalebarUnit){this.scalebarUnit="mi";}else{if(_4.scalebarUnit!=="english"&&_4.scalebarUnit!=="metric"){console.error("scalebar unit only accepts english or metric");return;}else{this.scalebarUnit=(_4.scalebarUnit==="english")?"mi":"km";}}this.map=_4.map;if(_5){_5.appendChild(this.domNode);}else{this.map.container.appendChild(this.domNode);if(_4.attachTo){dojo.addClass(this.domNode,"scalebar_"+_4.attachTo);}else{dojo.addClass(this.domNode,"scalebar_bottom-left");}}dojo.addClass(this.domNode,"esriScalebar");this._getDistance(this.map.extent);this._checkBingMaps();this._mapOnPan=dojo.connect(this.map,"onPan",this,this._getDistance);this._mapOnExtentChange=dojo.connect(this.map,"onExtentChange",this,this._getDistance);dojo.forEach(this.map.layerIds,function(_6,_7){if(this.map.getLayer(_6).declaredClass==="esri.virtualearth.VETiledLayer"){dojo.connect(this.map.getLayer(_6),"onVisibilityChange",dojo.hitch(this,function(_8){this._checkBingMaps();}));}},this);this._mapOnLayerAdd=dojo.connect(this.map,"onLayerAdd",dojo.hitch(this,function(_9){if(_9.declaredClass==="esri.virtualearth.VETiledLayer"){dojo.connect(_9,"onVisibilityChange",dojo.hitch(this,function(_a){this._checkBingMaps();}));}this._checkBingMaps();}));this._mapOnLayerRemove=dojo.connect(this.map,"onLayerRemove",dojo.hitch(this,this._checkBingMaps));},hide:function(){this._hidden=true;esri.hide(this.domNode);},show:function(){this._hidden=false;esri.show(this.domNode);},destroy:function(){dojo.disconnect(this._mapOnPan);dojo.disconnect(this._mapOnExtentChange);dojo.disconnect(this._mapOnLayerAdd);dojo.disconnect(this._mapOnLayerRemove);this.hide();this.map=null;dojo.destroy(this.domNode);},_checkBingMaps:function(){if(dojo.hasClass(this.domNode,"scalebar_bottom-left")){dojo.style(this.domNode,"left","25px");dojo.forEach(this.map.layerIds,function(_b,_c){if(this.map.getLayer(_b).declaredClass==="esri.virtualearth.VETiledLayer"&&this.map.getLayer(_b).visible){var _d="95px";if(this.map._mapParams.nav){_d="115px";}dojo.style(this.domNode,"left",_d);}},this);}},_getDistance:function(_e){var _f=dojo.position(this.domNode,true);var _10=_f.y-this.map.position.y;_10=(_10>this.map.height)?this.map.height:_10;_10=(_10<0)?0:_10;var _11=new esri.geometry.Point(0,_10,this.map.spatialReference);var _12=new esri.geometry.Point(this.map.width,_10,this.map.spatialReference);var _13,_14;this.mapUnit="esriDecimalDegrees";var pt1=esri.geometry.toMapPoint(_e,this.map.width,this.map.height,_11);var pt2=esri.geometry.toMapPoint(_e,this.map.width,this.map.height,_12);var _15=new esri.geometry.Point(_e.xmin,(_e.ymin+_e.ymax)/2,this.map.spatialReference);var _16=new esri.geometry.Point(_e.xmax,(_e.ymin+_e.ymax)/2,this.map.spatialReference);if(this.map.spatialReference.wkid===3857||this.map.spatialReference.wkid===102100||this.map.spatialReference.wkid===102113||(this.map.spatialReference.wkt&&this.map.spatialReference.wkt.indexOf("WGS_1984_Web_Mercator")!=-1)){pt1=esri.geometry.webMercatorToGeographic(pt1,true);pt2=esri.geometry.webMercatorToGeographic(pt2,true);_15=esri.geometry.webMercatorToGeographic(_15,true);_16=esri.geometry.webMercatorToGeographic(_16,true);}else{if(esri._isDefined(esri.WKIDUnitConversion[this.map.spatialReference.wkid])||(this.map.spatialReference.wkt&&this.map.spatialReference.wkt.indexOf("PROJCS")===0)){this.mapUnit="linearUnit";_13=Math.abs(_e.xmax-_e.xmin);var _17;if(esri._isDefined(esri.WKIDUnitConversion[this.map.spatialReference.wkid])){_17=esri.WKIDUnitConversion.values[esri.WKIDUnitConversion[this.map.spatialReference.wkid]];}else{var wkt=this.map.spatialReference.wkt;var _18=wkt.lastIndexOf(",")+1;var end=wkt.lastIndexOf("]]");_17=parseFloat(wkt.substring(_18,end));}_13*=_17;if(this.scalebarUnit==="mi"){_13/=1609;}else{_13/=1000;}}}if(this.mapUnit==="esriDecimalDegrees"){var _19=new esri.geometry.Polyline(new esri.SpatialReference({wkid:4326}));_19.addPath([pt1,pt2]);var _1a=esri.geometry._straightLineDensify(_19,10);_13=esri.geometry.geodesicLengths([_1a],esri.Units.KILOMETERS)[0];var _1b=new esri.geometry.Polyline(new esri.SpatialReference({wkid:4326}));_1b.addPath([_15,_16]);var _1c=esri.geometry._straightLineDensify(_1b,10);_14=esri.geometry.geodesicLengths([_1c],esri.Units.KILOMETERS)[0];if(this.scalebarUnit==="mi"){_13/=1.609;_14/=1.609;}}this._getScaleBarLength(_13);if(_14){if(_13/_14>0.1){if(!this._hidden){esri.show(this.domNode);}}else{esri.hide(this.domNode);}}},_getScaleBarLength:function(_1d){var _1e=50;var _1f=_1e*_1d/this.map.width;var _20=_1f;var i=0;var _21=this.scalebarUnit;if(_20<0.1){if(this.scalebarUnit==="mi"){_20*=5280;_21="ft";}else{if(this.scalebarUnit==="km"){_20*=1000;_21="m";}}}while(_20>=1){_20/=10;i++;}var _22,_23;if(_20>0.5){_22=1;_23=0.5;}else{if(_20>0.3){_22=0.5;_23=0.3;}else{if(_20>0.2){_22=0.3;_23=0.2;}else{if(_20>0.15){_22=0.2;_23=0.15;}else{if(_20>0.1){_22=0.15;_23=0.1;}}}}}var _24=((_22/_20)>=(_20/_23))?_23:_22;var _25=_24/_20;var _26=_1e*_25;var _27=Math.pow(10,i)*_24;this._drawScaleBar(_26,_27,_21);},_drawScaleBar:function(_28,_29,_2a){var _2b=2*Math.round(_28)+"px";this.domNode.style.width=_2b;dojo.forEach(dojo.query(".esriScalebarFirstNumber",this.domNode),function(_2c,idx){_2c.innerHTML=_29;},this);dojo.forEach(dojo.query(".esriScalebarSecondNumber",this.domNode),function(_2d,idx){if(this.mapUnit!=="esriUnknown"){_2d.innerHTML=2*_29+_2a;}else{_2d.innerHTML=2*_29+"Unknown Unit";}},this);}});}