/*
 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/virtualearth/VETiledLayer",["dojo/_base/declare","dojo/_base/lang","dojo/_base/json","dojo/_base/array","dojo/_base/config","dojo/has","dojo/string","dojo/_base/Deferred","esri/kernel","esri/urlUtils","esri/SpatialReference","esri/layers/TileInfo","esri/layers/TiledMapServiceLayer","esri/geometry/Extent","esri/request"],function(_1,_2,_3,_4,_5,_6,_7,_8,_9,_a,_b,_c,_d,_e,_f){var VET=_1(_d,{declaredClass:"esri.virtualearth.VETiledLayer",constructor:function(_10){try{_10=_2.mixin({bingMapsKey:null,culture:"en-US"},_10||{});var _11=window.location.protocol;if(_11==="file:"){_11="http:";}this.url=_11+"//dev.virtualearth.net/REST/v1";this._url=_a.urlToObject(this.url);this.spatialReference=new _b({wkid:102100});this.tileInfo=new _c({rows:256,cols:256,dpi:96,origin:{x:-20037508.342787,y:20037508.342787},spatialReference:{wkid:102100},lods:[{level:1,resolution:78271.5169639999,scale:295828763.795777},{level:2,resolution:39135.7584820001,scale:147914381.897889},{level:3,resolution:19567.8792409999,scale:73957190.948944},{level:4,resolution:9783.93962049996,scale:36978595.474472},{level:5,resolution:4891.96981024998,scale:18489297.737236},{level:6,resolution:2445.98490512499,scale:9244648.868618},{level:7,resolution:1222.99245256249,scale:4622324.434309},{level:8,resolution:611.49622628138,scale:2311162.217155},{level:9,resolution:305.748113140558,scale:1155581.108577},{level:10,resolution:152.874056570411,scale:577790.554289},{level:11,resolution:76.4370282850732,scale:288895.277144},{level:12,resolution:38.2185141425366,scale:144447.638572},{level:13,resolution:19.1092570712683,scale:72223.819286},{level:14,resolution:9.55462853563415,scale:36111.909643},{level:15,resolution:4.77731426794937,scale:18055.954822},{level:16,resolution:2.38865713397468,scale:9027.977411},{level:17,resolution:1.19432856685505,scale:4513.988705},{level:18,resolution:0.597164283559817,scale:2256.994353},{level:19,resolution:0.298582141647617,scale:1128.497176}]});this.initialExtent=(this.fullExtent=new _e(-20037508.342787,-20037508.34278,20037508.34278,20037508.342787,new _b({wkid:102100})));_2.mixin(this,_10);this.hasAttributionData=this.showAttribution;this._initLayer=_2.hitch(this,this._initLayer);this._errorHandler=_2.hitch(this,this._errorHandler);this._getTileInfo=_2.hitch(this,this._getTileInfo);if(this.bingMapsKey){this._getTileInfo();}else{throw new Error("BingMapsKey must be provided.");}}catch(e){this.onError(e);throw e;}},_unsetMap:function(map,_12){this.inherited("_unsetMap",arguments);},_getTileInfo:function(){if(!this.mapStyle){return;}var url=this._url.path+"/Imagery/Metadata/"+this.mapStyle;if(this.bingMapsKey){var _13=this.resourceInfo;if(!this.loaded&&_13){this._initLayer(_13);}else{_f({url:url,content:_2.mixin({},{key:this.bingMapsKey,ss:true,c:this.culture,include:this.hasAttributionData?"imageryProviders":null}),callbackParamName:"jsonp",load:this._initLayer,error:this._errorHandler});}}},_initLayer:function(_14,io){try{this.resourceInfo=_3.toJson(_14);var _15=_14.resourceSets[0].resources[0];var _16=_15.imageUrl.replace("{","${");this.tileServers=_4.map(_15.imageUrlSubdomains,function(_17){var _18=window.location.protocol;if(_18==="file:"){_18="http:";}return _7.substitute(_16,{subdomain:_17}).replace("http:",_18);});this._tsLength=this.tileServers.length;if(!this.loaded){this.copyright=this.copyright||"&copy; 2012 Microsoft Corporation and its data suppliers";this.loaded=true;this.onLoad(this);var _19=this.loadCallback;if(_19){delete this.loadCallback;_19(this);}}else{this.refresh();this.onMapStyleChange();}}catch(e){this.onError(e);}},getAttributionData:function(){var dfd=new _8(),_1a=_3.fromJson(this.resourceInfo),_1b;if(this.hasAttributionData&&_1a){_1b=_2.getObject("resourceSets.0.resources.0.imageryProviders",false,_1a);}if(_1b){dfd.callback({contributors:_1b});}else{var err=new Error("Layer does not have attribution data");err.log=_5.isDebug;dfd.errback(err);}return dfd;},getTileUrl:function(_1c,row,col){var _1d=this.tileServers[row%this._tsLength],_1e=_1d.replace(/\{/g,"${");return _7.substitute(_1e,{quadkey:this._getQuadKey(_1c,row,col),culture:this.culture,token:this.bingMapsKey});},_getQuadKey:function(_1f,row,col){var _20="",_21,_22,i;for(i=_1f;i>0;i--){_21="0";_22=1<<(i-1);if((col&_22)!=0){_21++;}if((row&_22)!=0){_21++;_21++;}_20=_20+_21;}return _20;},setMapStyle:function(_23){this.mapStyle=_23;this._getTileInfo();},setCulture:function(_24){this.culture=_24;this._getTileInfo();},setBingMapsKey:function(_25){this.bingMapsKey=_25;},onMapStyleChange:function(){}});_2.mixin(VET,{MAP_STYLE_AERIAL:"aerial",MAP_STYLE_AERIAL_WITH_LABELS:"aerialWithLabels",MAP_STYLE_ROAD:"road"});if(_6("extend-esri")){_2.setObject("virtualearth.VETiledLayer",VET,_9);}return VET;});