/*
 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.wms"],["require","esri.layers.dynamic"],["require","esri.layers.agscommon"]],defineResource:function(_4,_5,_6){if(!_4._hasResource["esri.layers.wms"]){_4._hasResource["esri.layers.wms"]=true;_4.provide("esri.layers.wms");_4.require("esri.layers.dynamic");_4.require("esri.layers.agscommon");_4.declare("esri.layers.WMSLayer",[esri.layers.DynamicMapServiceLayer],{_CRS_TO_EPSG:{84:4326,83:4269,27:4267},_REVERSED_LAT_LONG_RANGES:[[4001,4999],[2044,2045],[2081,2083],[2085,2086],[2093,2093],[2096,2098],[2105,2132],[2169,2170],[2176,2180],[2193,2193],[2200,2200],[2206,2212],[2319,2319],[2320,2462],[2523,2549],[2551,2735],[2738,2758],[2935,2941],[2953,2953],[3006,3030],[3034,3035],[3058,3059],[3068,3068],[3114,3118],[3126,3138],[3300,3301],[3328,3335],[3346,3346],[3350,3352],[3366,3366],[3416,3416],[20004,20032],[20064,20092],[21413,21423],[21473,21483],[21896,21899],[22171,22177],[22181,22187],[22191,22197],[25884,25884],[27205,27232],[27391,27398],[27492,27492],[28402,28432],[28462,28492],[30161,30179],[30800,30800],[31251,31259],[31275,31279],[31281,31290],[31466,31700]],_WEB_MERCATOR:[102100,3857,102113,900913],_WORLD_MERCATOR:[3395,54004],allExtents:[],constructor:function(_7,_8){_7=this._stripParameters(_7,["version","service","request","bbox","format","height","width","layers","srs","crs","styles","transparent","bgcolor","exceptions","time","elevation","sld","wfs"]);this.url=_7;this._url=esri.urlToObject(_7);this._getCapabilitiesURL=_7;this._initLayer=_4.hitch(this,this._initLayer);this._parseCapabilities=_4.hitch(this,this._parseCapabilities);this._getCapabilitiesError=_4.hitch(this,this._getCapabilitiesError);if(_8){this.imageFormat=this._getImageFormat(_8.format);this.imageTransparency=(_8.transparent===false)?false:true;this.visibleLayers=_8.visibleLayers?_8.visibleLayers:[];if(_8.resourceInfo){this._readResourceInfo(_8.resourceInfo);}else{this._getCapabilities();}}else{this.imageFormat="image/png";this.imageTransparency=true;this.visibleLayers=[];this._getCapabilities();}this._blankImageURL=_4.moduleUrl("esri","../../images/pixel.png").uri;},setVisibleLayers:function(_9){_9=this._checkVisibleLayersList(_9);this.visibleLayers=(_9)?_9:[];this.refresh(true);},setImageFormat:function(_a){this.imageFormat=this._getImageFormat(_a);this.refresh(true);},setImageTransparency:function(_b){this.imageTransparency=_b;this.refresh(true);},getImageUrl:function(_c,_d,_e,_f){if(!this.visibleLayers||this.visibleLayers.length===0){_f(this._blankImageURL);return;}var _10=_c.spatialReference.wkid;if(_4.some(this._WEB_MERCATOR,function(el){return el==_10;})){var _11=_4.filter(this.spatialReferences,function(el){return (_4.some(this._WEB_MERCATOR,function(el2){return el2==el;}));},this);if(_11.length==0){_11=_4.filter(this.spatialReferences,function(el){return (_4.some(this._WORLD_MERCATOR,function(el2){return el2==el;}));},this);}if(_11.length>0){_10=_11[0];}else{_10=this._WEB_MERCATOR[0];}}var _12=_4.filter(this.spatialReferences,function(el){return el!==_10;});this.spatialReferences=_12;this.spatialReferences.unshift(_10);var _13=_c.xmin;var _14=_c.xmax;var _15=_c.ymin;var _16=_c.ymax;var _17={};_17.SERVICE="WMS";_17.REQUEST="GetMap";_17.FORMAT=this.imageFormat;_17.TRANSPARENT=this.imageTransparency?"TRUE":"FALSE";_17.STYLES="";_17.VERSION=this.version;_17.LAYERS=this.visibleLayers?this.visibleLayers.toString():null;_17.WIDTH=_d;_17.HEIGHT=_e;if(this.maxWidth<_d){_17.WIDTH=this.maxWidth;}if(this.maxHeight<_e){_17.HEIGHT=this.maxHeight;}var _18=_10?_10:NaN;if(!isNaN(_18)){if(this.version=="1.3.0"){_17.CRS="EPSG:"+_18;}else{_17.SRS="EPSG:"+_18;}}if(this.version=="1.3.0"&&this._useLatLong(_18)){_17.BBOX=_15+","+_13+","+_16+","+_14;}else{_17.BBOX=_13+","+_15+","+_14+","+_16;}var _19=this.getMapURL;_19+=(_19.indexOf("?")==-1)?"?":"";for(var key in _17){_19+=(_19.substring(_19.length-1,_19.length)=="?")?"":"&";_19+=key+"="+_17[key];}_f(_19);},_initLayer:function(_1a,io){this.spatialReference=new esri.SpatialReference(this.extent.spatialReference);this.initialExtent=new esri.geometry.Extent(this.extent);this.fullExtent=new esri.geometry.Extent(this.extent);this.visibleLayers=this._checkVisibleLayersList(this.visibleLayers);this.loaded=true;this.onLoad(this);var _1b=this._loadCallback;if(_1b){delete this._loadCallback;_1b(this);}},_readResourceInfo:function(_1c){if(!_1c.extent){console.error("esri.layers.WMSLayer: unable to find the 'extent' property in resourceInfo");return;}if(!_1c.layerInfos){console.error("esri.layers.WMSLayer: unable to find the 'layerInfos' property in resourceInfo");return;}this.extent=_1c.extent;this.allExtents[0]=_1c.extent;this.layerInfos=_1c.layerInfos;this.description=_1c.description?_1c.description:"";this.copyright=_1c.copyright?_1c.copyright:"";this.title=_1c.title?_1c.title:"";this.getMapURL=_1c.getMapURL?_1c.getMapURL:this._getCapabilitiesURL;this.version=_1c.version?_1c.version:"1.3.0";this.maxWidth=_1c.maxWidth?_1c.maxWidth:5000;this.maxHeight=_1c.maxHeight?_1c.maxHeight:5000;this.spatialReferences=_1c.spatialReferences?_1c.spatialReferences:[];this.imageFormat=this._getImageFormat(_1c.format);this._initLayer();},_getCapabilities:function(){var _1d=this._url.query?this._url.query:{};_1d.SERVICE="WMS";_1d.REQUEST="GetCapabilities";var uri=this._url.path+"?";for(var key in _1d){uri+=(uri.substring(uri.length-1,uri.length)=="?")?"":"&";uri+=key+"="+_1d[key];}var _1e=esri.request({url:uri,handleAs:"xml",load:this._parseCapabilities,error:this._getCapabilitiesError},{usePost:false});},_parseCapabilities:function(xml){if(!xml){this.onError("GetCapabilities request for "+this._getCapabilitiesURL+" failed. (Response is null.)");return;}this.version=this._getAttributeValue("WMS_Capabilities","version",xml,null);if(!this.version){this.version=this._getAttributeValue("WMT_MS_Capabilities","version",xml,"1.3.0");}var _1f=this._getTag("Service",xml);this.title=this._getTagValue("Title",_1f,"");if(!this.title||this.title.length==0){this.title=this._getTagValue("Name",_1f,"");}this.copyright=this._getTagValue("AccessConstraints",_1f,"");this.description=this._getTagValue("Abstract",_1f,"");this.maxWidth=parseInt(this._getTagValue("MaxWidth",_1f,5000));this.maxHeight=parseInt(this._getTagValue("MaxHeight",_1f,5000));var _20=this._getTag("Layer",xml);if(!_20){this._getCapabilitiesError({"error":{"message":"Response does not contain any layers."}});return;}var _21=this._getLayerInfo(_20);if(_21){this.layerInfos=_21.subLayers;if(!this.layerInfos||this.layerInfos.length==0){this.layerInfos=[_21];}this.extent=_21.extent;this.allExtents=_21.allExtents;this.spatialReferences=_21.spatialReferences;if(this.spatialReferences.length==0&&this.layerInfos.length>0){this.spatialReferences=this.layerInfos[0].spatialReferences;}}this.getMapURL=this._getCapabilitiesURL;var _22=_4.query("DCPType",this._getTag("GetMap",xml));if(_22&&_22.length>0){var _23=_4.query("HTTP",_22[0]);if(_23&&_23.length>0){var _24=_4.query("Get",_23[0]);if(_24&&_24.length>0){var _25=this._getAttributeValue("OnlineResource","xlink:href",_24[0],null);if(_25){if(_25.indexOf("&")==(_25.length-1)){_25=_25.substring(0,_25.length-1);}this.getMapURL=_25;}}}}this.getMapFormats=[];if(_4.query("Operation",xml).length==0){_4.forEach(_4.query("Format",this._getTag("GetMap",xml)),function(_26){this.getMapFormats.push(_26.text?_26.text:_26.textContent);},this);}else{_4.forEach(_4.query("Operation",xml),function(_27){if(_27.getAttribute("name")=="GetMap"){_4.forEach(_4.query("Format",_27),function(_28){this.getMapFormats.push(_28.text?_28.text:_28.textContent);},this);}},this);}if(!_4.some(this.getMapFormats,function(el){return el.indexOf(this.imageFormat)>-1;},this)){this.imageFormat=this.getMapFormats[0];}this._initLayer();},_getCapabilitiesError:function(_29,io){this.onError("GetCapabilities request for "+this._getCapabilitiesURL+" failed. ("+_4.toJson(_29)+")");},_getLayerInfo:function(_2a){if(!_2a){return null;}var _2b=new esri.layers.WMSLayerInfo();_2b.name="";_2b.title="";_2b.description="";_2b.allExtents=[];_2b.spatialReferences=[];_2b.subLayers=[];var _2c=this._getTag("LatLonBoundingBox",_2a);if(_2c){_2b.allExtents[0]=this._getExtent(_2c,4326);}var _2d=this._getTag("EX_GeographicBoundingBox",_2a);if(_2d){var _2e=new esri.geometry.Extent(0,0,0,0,new esri.SpatialReference({wkid:4326}));_2e.xmin=parseFloat(this._getTagValue("westBoundLongitude",_2d,0));_2e.ymin=parseFloat(this._getTagValue("southBoundLatitude",_2d,0));_2e.xmax=parseFloat(this._getTagValue("eastBoundLongitude",_2d,0));_2e.ymax=parseFloat(this._getTagValue("northBoundLatitude",_2d,0));_2b.allExtents[0]=_2e;}_2b.extent=_2b.allExtents[0];var _2f=(_4.indexOf(["1.0.0","1.1.0","1.1.1"],this.version)>-1)?"SRS":"CRS";_4.forEach(_2a.childNodes,function(_30){if(_30.nodeName=="Name"){_2b.name=(_30.text?_30.text:_30.textContent)||"";}else{if(_30.nodeName=="Title"){_2b.title=(_30.text?_30.text:_30.textContent)||"";}else{if(_30.nodeName=="Abstract"){_2b.description=(_30.text?_30.text:_30.textContent)||"";}else{if(_30.nodeName=="BoundingBox"){srAttr=_30.getAttribute(_2f);if(srAttr&&srAttr.indexOf("EPSG:")===0){wkid=parseInt(srAttr.substring(5));if(wkid!==0&&!isNaN(wkid)){var _31;if(this.version=="1.3.0"){_31=this._getExtent(_30,wkid,this._useLatLong(wkid));}else{_31=this._getExtent(_30,wkid);}_2b.allExtents[wkid]=_31;if(!_2b.extent){_2b.extent=_31;}}}else{if(srAttr&&srAttr.indexOf("CRS:")===0){wkid=parseInt(srAttr.substring(4));if(wkid!==0&&!isNaN(wkid)){if(this._CRS_TO_EPSG[wkid]){wkid=this._CRS_TO_EPSG[wkid];}_2b.allExtents[wkid]=this._getExtent(_30,wkid);}}else{wkid=parseInt(srAttr);if(wkid!==0&&!isNaN(wkid)){_2b.allExtents[wkid]=this._getExtent(_30,wkid);}}}}else{if(_30.nodeName==_2f){var _32=_30.text?_30.text:_30.textContent;var arr=_32.split(" ");_4.forEach(arr,function(val){if(val.indexOf(":")>-1){val=parseInt(val.split(":")[1]);}else{val=parseInt(val);}if(val!==84&&val!==0&&!isNaN(val)){if(!_4.some(_2b.spatialReferences,function(el){return (el==val);})){_2b.spatialReferences.push(val);}}},this);}else{if(_30.nodeName=="Style"){var _33=this._getTag("LegendURL",_30);if(_33){var _34=this._getTag("OnlineResource",_33);if(_34){_2b.legendURL=_34.getAttribute("xlink:href");}}}else{if(_30.nodeName==="Layer"){_2b.subLayers.push(this._getLayerInfo(_30));}}}}}}}},this);return _2b;},_getImageFormat:function(_35){var _36=_35?_35.toLowerCase():"";switch(_36){case "jpg":return "image/jpeg";case "bmp":return "image/bmp";case "gif":return "image/gif";case "svg":return "image/svg+xml";default:return "image/png";}},getImageFormat:function(){var _37=this.imageFormat?this.imageFormat.toLowerCase():"";switch(_37){case "image/jpeg":return "jpg";case "image/bmp":return "bmp";case "image/gif":return "gif";case "image/svg+xml":return "svg";default:return "png";}},_getExtent:function(_38,_39,_3a){var _3b;if(_38){_3b=new esri.geometry.Extent();var _3c=parseFloat(_38.getAttribute("minx"));var _3d=parseFloat(_38.getAttribute("miny"));var _3e=parseFloat(_38.getAttribute("maxx"));var _3f=parseFloat(_38.getAttribute("maxy"));if(_3a){_3b.xmin=isNaN(_3d)?((-1)*Number.MAX_VALUE):_3d;_3b.ymin=isNaN(_3c)?((-1)*Number.MAX_VALUE):_3c;_3b.xmax=isNaN(_3f)?Number.MAX_VALUE:_3f;_3b.ymax=isNaN(_3e)?Number.MAX_VALUE:_3e;}else{_3b.xmin=isNaN(_3c)?((-1)*Number.MAX_VALUE):_3c;_3b.ymin=isNaN(_3d)?((-1)*Number.MAX_VALUE):_3d;_3b.xmax=isNaN(_3e)?Number.MAX_VALUE:_3e;_3b.ymax=isNaN(_3f)?Number.MAX_VALUE:_3f;}_3b.spatialReference=new esri.SpatialReference({wkid:_39});}return _3b;},_useLatLong:function(_40){var _41;for(var i=0;i<this._REVERSED_LAT_LONG_RANGES.length;i++){var _42=this._REVERSED_LAT_LONG_RANGES[i];if(_40>=_42[0]&&_40<=_42[1]){_41=true;break;}}return _41;},_getTag:function(_43,xml){var _44=_4.query(_43,xml);if(_44&&_44.length>0){return _44[0];}else{return null;}},_getTagValue:function(_45,xml,_46){var _47=_4.query(_45,xml);if(_47&&_47.length>0){if(_47[0].text){return _47[0].text;}else{return _47[0].textContent;}}else{return _46;}},_getAttributeValue:function(_48,_49,xml,_4a){var _4b=_4.query(_48,xml);if(_4b&&_4b.length>0){return _4b[0].getAttribute(_49);}else{return _4a;}},_checkVisibleLayersList:function(_4c){if(_4c&&_4c.length>0&&this.layerInfos&&this.layerInfos.length>0){if((typeof _4c[0])=="number"){var _4d=[];_4.forEach(_4c,function(pos){if(pos<this.layerInfos.length){_4d.push(this.layerInfos[pos].name);}},this);_4c=_4d;}}return _4c;},_stripParameters:function(url,_4e){var obj=esri.urlToObject(url);qs=[];for(var _4f in obj.query){if(_4.indexOf(_4e,_4f.toLowerCase())===-1){qs.push(_4f+"="+obj.query[_4f]);}}return obj.path+(qs.length?("?"+qs.join("&")):"");}});_4.declare("esri.layers.WMSLayerInfo",null,{name:null,title:null,description:null,extent:null,legendURL:null,subLayers:[],allExtents:[],spatialReferences:[],constructor:function(_50){if(_50){this.name=_50.name;this.title=_50.title;this.description=_50.description;this.extent=_50.extent;this.legendURL=_50.legendURL;this.subLayers=_50.subLayers?_50.subLayers:[];this.allExtents=_50.allExtents?_50.allExtents:[];this.spatialReferences=_50.spatialReferences?_50.spatialReferences:[];}},clone:function(){var _51={name:this.name,title:this.title,description:this.description,legendURL:this.legendURL};if(this.extent){_51.extent=this.extent.getExtent();}_51.subLayers=[];_4.forEach(this.subLayers,function(_52){_51.subLayers.push(_52.clone());});_51.allExtents=[];for(var _53 in this.allExtents){_53=parseInt(_53);if(!isNaN(_53)){_51.allExtents[_53]=this.allExtents[_53].getExtent();}}_51.spatialReferences=[];_4.forEach(this.spatialReferences,function(_54){_51.spatialReferences.push(_54);});return _51;}});}}};});