/*
 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
require({cache:{"url:esri/dijit/templates/BasemapGallery.html":"<div class=\"esriBasemapGallery\">\r\n  <div dojoAttachPoint=\"flowContainer\">\r\n  </div>\r\n</div>"}});define("esri/dijit/BasemapGallery",["require","dojo/_base/declare","dojo/_base/array","dojo/_base/connect","dojo/_base/lang","dojo/_base/kernel","dojo/_base/sniff","dojo/has","dojo/query","dojo/DeferredList","dojo/dom","dojo/dom-construct","dojo/dom-class","dijit/_Widget","dijit/_Templated","esri/kernel","esri/urlUtils","esri/request","esri/geometry/Extent","esri/virtualearth/VETiledLayer","esri/layers/OpenStreetMapLayer","esri/layers/ArcGISTiledMapServiceLayer","esri/layers/ArcGISDynamicMapServiceLayer","esri/layers/ArcGISImageServiceLayer","esri/layers/ImageServiceParameters","esri/dijit/Basemap","esri/dijit/_EventedWidget","dojo/text!esri/dijit/templates/BasemapGallery.html"],function(_1,_2,_3,_4,_5,_6,_7,_8,_9,_a,_b,_c,_d,_e,_f,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_1a,_1b,_1c){var BMG=_2([_1b,_e,_f],{declaredClass:"esri.dijit.BasemapGallery",widgetsInTemplate:true,templateString:_1c,basePath:_1.toUrl("esri/dijit")+"/",loaded:false,basemaps:[],bingMapsKey:null,flowContainer:null,_hasUI:false,_selectedBasemap:null,_selectBasemapInProgress:false,_eventMap:{"load":true,"selection-change":true,"add":["basemap"],"remove":["basemap"],"error":["message"]},constructor:function(_1d,_1e){_1d=_1d||{};if(!_1d.map){console.error("esri.dijit.BasemapGallery: Unable to find the 'map' property in parameters");}this.map=_1d.map;this._hasUI=_1e?true:false;this.bingMapsKey=(_1d.bingMapsKey&&_1d.bingMapsKey.length>0)?_1d.bingMapsKey:null;this.showArcGISBasemaps=(_1d.showArcGISBasemaps===false)?false:true;this.basemaps=_1d.basemaps||[];this.basemapIds=_1d.basemapIds;this.referenceIds=_1d.referenceIds;this.basemapsGroup=_1d.basemapsGroup;if(location.protocol==="https:"){_10.dijit._arcgisUrl=_10.dijit._arcgisUrl.replace("http:","https:");}this.init();},init:function(){this.inherited(arguments);_3.forEach(this.basemaps,function(_1f,i){if(!_1f.id||_1f.id.length===0){_1f.id=this._getUniqueId();}_3.forEach(_1f.layers,function(_20){_20.opacity=(_20.opacity>=0)?_20.opacity:1;_20.visibility=true;},this);},this);if(this.basemapIds&&this.basemapIds.length>0){_3.forEach(this.basemapIds,function(_21){var _22=this.map.getLayer(_21);_22._basemapGalleryLayerType="basemap";},this);}if(this.referenceIds&&this.referenceIds.length>0){_3.forEach(this.referenceIds,function(_23){var _24=this.map.getLayer(_23);_24._basemapGalleryLayerType="reference";},this);}if(this.basemapsGroup&&((this.basemapsGroup.owner&&this.basemapsGroup.title)||this.basemapsGroup.id)){this._findCustomBasemapsGroup(_5.hitch(this,"_handleArcGISBasemapsResponse"));}else{if(this.showArcGISBasemaps){this._findArcGISBasemapsGroup(_5.hitch(this,"_handleArcGISBasemapsResponse"));}else{this._finishStartup();}}},startup:function(){if(this.loaded){this._refreshUI();}else{_4.connect(this,"onLoad",_5.hitch(this,function(){this._refreshUI();}));}},select:function(id){this._select(id);},getSelected:function(){return this._selectedBasemap;},get:function(id){var i;for(i=0;i<this.basemaps.length;i++){if(this.basemaps[i].id==id){return this.basemaps[i];}}return null;},add:function(_25){if(_25&&!_25.id){_25.id=this._getUniqueId();this.basemaps.push(_25);this._refreshUI();this.onAdd(_25);return true;}else{if(_25&&this._isUniqueId(_25.id)){this.basemaps.push(_25);this._refreshUI();this.onAdd(_25);return true;}}return false;},remove:function(id){var i;for(i=0;i<this.basemaps.length;i++){var _26=this.basemaps[i];if(_26.id===id){if(this._selectedBasemap&&this._selectedBasemap.id===_26.id){this._selectedBasemap=null;}this.basemaps.splice(i,1);this._refreshUI();this.onRemove(_26);return _26;}}return null;},onLoad:function(){},onSelectionChange:function(){},onAdd:function(_27){},onRemove:function(_28){},onError:function(msg){},_defaultBasemapGalleryGroupQuery:"title:\"ArcGIS Online Basemaps\" AND owner:esri",_basemapGalleryGroupQuery:null,_finishStartup:function(){this.loaded=true;this.onLoad();if(this.map.layerIds.length===0&&this.basemaps.length>0&&!this._selectBasemapInProgress){this._select(this.basemaps[0].id);}},_findCustomBasemapsGroup:function(_29){if(this.basemapsGroup&&this.basemapsGroup.id){this._findArcGISBasemaps(this.basemapsGroup.id,_29);}else{this._basemapGalleryGroupQuery="title:\""+this.basemapsGroup.title+"\" AND owner:"+this.basemapsGroup.owner;this._findArcGISBasemapsGroup(_29);}},_findArcGISBasemapsGroup:function(_2a){if(!this._basemapGalleryGroupQuery){var url=_10.dijit._arcgisUrl+"/accounts/self";var _2b={};_2b.f="json";_2b.culture=_6.locale;_12({url:url,content:_2b,callbackParamName:"callback",load:_5.hitch(this,function(_2c,_2d){if(_2c&&_2c.basemapGalleryGroupQuery){this._basemapGalleryGroupQuery=_2c.basemapGalleryGroupQuery;}else{this._basemapGalleryGroupQuery=this._defaultBasemapGalleryGroupQuery;}this._findArcGISBasemapsGroupContent(_2a);}),error:_5.hitch(this,function(_2e,_2f){this._basemapGalleryGroupQuery=this._defaultBasemapGalleryGroupQuery;})});}else{this._findArcGISBasemapsGroupContent(_2a);}},_findArcGISBasemapsGroupContent:function(_30){var _31=_5.hitch(this,"_findArcGISBasemaps");var url=_10.dijit._arcgisUrl+"/community/groups";var _32={};_32.q=this._basemapGalleryGroupQuery;_32.f="json";_12({url:url,content:_32,callbackParamName:"callback",load:_5.hitch(this,function(_33,_34){if(_33.results.length>0){_31(_33.results[0].id,_30);}else{var msg="esri.dijit.BasemapGallery: could not find group for basemaps.";this.onError(msg);}}),error:_5.hitch(this,function(_35){var msg="esri.dijit.BasemapGallery: could not find group for basemaps.";this.onError(msg);})});},_findArcGISBasemaps:function(_36,_37){var url=_10.dijit._arcgisUrl+"/search";var _38={};_38.q="group:"+_36+" AND type:\"web map\"";_38.sortField="name";_38.sortOrder="desc";_38.num=50;_38.f="json";_12({url:url,content:_38,callbackParamName:"callback",load:_5.hitch(this,function(_39,_3a){if(_39.results.length>0){_37(_39.results);}else{var msg="esri.dijit.BasemapGallery: could not find group for basemaps.";this.onError(msg);}}),error:_5.hitch(this,function(_3b,_3c){var msg="esri.dijit.BasemapGallery: could not find group for basemaps.";this.onError(msg);})});},_handleArcGISBasemapsResponse:function(_3d){if(_3d.length>0){_3.forEach(_3d,function(_3e,i){if(this.bingMapsKey||(!this.bingMapsKey&&_3e.title&&_3e.title.indexOf("Bing Maps")==-1)){var _3f={};_3f.id=this._getUniqueId();_3f.title=_3e.title;_3f.thumbnailUrl="";if(_3e.thumbnail&&_3e.thumbnail.length){_3f.thumbnailUrl=(_10.dijit._arcgisUrl+"/content/items/"+_3e.id+"/info/"+_3e.thumbnail);if(_10.id){var _40=_10.id.findCredential(_11.urlToObject(_10.dijit._arcgisUrl).path);if(_40){_3f.thumbnailUrl+="?token="+_40.token;}}}_3f.itemId=_3e.id;var _41=new _1a(_3f,this);this.basemaps.splice(0,0,_41);}},this);this._finishStartup();}},_refreshUI:function(){if(this._hasUI){_c.empty(this.flowContainer);_3.forEach(this.basemaps,function(_42,i){if(!_42.id){_42.id="basemap_"+i;}this.flowContainer.appendChild(this._buildNodeLayout(_42));},this);_c.create("br",{style:{clear:"both"}},this.flowContainer);this._markSelected(this._selectedBasemap);}},_buildNodeLayout:function(_43){var nId="galleryNode_"+_43.id;var n=_c.create("div",{id:nId,"class":"esriBasemapGalleryNode"});var _44=_c.create("a",{href:"javascript:void(0);"},n);_4.connect(_44,"onclick",_5.hitch(this,"_onNodeClick",_43));if(_43.thumbnailUrl){_c.create("img",{"class":"esriBasemapGalleryThumbnail",src:_43.thumbnailUrl},_44);}else{_c.create("img",{"class":"esriBasemapGalleryThumbnail",src:this.basePath.toString()+"images/transparent.gif"},_44);}var _45=_c.create("div",{"class":"esriBasemapGalleryLabelContainer"},n);var _46=_43.title||"";_c.create("span",{innerHTML:_46,alt:_46,title:_46},_45);return n;},_onNodeClick:function(_47,e){e.preventDefault();this._markSelected(_47);this.select(_47.id);},_markSelected:function(_48){if(_48){_3.forEach(_6.query(".esriBasemapGallerySelectedNode",this.domNode),function(_49){_d.remove(_49,"esriBasemapGallerySelectedNode");});var _4a=_b.byId("galleryNode_"+_48.id);if(_4a){_d.add(_4a,"esriBasemapGallerySelectedNode");}}},_select:function(id){this._selectBasemapInProgress=true;var _4b=this.get(id);if(_4b){if(_4b.layers){this._getServiceInfos(_4b);}else{var _4c=_4b.getLayers();if(_5.isArray(_4c)){this._getServiceInfos(_4b);}else{_4c.addCallback(_5.hitch(this,function(_4d){this._getServiceInfos(_4b);}));}}this._markSelected(_4b);}else{this._selectBasemapInProgress=false;}},_getServiceInfos:function(_4e){if(location.protocol=="https:"){_3.forEach(_4e.layers,function(_4f){if(this._isAgolService(_4f.url)||this._isHostedService(_4f.url)){_4f.url=_4f.url.replace("http:","https:");}},this);}this._selectedBasemap=_4e;var _50=[];_3.forEach(_4e.layers,function(_51){if(_51.url&&_51.url.length>0&&!_51.isReference){_51.deferredsPos=_50.length;_50.push(this._getServiceInfo(_51.url));}},this);if(_50.length>0){var _52=new _a(_50);_52.addCallback(_5.hitch(this,function(_53){var _54=null;_3.forEach(_4e.layers,function(_55){if(_55.deferredsPos===0||_55.deferredsPos){_55.serviceInfoResponse=_53[_55.deferredsPos][1];var ext=_55.serviceInfoResponse.fullExtent;if(!ext){ext=_55.serviceInfoResponse.extent;}if(!_54){_54=new _13(ext);}else{_54=_54.union(new _13(ext));}}},this);if(this.map.extent){var _56=this._getIntersectionPercent(_54,this.map.extent);if(_56<5){this.map.setExtent(_54,true);}}this._switchBasemapLayers(_4e);this._updateReferenceLayer(_4e);}));}else{this._switchBasemapLayers(_4e);this._updateReferenceLayer(_4e);}},_switchBasemapLayers:function(_57){var _58=_57.layers;if(this.map.layerIds.length>0&&this.map.getNumLevels()===0&&(_58[0].type==="OpenStreetMap"||(_58[0].type&&_58[0].type.indexOf("BingMaps")>-1))){var msg="esri.dijit.BasemapGallery: Unable to switch basemap because new basemap is a tiled service and cannot be loaded as a dynamic layer.";this.onError(msg);return;}_3.forEach(_58,function(_59){if(!_59.isReference&&_59.type&&_59.type.indexOf("BingMaps")>-1&&!this.bingMapsKey){var msg="esri.dijit.BasemapGallery: Invalid Bing Maps key.";this.onError(msg);return;}},this);this._removeBasemapLayers();_3.forEach(_58,function(_5a){if(!_5a.isReference){var _5b,msg;if(_5a.type==="OpenStreetMap"){if(this.map.layerIds.length>0&&this.map.getNumLevels()===0){msg="esri.dijit.BasemapGallery: Unable to switch basemap because new basemap is a tiled service and cannot be loaded as a dynamic layer.";this.onError(msg);return;}_5b=new _15({id:"layer_osm",opacity:_5a.opacity});}else{if(_5a.type&&_5a.type.indexOf("BingMaps")>-1){if(this.map.layerIds.length>0&&this.map.getNumLevels()===0){msg="esri.dijit.BasemapGallery: Unable to switch basemap because new basemap is a tiled service and cannot be loaded as a dynamic layer.";this.onError(msg);return;}var _5c=_14.MAP_STYLE_AERIAL_WITH_LABELS;if(_5a.type=="BingMapsAerial"){_5c=_14.MAP_STYLE_AERIAL;}else{if(_5a.type=="BingMapsRoad"){_5c=_14.MAP_STYLE_ROAD;}}_5b=new _14({id:"layer_bing",bingMapsKey:this.bingMapsKey,mapStyle:_5c,opacity:_5a.opacity});}else{if(_5a.serviceInfoResponse&&_5a.serviceInfoResponse.mapName){if((this.map.layerIds.length===0||this.map.getNumLevels()>0)&&_5a.serviceInfoResponse.singleFusedMapCache===true){_5b=this._loadAsCached(_5a);}else{_5b=this._loadAsDynamic(_5a);}}else{if(_5a.serviceInfoResponse&&_5a.serviceInfoResponse.pixelSizeX){var _5d=new _19();_5d.bandIds=_5a.bandIds;if(!_5a.bandIds&&_5a.serviceInfoResponse.bandCount&&parseInt(_5a.serviceInfoResponse.bandCount)>3){_5d.bandIds=[0,1,2];}_5b=new _18(_5a.url,{resourceInfo:_5a.serviceInfoResponse,opacity:_5a.opacity,visible:_5a.visibility,imageServiceParameters:_5d});}}}}if(_5b){_5b._basemapGalleryLayerType="basemap";this.map.addLayer(_5b,0);}}},this);this._selectBasemapInProgress=false;this.onSelectionChange();},_removeBasemapLayers:function(){var _5e=this.map.layerIds;var _5f=[];_3.forEach(_5e,function(id){var _60=this.map.getLayer(id);if(_60._basemapGalleryLayerType==="basemap"){_5f.push(_60);}},this);if(_5f.length===0&&_5e.length>0){_5f.push(this.map.getLayer(_5e[0]));}if(_5f.length>0){_3.forEach(_5f,function(_61){this.map.removeLayer(_61);},this);}},_updateReferenceLayer:function(_62){var i;this._removeReferenceLayer();for(i=0;i<_62.layers.length;i++){if(_62.layers[i].isReference===true){this._addReferenceLayer(_62.layers[i]);}}},_removeReferenceLayer:function(){var i;for(i=this.map.layerIds.length-1;i>=0;i--){var id=this.map.layerIds[i];var _63=this.map.getLayer(id);if(_63._basemapGalleryLayerType==="reference"){this.map.removeLayer(_63);}}},_addReferenceLayer:function(_64){this._getServiceInfo(_64.url,_5.hitch(this,"_handleReferenceServiceInfoResponse",_64));},_handleReferenceServiceInfoResponse:function(_65,_66,_67){var _68;_65.serviceInfoResponse=_66;if(_66&&_66.mapName){if(_66.singleFusedMapCache===true){_68=this._loadAsCached(_65);}else{_68=this._loadAsDynamic(_65);}}else{if(_66&&_66.pixelSizeX){var _69=new _19();_69.bandIds=_65.bandIds;if(!_65.bandIds&&_66.bandCount&&parseInt(_66.bandCount)>3){_69.bandIds=[0,1,2];}_68=new _18(_65.url,{resourceInfo:_66,opacity:_65.opacity,visible:_65.visibility,imageServiceParameters:_69});}}if(_68){_68._basemapGalleryLayerType="reference";this.map.addLayer(_68);}},_getServiceInfo:function(url,_6a){var _6b={};_6b.f="json";var _6c=_12({url:url,content:_6b,callbackParamName:"callback",load:function(_6d,_6e){if(_6a){_6a(_6d,_6e);}},error:_5.hitch(this,function(_6f,_70){var msg="esri.dijit.BasemapGallery: service not accessible.";this.onError(msg);})});return _6c;},_loadAsCached:function(_71){var _72=[];if(!_71.displayLevels){_72=_3.map(_71.serviceInfoResponse.tileInfo.lods,function(lod){return lod.level;});}var _73=new _16(_71.url,{resourceInfo:_71.serviceInfoResponse,opacity:_71.opacity,visible:_71.visibility,displayLevels:_71.displayLevels||_72});return _73;},_loadAsDynamic:function(_74){var _75=new _17(_74.url,{resourceInfo:_74.serviceInfoResponse,opacity:_74.opacity,visible:_74.visibility});if(_74.visibleLayers){_75.setVisibleLayers(_74.visibleLayers);}return _75;},_getIntersectionPercent:function(_76,_77){var _78=_77.intersects(_76);if(_78){var _79=_78.getWidth()*_78.getHeight();var _7a=_77.getWidth()*_77.getHeight();return (_79/_7a)*100;}else{return 0;}},_getIds:function(){var ids=[];_3.forEach(this.basemaps,function(_7b){ids.push(_7b.id);},this);return ids;},_getUniqueId:function(){var _7c=","+this._getIds().toString()+",";var _7d=0;while(true){if(_7c.indexOf(",basemap_"+_7d+",")>-1){_7d++;}else{return "basemap_"+_7d;}}},_isUniqueId:function(id){var _7e=","+this._getIds().toString()+",";if(_7e.indexOf(","+id+",")===-1){return true;}return false;},_isAgolService:function(url){if(!url){return false;}return (url.indexOf("/services.arcgisonline.com/")!==-1||url.indexOf("/server.arcgisonline.com/")!==-1);},_isHostedService:function(url){if(!url){return false;}return (url.indexOf(".arcgis.com/")!==-1);}});if(_8("extend-esri")){_5.setObject("dijit.BasemapGallery",BMG,_10);}return BMG;});