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