/*
 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.layers.KMLLayer"],["require","esri.utils"],["require","esri.layers.layer"],["require","esri.layers.MapImageLayer"],["require","esri.layers.FeatureLayer"],["require","esri.dijit.Popup"]],defineResource:function(_4,_5,_6){if(!_4._hasResource["esri.layers.KMLLayer"]){_4._hasResource["esri.layers.KMLLayer"]=true;_4.provide("esri.layers.KMLLayer");_4.require("esri.utils");_4.require("esri.layers.layer");_4.require("esri.layers.MapImageLayer");_4.require("esri.layers.FeatureLayer");_4.require("esri.dijit.Popup");_4.declare("esri.layers.KMLLayer",[esri.layers.Layer],{serviceUrl:"http://utility.arcgis.com/sharing/kml",constructor:function(_7,_8){if(!_7){console.log("KMLLayer:constructor - please provide url for the KML file");}this._outSR=(_8&&_8.outSR)||new esri.SpatialReference({wkid:4326});this._options=_8;if(esri.config.defaults.kmlService){this.serviceUrl=esri.config.defaults.kmlService;}var _9=(this.linkInfo=_8&&_8.linkInfo);if(_9){this.visible=!!_9.visibility;this._waitingForMap=!!_9.viewFormat;}if(!_9||(_9&&_9.visibility&&!this._waitingForMap)){this._parseKml();}this.refresh=_4.hitch(this,this.refresh);},getFeature:function(_a){if(!_a){return;}var _b=_a.type,id=_a.id,_c,i,_d;switch(_b){case "esriGeometryPoint":case "esriGeometryPolyline":case "esriGeometryPolygon":var _e=this["_"+_b];if(_e){_c=_4.getObject("_mode._featureMap."+id,false,_e);}break;case "GroundOverlay":var _f=this._groundLyr;if(_f){var _10=_f.getImages();_d=_10.length;for(i=0;i<_d;i++){if(_10[i].id===id){_c=_10[i];break;}}}break;case "ScreenOverlay":break;case "NetworkLink":_4.some(this._links,function(_11){if(_11.linkInfo&&_11.linkInfo.id===id){_c=_11;return true;}else{return false;}});break;case "Folder":var _12=this.folders;_d=_12?_12.length:0;for(i=0;i<_d;i++){if(_12[i].id===id){_c=_12[i];break;}}break;default:console.log("KMLLayer:getFeature - unknown feature type");break;}return _c;},getLayers:function(){var _13=[];if(this._groundLyr){_13.push(this._groundLyr);}if(this._fLayers){_13=_13.concat(this._fLayers);}if(this._links){_4.forEach(this._links,function(_14){if(_14.declaredClass){_13.push(_14);}});}return _13;},setFolderVisibility:function(_15,_16){if(!_15){return;}this._fireUpdateStart();_15.visible=_16;if(_16){_16=this._areLocalAncestorsVisible(_15);}this._setState(_15,_16);this._fireUpdateEnd();},onRefresh:function(){},_parseKml:function(map){var _17=this;this._fireUpdateStart();this._io=esri.request({url:this.serviceUrl,content:{url:this._url.path+this._getQueryParameters(map),model:"simple",folders:"",refresh:this.loaded?true:undefined,outSR:_4.toJson(this._outSR.toJson())},callbackParamName:"callback",load:function(_18){_17._io=null;_17._initLayer(_18);},error:function(err){_17._io=null;err=_4.mixin(new Error(),err);err.message="Unable to load KML: "+_17.url+" "+(err.message||"");_17._fireUpdateEnd(err);_17.onError(err);}});},_initLayer:function(_19){if(this.loaded){this._removeInternalLayers();}this.name=_19.name;this.description=_19.description;this.snippet=_19.snippet;this.visibility=_19.visibility;this.featureInfos=_19.featureInfos;var i,len;var _1a=(this.folders=_19.folders),_1b=[],_1c;if(_1a){len=_1a.length;for(i=0;i<len;i++){_1c=(_1a[i]=new esri.layers.KMLFolder(_1a[i]));if(_1c.parentFolderId===-1){_1b.push(_1c);}}}var _1d=(this._links=_19.networkLinks),_1e;len=_1d?_1d.length:0;for(i=0;i<len;i++){if(_1d[i].viewRefreshMode&&_1d[i].viewRefreshMode.toLowerCase().indexOf("onregion")!==-1){continue;}_1e=_4.mixin({},this._options);_1e.linkInfo=_1d[i];if(_1e.id){_1e.id=_1e.id+"_"+i;}_1d[i]=new esri.layers.KMLLayer(_1d[i].href,_1e);_1d[i]._parentLayer=this;_1d[i]._parentFolderId=this._getLinkParentId(_1d[i].linkInfo.id);}var _1f=_19.groundOverlays;if(_1f&&_1f.length>0){_1e=_4.mixin({},this._options);if(_1e.id){_1e.id=_1e.id+"_"+"mapImage";}var _20=(this._groundLyr=new esri.layers.MapImageLayer(_1e));len=_1f.length;for(i=0;i<len;i++){_20.addImage(new esri.layers.KMLGroundOverlay(_1f[i]));}}var _21=_4.getObject("featureCollection.layers",false,_19);if(_21&&_21.length>0){this._fLayers=[];_4.forEach(_21,function(_22,i){var _23=_4.getObject("featureSet.features",false,_22),_24;if(_23&&_23.length>0){_1e=_4.mixin({outFields:["*"],infoTemplate:_22.popupInfo?new esri.dijit.PopupTemplate(_22.popupInfo):null,editable:false},this._options);if(_1e.id){_1e.id=_1e.id+"_"+i;}_22.layerDefinition.capabilities="Query,Data";_24=new esri.layers.FeatureLayer(_22,_1e);if(_24.geometryType){this["_"+_24.geometryType]=_24;}this._fLayers.push(_24);}},this);if(this._fLayers.length===0){delete this._fLayers;}}len=_1b.length;for(i=0;i<len;i++){_1c=_1b[i];this._setState(_1c,_1c.visible);}this._fireUpdateEnd();if(this.loaded){this._addInternalLayers();this.onRefresh();}else{this.loaded=true;this.onLoad(this);}},_addInternalLayers:function(){var map=this._map;this._fireUpdateStart();if(this._links){_4.forEach(this._links,function(_25){if(_25.declaredClass){map.addLayer(_25);if(_25._waitingForMap){_25._waitingForMap=null;if(_25.visible){_25._parseKml(map);}else{_25._wMap=map;}}}});}var _26=map.spatialReference,_27=this._outSR,_28,_29;if(_26.wkid){_28=(_26._isWebMercator()&&_27._isWebMercator())||(_26.wkid===_27.wkid);}else{if(_26.wkt){_28=(_26.wkt===_27.wkt);}else{console.log("KMLLayer:_setMap - map has invalid spatial reference");return;}}if(!_28){if(_26._isWebMercator()&&_27.wkid===4326){_29=esri.geometry.geographicToWebMercator;}else{if(_27._isWebMercator()&&_26.wkid===4326){_29=esri.geometry.webMercatorToGeographic;}else{console.log("KMLLayer:_setMap - unsupported workflow. Spatial reference of the map and kml layer do not match, and the conversion cannot be done on the client.");return;}}}if(this._groundLyr){if(_29){_4.forEach(this._groundLyr.getImages(),function(_2a){_2a.extent=_29(_2a.extent);});}map.addLayer(this._groundLyr);}var _2b=this._fLayers;if(_2b&&_2b.length>0){_4.forEach(_2b,function(_2c){if(_29){var _2d=_2c.graphics,i,_2e,len=_2d?_2d.length:0;for(i=0;i<len;i++){_2e=_2d[i].geometry;if(_2e){_2d[i].setGeometry(_29(_2e));}}}map.addLayer(_2c);});}this.onVisibilityChange(this.visible);},_removeInternalLayers:function(){var map=this._map;if(this._links){_4.forEach(this._links,function(_2f){if(_2f.declaredClass&&_2f._io){_2f._io.cancel();}});}if(map){_4.forEach(this.getLayers(),map.removeLayer,map);}},_setState:function(_30,_31){var _32=_30.featureInfos,_33,_34,i,len=_32?_32.length:0,_35=_31?"show":"hide";for(i=0;i<len;i++){_33=_32[i];_34=this.getFeature(_33);if(!_34){continue;}if(_33.type==="Folder"){this._setState(_34,_31&&_34.visible);}else{if(_33.type==="NetworkLink"){this._setInternalVisibility(_34,_31);}else{_34[_35]();}}}},_areLocalAncestorsVisible:function(_36){var _37=_36.parentFolderId,_38=_36.visible;while(_38&&_37!==-1){var _39=this.getFeature({type:"Folder",id:_37});_38=_38&&_39.visible;_37=_39.parentFolderId;}return _38;},_setInternalVisibility:function(_3a,_3b){var _3c=_3a._parentLayer,_3d=_3a._parentFolderId;_3b=_3b&&_3a.visible;while(_3b&&_3c){_3b=_3b&&_3c.visible;if(_3d>-1){_3b=_3b&&_3c._areLocalAncestorsVisible(_3c.getFeature({type:"Folder",id:_3d}));}_3d=_3c._parentFolderId;_3c=_3c._parentLayer;}this._setIntState(_3a,_3b);},_setIntState:function(_3e,_3f){if(!_3e){return;}_4.forEach(_3e.getLayers(),function(_40){if(_40.linkInfo){_3e._setIntState(_40,_3f&&_40.visible&&_3e._areLocalAncestorsVisible(_3e.getFeature({type:"Folder",id:_40._parentFolderId})));}else{_40.setVisibility(_3f);}});},_getLinkParentId:function(id){var _41=-1;if(this.folders){_4.some(this.folders,function(_42){if(_42.networkLinkIds&&_4.indexOf(_42.networkLinkIds,id)!==-1){_41=_42.id;return true;}return false;});}return _41;},_checkAutoRefresh:function(){var _43=this.linkInfo;if(_43){if(this.visible){if(this.loaded&&this._map){var _44=_43.refreshMode,_45=_43.refreshInterval,_46=_43.viewRefreshMode,_47=_43.viewRefreshTime;if(_44&&_44.toLowerCase().indexOf("oninterval")!==-1&&_45>0){this._stopAutoRefresh();this._timeoutHandle=setTimeout(this.refresh,_45*1000);}if(_46&&_46.toLowerCase().indexOf("onstop")!==-1&&_47>0){if(!this._extChgHandle){this._extChgHandle=_4.connect(this._map,"onExtentChange",this,this._extentChanged);}}}}else{this._stopAutoRefresh();_4.disconnect(this._extChgHandle);delete this._extChgHandle;}}},_stopAutoRefresh:function(){clearTimeout(this._timeoutHandle);this._timeoutHandle=null;},_getQueryParameters:function(map){map=map||this._map;var _48={},_49=this.linkInfo,_4a=map&&map.extent;if(this._url.query){_4.mixin(_48,this._url.query);}if(_49){var _4b=_49.viewFormat,_4c=_49.httpQuery,_4d=_49.viewBoundScale;if(_4a&&_4b){var _4e=_4a,_4f=_4a,sr=_4a.spatialReference;if(sr){if(sr._isWebMercator()){_4e=esri.geometry.webMercatorToGeographic(_4a);}else{if(sr.wkid===4326){_4f=esri.geometry.geographicToWebMercator(_4a);}}}var _50=_4e.getCenter(),_51=Math.max(_4f.getWidth(),_4f.getHeight());if(_4d){_4e=_4e.expand(_4d);}_4b=_4b.replace(/\[bboxWest\]/ig,_4e.xmin).replace(/\[bboxEast\]/ig,_4e.xmax).replace(/\[bboxSouth\]/ig,_4e.ymin).replace(/\[bboxNorth\]/ig,_4e.ymax).replace(/\[lookatLon\]/ig,_50.x).replace(/\[lookatLat\]/ig,_50.y).replace(/\[lookatRange\]/ig,_51).replace(/\[lookatTilt\]/ig,0).replace(/\[lookatHeading\]/ig,0).replace(/\[lookatTerrainLon\]/ig,_50.x).replace(/\[lookatTerrainLat\]/ig,_50.y).replace(/\[lookatTerrainAlt\]/ig,0).replace(/\[cameraLon\]/ig,_50.x).replace(/\[cameraLat\]/ig,_50.y).replace(/\[cameraAlt\]/ig,_51).replace(/\[horizFov\]/ig,60).replace(/\[vertFov\]/ig,60).replace(/\[horizPixels\]/ig,map.width).replace(/\[vertPixels\]/ig,map.height).replace(/\[terrainEnabled\]/ig,0);_4.mixin(_48,_4.queryToObject(_4b));}if(_4c){_4c=_4c.replace(/\[clientVersion\]/ig,esri.version).replace(/\[kmlVersion\]/ig,2.2).replace(/\[clientName\]/ig,"ArcGIS API for JavaScript").replace(/\[language\]/ig,_4.locale);_4.mixin(_48,_4.queryToObject(_4c));}}var _52=[],_53;for(_53 in _48){if(esri._isDefined(_48[_53])){_52.push(_53+"="+_48[_53]);}}_52=_52.join("&");return _52?("?"+_52):"";},_setMap:function(map,_54){this._map=map;var div=this._div=_4.create("div",null,_54);_4.style(div,"position","absolute");this._addInternalLayers();return div;},_unsetMap:function(map,_55){if(this._io){this._io.cancel();}this._stopAutoRefresh();_4.disconnect(this._extChgHandle);delete this._extChgHandle;this._removeInternalLayers();var div=this._div;if(div){_55.removeChild(div);_4.destroy(div);}this._map=this._wMap=this._div=null;},onVisibilityChange:function(_56){if(!this.loaded){if(this.linkInfo&&_56){if(!this._waitingForMap){this._parseKml(this._wMap);}}return;}this._fireUpdateStart();this._setInternalVisibility(this,_56);this._checkAutoRefresh();this._fireUpdateEnd();},refresh:function(){if(!this.loaded||!this._map||this._io){return;}this._parseKml();},_extentChanged:function(){this._stopAutoRefresh();this._timeoutHandle=setTimeout(this.refresh,this.linkInfo.viewRefreshTime*1000);}});_4.declare("esri.layers.KMLGroundOverlay",[esri.layers.MapImage],{constructor:function(_57){if(esri._isDefined(this.visibility)){this.visible=!!this.visibility;}}});_4.declare("esri.layers.KMLFolder",null,{constructor:function(_58){_4.mixin(this,_58);if(esri._isDefined(this.visibility)){this.visible=!!this.visibility;}}});}}};});