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

window[esri._dojoScopeName||"dojo"]._xdResourceLoaded(function(_1,_2,_3){return {depends:[["provide","esri.dijit.BasemapGallery"],["require","esri.virtualearth.VETiledLayer"],["require","esri.layers.osm"],["require","dijit._Widget"],["require","dijit._Templated"],["require","dojo.DeferredList"]],defineResource:function(_4,_5,_6){if(!_4._hasResource["esri.dijit.BasemapGallery"]){_4._hasResource["esri.dijit.BasemapGallery"]=true;_4.provide("esri.dijit.BasemapGallery");_4.require("esri.virtualearth.VETiledLayer");_4.require("esri.layers.osm");_4.require("dijit._Widget");_4.require("dijit._Templated");_4.require("dojo.DeferredList");(function(){var _7=[_4.moduleUrl("esri.dijit","css/BasemapGallery.css")];var _8=document.getElementsByTagName("head").item(0),_9;for(var i=0,il=_7.length;i<il;i++){_9=document.createElement("link");_9.type="text/css";_9.rel="stylesheet";_9.href=_7[i].toString();_8.appendChild(_9);}}());esri.dijit._arcgisUrl="http://www.arcgis.com/sharing";_4.declare("esri.dijit.BasemapGallery",[_5._Widget,_5._Templated],{widgetsInTemplate:true,templateString:"<div class=\"esriBasemapGallery\">\r\n  <div dojoAttachPoint=\"flowContainer\">\r\n  </div>\r\n</div>\r\n",basePath:_4.moduleUrl("esri.dijit"),loaded:false,basemaps:[],bingMapsKey:null,flowContainer:null,_hasUI:false,_selectedBasemap:null,constructor:function(_a,_b){_a=_a||{};if(!_a.map){console.error("esri.dijit.BasemapGallery: Unable to find the 'map' property in parameters");}this.map=_a.map;this._hasUI=_b?true:false;this.bingMapsKey=(_a.bingMapsKey&&_a.bingMapsKey.length>0)?_a.bingMapsKey:null;this.showArcGISBasemaps=(_a.showArcGISBasemaps===false)?false:true;this.basemaps=_a.basemaps?_a.basemaps:[];this.basemapIds=_a.basemapIds;this.referenceIds=_a.referenceIds;this.basemapsGroup=_a.basemapsGroup;this.init();},init:function(){this.inherited(arguments);_4.forEach(this.basemaps,function(_c,i){if(!_c.id||_c.id.length===0){_c.id=this._getUniqueId();}_4.forEach(_c.layers,function(_d){_d.opacity=(_d.opacity>=0)?_d.opacity:1;_d.visibility=true;},this);},this);if(this.basemapIds&&this.basemapIds.length>0){_4.forEach(this.basemapIds,function(_e){var _f=this.map.getLayer(_e);_f._basemapGalleryLayerType="basemap";},this);}if(this.referenceIds&&this.referenceIds.length>0){_4.forEach(this.referenceIds,function(_10){var _11=this.map.getLayer(_10);_11._basemapGalleryLayerType="reference";},this);}if(this.basemapsGroup&&((this.basemapsGroup.owner&&this.basemapsGroup.title)||this.basemapsGroup.id)){this._findCustomBasemapsGroup(_4.hitch(this,"_handleArcGISBasemapsResponse"));}else{if(this.showArcGISBasemaps){this._findArcGISBasemapsGroup(_4.hitch(this,"_handleArcGISBasemapsResponse"));}else{this._finishStartup();}}},startup:function(){if(this.loaded){this._refreshUI();}else{_4.connect(this,"onLoad",_4.hitch(this,function(){this._refreshUI();}));}},select:function(id){this._select(id);},getSelected:function(){return this._selectedBasemap;},get:function(id){for(var i=0;i<this.basemaps.length;i++){if(this.basemaps[i].id==id){return this.basemaps[i];}}return null;},add:function(_12){if(_12&&!_12.id){_12.id=this._getUniqueId();this.basemaps.push(_12);this._refreshUI();this.onAdd(_12);return true;}else{if(_12&&this._isUniqueId(_12.id)){this.basemaps.push(_12);this._refreshUI();this.onAdd(_12);return true;}}return false;},remove:function(id){for(var i=0;i<this.basemaps.length;i++){var _13=this.basemaps[i];if(_13.id===id){if(this._selectedBasemap&&this._selectedBasemap.id===_13.id){this._selectedBasemap=null;}this.basemaps.splice(i,1);this._refreshUI();this.onRemove(_13);return _13;}}return null;},onLoad:function(){},onSelectionChange:function(){},onAdd:function(_14){},onRemove:function(_15){},onError:function(msg){},_basemapGalleryGroupQuery:"title:\"ArcGIS Online Basemaps\" AND owner:esri",_finishStartup:function(){this.loaded=true;this.onLoad();if(this.map.layerIds.length===0&&this.basemaps.length>0){this._select(this.basemaps[0].id);}},_findCustomBasemapsGroup:function(_16){if(this.basemapsGroup&&this.basemapsGroup.id){this._findArcGISBasemaps(this.basemapsGroup.id,_16);}else{this._basemapGalleryGroupQuery="title:\""+this.basemapsGroup.title+"\" AND owner:"+this.basemapsGroup.owner;this._findArcGISBasemapsGroup(_16);}},_findArcGISBasemapsGroup:function(_17){var _18=_4.hitch(this,"_findArcGISBasemaps");var url=esri.dijit._arcgisUrl+"/community/groups";var _19={};_19.q=this._basemapGalleryGroupQuery;_19.f="json";esri.request({url:url,content:_19,callbackParamName:"callback",load:function(_1a,_1b){if(_1a.results.length>0){_18(_1a.results[0].id,_17);}else{console.error("esri.dijit.BasemapGallery: could not find group for basemaps.");}},error:esriConfig.defaults.io.errorHandler});},_findArcGISBasemaps:function(_1c,_1d){var url=esri.dijit._arcgisUrl+"/search";var _1e={};_1e.q="group:"+_1c+" AND type:\"web map\"";_1e.sortField="name";_1e.sortOrder="desc";_1e.num=50;_1e.f="json";esri.request({url:url,content:_1e,callbackParamName:"callback",load:function(_1f,_20){_1d(_1f.results);},error:esriConfig.defaults.io.errorHandler});},_handleArcGISBasemapsResponse:function(_21){if(_21.length>0){_4.forEach(_21,function(_22,i){if(this.bingMapsKey||(!this.bingMapsKey&&_22.title&&_22.title.indexOf("Bing Maps")==-1)){var _23={};_23.id=this._getUniqueId();_23.title=_22.title;_23.thumbnailUrl=(_22.thumbnail&&_22.thumbnail.length>0)?(esri.dijit._arcgisUrl+"/content/items/"+_22.id+"/info/"+_22.thumbnail):"";_23.itemId=_22.id;var _24=new esri.dijit.Basemap(_23);this.basemaps.splice(0,0,_24);}},this);this._finishStartup();}},_refreshUI:function(){if(this._hasUI){_4.empty(this.flowContainer);_4.forEach(this.basemaps,function(_25,i){if(!_25.id){_25.id="basemap_"+i;}this.flowContainer.appendChild(this._buildNodeLayout(_25));},this);_4.create("br",{style:{clear:"both"}},this.flowContainer);this._markSelected(this._selectedBasemap);}},_buildNodeLayout:function(_26){var nId="galleryNode_"+_26.id;var n=_4.create("div",{id:nId,"class":"esriBasemapGalleryNode"});var _27=_4.create("a",{href:"#"},n);_4.connect(_27,"onclick",_4.hitch(this,"_onNodeClick",_26));if(_26.thumbnailUrl){_4.create("img",{"class":"esriBasemapGalleryThumbnail",src:_26.thumbnailUrl},_27);}else{_4.create("img",{"class":"esriBasemapGalleryThumbnail",src:this.basePath.toString()+"images/transparent.gif"},_27);}var _28=_4.create("div",{"class":"esriBasemapGalleryLabelContainer"},n);var _29=_26.title||"";_4.create("span",{innerHTML:_29,alt:_29,title:_29},_28);return n;},_onNodeClick:function(_2a,e){e.preventDefault();this._markSelected(_2a);this.select(_2a.id);},_markSelected:function(_2b){if(_2b){_4.forEach(_4.query(".esriBasemapGallerySelectedNode",this.domNode),function(_2c){_4.removeClass(_2c,"esriBasemapGallerySelectedNode");});_4.addClass(_4.byId("galleryNode_"+_2b.id),"esriBasemapGallerySelectedNode");}},_select:function(id){var _2d=this.get(id);if(_2d.layers){this._getServiceInfos(_2d);}else{var _2e=_2d.getLayers();if(_4.isArray(_2e)){this._getServiceInfos(_2d);}else{_2e.addCallback(_4.hitch(this,function(_2f){this._getServiceInfos(_2d);}));}}},_getServiceInfos:function(_30){this._selectedBasemap=_30;var _31=[];_4.forEach(_30.layers,function(_32){if(_32.url&&_32.url.length>0&&!_32.isReference){_32.deferredsPos=_31.length;_31.push(this._getServiceInfo(_32.url));}},this);if(_31.length>0){var _33=new _4.DeferredList(_31);_33.addCallback(_4.hitch(this,function(_34){var _35=null;_4.forEach(_30.layers,function(_36){if(_36.deferredsPos===0||_36.deferredsPos){_36.serviceInfoResponse=_31[_36.deferredsPos].ioArgs.json;var ext=_36.serviceInfoResponse.fullExtent;if(!ext){ext=_36.serviceInfoResponse.extent;}if(!_35){_35=new esri.geometry.Extent(ext);}else{_35=_35.union(new esri.geometry.Extent(ext));}}},this);if(this.map.extent){var _37=this._getIntersectionPercent(_35,this.map.extent);if(_37<5){this.map.setExtent(_35,true);}}this._switchBasemapLayers(_30);this._updateReferenceLayer(_30);}));}else{this._switchBasemapLayers(_30);this._updateReferenceLayer(_30);}},_switchBasemapLayers:function(_38){var _39=_38.layers;if(this.map.layerIds.length>0&&this.map.getNumLevels()===0&&(_39[0].type==="OpenStreetMap"||(_39[0].type&&_39[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;}_4.forEach(_39,function(_3a){if(!_3a.isReference&&_3a.type&&_3a.type.indexOf("BingMaps")>-1&&!this.bingMapsKey){var msg="esri.dijit.BasemapGallery: Invalid Bing Maps key.";this.onError(msg);return;}},this);this._removeBasemapLayers();_4.forEach(_39,function(_3b){if(!_3b.isReference){var _3c;if(_3b.type==="OpenStreetMap"){if(this.map.layerIds.length>0&&this.map.getNumLevels()===0){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;}_3c=new esri.layers.OpenStreetMapLayer({id:"layer_osm",opacity:_3b.opacity});}else{if(_3b.type&&_3b.type.indexOf("BingMaps")>-1){if(this.map.layerIds.length>0&&this.map.getNumLevels()===0){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;}var _3d=esri.virtualearth.VETiledLayer.MAP_STYLE_AERIAL_WITH_LABELS;if(_3b.type=="BingMapsAerial"){_3d=esri.virtualearth.VETiledLayer.MAP_STYLE_AERIAL;}else{if(_3b.type=="BingMapsRoad"){_3d=esri.virtualearth.VETiledLayer.MAP_STYLE_ROAD;}}_3c=new esri.virtualearth.VETiledLayer({id:"layer_bing",bingMapsKey:this.bingMapsKey,mapStyle:_3d,opacity:_3b.opacity});}else{if(_3b.serviceInfoResponse&&_3b.serviceInfoResponse.mapName){if((this.map.layerIds.length===0||this.map.getNumLevels()>0)&&_3b.serviceInfoResponse.singleFusedMapCache===true){_3c=this._loadAsCached(_3b);}else{_3c=this._loadAsDynamic(_3b);}}else{if(_3b.serviceInfoResponse&&_3b.serviceInfoResponse.pixelSizeX){var _3e=new esri.layers.ImageServiceParameters();_3e.bandIds=_3b.bandIds;if(!_3b.bandIds&&_3b.serviceInfoResponse.bandCount&&parseInt(_3b.serviceInfoResponse.bandCount)>3){_3e.bandIds=[0,1,2];}_3c=new esri.layers.ArcGISImageServiceLayer(_3b.url,{resourceInfo:_3b.serviceInfoResponse,opacity:_3b.opacity,visible:_3b.visibility,imageServiceParameters:_3e});}}}}if(_3c){_3c._basemapGalleryLayerType="basemap";this.map.addLayer(_3c,0);}}},this);this.onSelectionChange();},_removeBasemapLayers:function(){var _3f=this.map.layerIds;var _40=[];_4.forEach(_3f,function(id){var _41=this.map.getLayer(id);if(_41._basemapGalleryLayerType==="basemap"){_40.push(_41);}},this);if(_40.length===0&&_3f.length>0){_40.push(this.map.getLayer(_3f[0]));}if(_40.length>0){_4.forEach(_40,function(_42){this.map.removeLayer(_42);},this);}},_updateReferenceLayer:function(_43){this._removeReferenceLayer();for(var i=0;i<_43.layers.length;i++){if(_43.layers[i].isReference===true){this._addReferenceLayer(_43.layers[i]);}}},_removeReferenceLayer:function(){for(var i=this.map.layerIds.length-1;i>=0;i--){var id=this.map.layerIds[i];var _44=this.map.getLayer(id);if(_44._basemapGalleryLayerType==="reference"){this.map.removeLayer(_44);}}},_addReferenceLayer:function(_45){this._getServiceInfo(_45.url,_4.hitch(this,"_handleReferenceServiceInfoResponse",_45));},_handleReferenceServiceInfoResponse:function(_46,_47,_48){var _49;_46.serviceInfoResponse=_47;if(_47&&_47.mapName){if(_47.singleFusedMapCache===true){_49=this._loadAsCached(_46);}else{_49=this._loadAsDynamic(_46);}}else{if(_47&&_47.pixelSizeX){var _4a=new esri.layers.ImageServiceParameters();_4a.bandIds=_46.bandIds;if(!_46.bandIds&&_47.bandCount&&parseInt(_47.bandCount)>3){_4a.bandIds=[0,1,2];}_49=new esri.layers.ArcGISImageServiceLayer(_46.url,{resourceInfo:_47,opacity:_46.opacity,visible:_46.visibility,imageServiceParameters:_4a});}}if(_49){_49._basemapGalleryLayerType="reference";this.map.addLayer(_49);}},_getServiceInfo:function(url,_4b){var _4c={};_4c.f="json";var _4d=esri.request({url:url,content:_4c,callbackParamName:"callback",load:function(_4e,_4f){if(_4b){_4b(_4e,_4f);}},error:esriConfig.defaults.io.errorHandler});return _4d;},_loadAsCached:function(_50){var _51=[];if(!_50.displayLevels){_51=_4.map(_50.serviceInfoResponse.tileInfo.lods,function(lod){return lod.level;});}var _52=new esri.layers.ArcGISTiledMapServiceLayer(_50.url,{resourceInfo:_50.serviceInfoResponse,opacity:_50.opacity,visible:_50.visibility,displayLevels:(_50.displayLevels)?_50.displayLevels:_51});return _52;},_loadAsDynamic:function(_53){var _54=new esri.layers.ArcGISDynamicMapServiceLayer(_53.url,{resourceInfo:_53.serviceInfoResponse,opacity:_53.opacity,visible:_53.visibility});if(_53.visibleLayers){_54.setVisibleLayers(_53.visibleLayers);}return _54;},_getIntersectionPercent:function(_55,_56){var _57=_56.intersects(_55);if(_57){var _58=_57.getWidth()*_57.getHeight();var _59=_56.getWidth()*_56.getHeight();return (_58/_59)*100;}else{return 0;}},_getIds:function(){var ids=[];_4.forEach(this.basemaps,function(_5a){ids.push(_5a.id);},this);return ids;},_getUniqueId:function(){var _5b=","+this._getIds().toString()+",";var _5c=0;while(true){if(_5b.indexOf(",basemap_"+_5c+",")>-1){_5c++;}else{return "basemap_"+_5c;}}},_isUniqueId:function(id){var _5d=","+this._getIds().toString()+",";if(_5d.indexOf(","+id+",")===-1){return true;}return false;}});_4.declare("esri.dijit.Basemap",null,{id:null,title:"",thumbnailUrl:null,layers:null,itemId:null,constructor:function(_5e){_5e=_5e||{};if(!_5e.layers&&!_5e.itemId){console.error("esri.dijit.Basemap: unable to find the 'layers' property in parameters");}this.id=_5e.id;this.itemId=_5e.itemId;this.layers=_5e.layers;this.title=_5e.title?_5e.title:"";this.thumbnailUrl=_5e.thumbnailUrl;},getLayers:function(){if(this.layers){return this.layers;}else{if(this.itemId){var url=esri.dijit._arcgisUrl+"/content/items/"+this.itemId+"/data";var _5f={};_5f.f="json";var _60=esri.request({url:url,content:_5f,callbackParamName:"callback",error:esriConfig.defaults.io.errorHandler});_60.addCallback(_4.hitch(this,function(_61,_62){this.layers=[];_4.forEach(_61.baseMap.baseMapLayers,function(_63){var _64={};if(_63.url){_64.url=_63.url;}if(_63.type){_64.type=_63.type;}if(_63.isReference){_64.isReference=_63.isReference;}if(_63.displayLevels){_64.displayLevels=_63.displayLevels;}if(_63.visibleLayers){_64.visibleLayers=_63.visibleLayers;}if(_63.bandIds){_64.bandIds=_63.bandIds;}this.layers.push(new esri.dijit.BasemapLayer(_64));},this);return this.layers;}));return _60;}}}});_4.declare("esri.dijit.BasemapLayer",null,{constructor:function(_65){_65=_65||{};if(!_65.url&&!_65.type){console.error("esri.dijit.BasemapLayer: unable to find the 'url' or 'type' property in parameters");}this.url=_65.url;this.type=_65.type;this.isReference=(_65.isReference===true)?true:false;this.displayLevels=_65.displayLevels;this.visibleLayers=_65.visibleLayers;this.bandIds=_65.bandIds;this.opacity=_65.opacity;}});}}};});