/*
 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
define("esri/layers/WMSLayer",["require","dojo/_base/kernel","dojo/_base/declare","dojo/_base/lang","dojo/_base/array","dojo/has","esri/kernel","esri/request","esri/urlUtils","esri/SpatialReference","esri/geometry/Extent","esri/layers/DynamicMapServiceLayer","esri/layers/WMSLayerInfo","dojo/query"],function(_1,_2,_3,_4,_5,_6,_7,_8,_9,_a,_b,_c,_d){var _e=_3([_c],{declaredClass:"esri.layers.WMSLayer",_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(_f,_10){_f=this._stripParameters(_f,["version","service","request","bbox","format","height","width","layers","srs","crs","styles","transparent","bgcolor","exceptions","time","elevation","sld","wfs"]);this.url=_f;this._url=_9.urlToObject(_f);this._getCapabilitiesURL=_f;this._initLayer=_4.hitch(this,this._initLayer);this._parseCapabilities=_4.hitch(this,this._parseCapabilities);this._getCapabilitiesError=_4.hitch(this,this._getCapabilitiesError);if(_10){this.imageFormat=this._getImageFormat(_10.format);this.imageTransparency=(_10.transparent===false)?false:true;this.visibleLayers=_10.visibleLayers?_10.visibleLayers:[];if(_10.resourceInfo){this._readResourceInfo(_10.resourceInfo);}else{this._getCapabilities();}}else{this.imageFormat="image/png";this.imageTransparency=true;this.visibleLayers=[];this._getCapabilities();}this._blankImageURL=_1.toUrl("esri/layers")+"/../images/pixel.png";},setVisibleLayers:function(_11){_11=this._checkVisibleLayersList(_11);this.visibleLayers=_11?_11:[];this.refresh(true);},setImageFormat:function(_12){this.imageFormat=this._getImageFormat(_12);this.refresh(true);},setImageTransparency:function(_13){this.imageTransparency=_13;this.refresh(true);},getImageUrl:function(_14,_15,_16,_17){if(!this.visibleLayers||this.visibleLayers.length===0){_17(this._blankImageURL);return;}var _18=_14.spatialReference.wkid;if(_5.some(this._WEB_MERCATOR,function(el){return el==_18;})){var _19=_5.filter(this.spatialReferences,function(el){return (_5.some(this._WEB_MERCATOR,function(el2){return el2==el;}));},this);if(_19.length==0){_19=_5.filter(this.spatialReferences,function(el){return (_5.some(this._WORLD_MERCATOR,function(el2){return el2==el;}));},this);}if(_19.length>0){_18=_19[0];}else{_18=this._WEB_MERCATOR[0];}}var _1a=_5.filter(this.spatialReferences,function(el){return el!==_18;});this.spatialReferences=_1a;this.spatialReferences.unshift(_18);var _1b=_14.xmin;var _1c=_14.xmax;var _1d=_14.ymin;var _1e=_14.ymax;var _1f={};_1f.SERVICE="WMS";_1f.REQUEST="GetMap";_1f.FORMAT=this.imageFormat;_1f.TRANSPARENT=this.imageTransparency?"TRUE":"FALSE";_1f.STYLES="";_1f.VERSION=this.version;_1f.LAYERS=this.visibleLayers?this.visibleLayers.toString():null;_1f.WIDTH=_15;_1f.HEIGHT=_16;if(this.maxWidth<_15){_1f.WIDTH=this.maxWidth;}if(this.maxHeight<_16){_1f.HEIGHT=this.maxHeight;}var _20=_18?_18:NaN;if(!isNaN(_20)){if(this.version=="1.3.0"){_1f.CRS="EPSG:"+_20;}else{_1f.SRS="EPSG:"+_20;}}if(this.version=="1.3.0"&&this._useLatLong(_20)){_1f.BBOX=_1d+","+_1b+","+_1e+","+_1c;}else{_1f.BBOX=_1b+","+_1d+","+_1c+","+_1e;}var _21=this.getMapURL,key;_21+=(_21.indexOf("?")==-1)?"?":"";for(key in _1f){_21+=(_21.substring(_21.length-1,_21.length)=="?")?"":"&";_21+=key+"="+_1f[key];}_17(_9.addProxy(_21));},_initLayer:function(_22,io){this.spatialReference=new _a(this.extent.spatialReference);this.initialExtent=new _b(this.extent);this.fullExtent=new _b(this.extent);this.visibleLayers=this._checkVisibleLayersList(this.visibleLayers);this.loaded=true;this.onLoad(this);var _23=this._loadCallback;if(_23){delete this._loadCallback;_23(this);}},_readResourceInfo:function(_24){if(!_24.extent){console.error("esri.layers.WMSLayer: unable to find the 'extent' property in resourceInfo");return;}if(!_24.layerInfos){console.error("esri.layers.WMSLayer: unable to find the 'layerInfos' property in resourceInfo");return;}this.extent=_24.extent;this.allExtents[0]=_24.extent;this.layerInfos=_24.layerInfos;this.description=_24.description?_24.description:"";this.copyright=_24.copyright?_24.copyright:"";this.title=_24.title?_24.title:"";this.getMapURL=_24.getMapURL?_24.getMapURL:this._getCapabilitiesURL;this.version=_24.version?_24.version:"1.3.0";this.maxWidth=_24.maxWidth?_24.maxWidth:5000;this.maxHeight=_24.maxHeight?_24.maxHeight:5000;this.spatialReferences=_24.spatialReferences?_24.spatialReferences:[];this.imageFormat=this._getImageFormat(_24.format);this.setScaleRange(_24.minScale,_24.maxScale);this._initLayer();},_getCapabilities:function(){var _25=this._url.query?this._url.query:{};_25.SERVICE="WMS";_25.REQUEST="GetCapabilities";var uri=this._url.path+"?",key;for(key in _25){uri+=(uri.substring(uri.length-1,uri.length)=="?")?"":"&";uri+=key+"="+_25[key];}var _26=_8({url:uri,handleAs:"xml",headers:{"Content-Type":null},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 _27=this._getTag("Service",xml);this.title=this._getTagValue("Title",_27,"");if(!this.title||this.title.length==0){this.title=this._getTagValue("Name",_27,"");}this.copyright=this._getTagValue("AccessConstraints",_27,"");this.description=this._getTagValue("Abstract",_27,"");this.maxWidth=parseInt(this._getTagValue("MaxWidth",_27,5000),10);this.maxHeight=parseInt(this._getTagValue("MaxHeight",_27,5000),10);var _28=this._getTag("Layer",xml);if(!_28){this._getCapabilitiesError({"error":{"message":"Response does not contain any layers."}});return;}var _29=this._getLayerInfo(_28);var _2a=0;var _2b=null;var _2c=this._getTag("Capability",xml);_5.forEach(_2c.childNodes,function(_2d){if(_2d.nodeName=="Layer"){if(_2a===0){_2b=_2d;}else{if(_2a===1){if(_29.name){_29.name="";_29.subLayers=[];_29.subLayers.push(this._getLayerInfo(_2b));}_29.subLayers.push(this._getLayerInfo(_2d));}else{_29.subLayers.push(this._getLayerInfo(_2d));}}_2a++;}},this);if(_29){this.layerInfos=_29.subLayers;if(!this.layerInfos||this.layerInfos.length==0){this.layerInfos=[_29];}this.extent=_29.extent;this.allExtents=_29.allExtents;this.spatialReferences=_29.spatialReferences;if(this.spatialReferences.length==0&&this.layerInfos.length>0){this.spatialReferences=this.layerInfos[0].spatialReferences;}}this.getMapURL=this._getCapabilitiesURL;var _2e=_2.query("DCPType",this._getTag("GetMap",xml));if(_2e&&_2e.length>0){var _2f=_2.query("HTTP",_2e[0]);if(_2f&&_2f.length>0){var _30=_2.query("Get",_2f[0]);if(_30&&_30.length>0){var _31=this._getAttributeValue("OnlineResource","xlink:href",_30[0],null);if(_31){if(_31.indexOf("&")==(_31.length-1)){_31=_31.substring(0,_31.length-1);}this.getMapURL=_31;}}}}this.getMapFormats=[];if(_2.query("Operation",xml).length==0){_5.forEach(_2.query("Format",this._getTag("GetMap",xml)),function(_32){this.getMapFormats.push(_32.text?_32.text:_32.textContent);},this);}else{_5.forEach(_2.query("Operation",xml),function(_33){if(_33.getAttribute("name")=="GetMap"){_5.forEach(_2.query("Format",_33),function(_34){this.getMapFormats.push(_34.text?_34.text:_34.textContent);},this);}},this);}if(!_5.some(this.getMapFormats,function(el){return el.indexOf(this.imageFormat)>-1;},this)){this.imageFormat=this.getMapFormats[0];}this._initLayer();},_getCapabilitiesError:function(_35,io){this.onError("GetCapabilities request for "+this._getCapabilitiesURL+" failed.",_35);},_getLayerInfo:function(_36){if(!_36){return null;}var _37=new _d();_37.name="";_37.title="";_37.description="";_37.allExtents=[];_37.spatialReferences=[];_37.subLayers=[];var _38=this._getTag("LatLonBoundingBox",_36);if(_38){_37.allExtents[0]=this._getExtent(_38,4326);}var _39=this._getTag("EX_GeographicBoundingBox",_36);if(_39){var _3a=new _b(0,0,0,0,new _a({wkid:4326}));_3a.xmin=parseFloat(this._getTagValue("westBoundLongitude",_39,0));_3a.ymin=parseFloat(this._getTagValue("southBoundLatitude",_39,0));_3a.xmax=parseFloat(this._getTagValue("eastBoundLongitude",_39,0));_3a.ymax=parseFloat(this._getTagValue("northBoundLatitude",_39,0));_37.allExtents[0]=_3a;}_37.extent=_37.allExtents[0];var _3b=(_5.indexOf(["1.0.0","1.1.0","1.1.1"],this.version)>-1)?"SRS":"CRS";_5.forEach(_36.childNodes,function(_3c){if(_3c.nodeName=="Name"){_37.name=(_3c.text?_3c.text:_3c.textContent)||"";}else{if(_3c.nodeName=="Title"){_37.title=(_3c.text?_3c.text:_3c.textContent)||"";}else{if(_3c.nodeName=="Abstract"){_37.description=(_3c.text?_3c.text:_3c.textContent)||"";}else{if(_3c.nodeName=="BoundingBox"){var _3d=_3c.getAttribute(_3b),_3e;if(_3d&&_3d.indexOf("EPSG:")===0){_3e=parseInt(_3d.substring(5),10);if(_3e!==0&&!isNaN(_3e)){var _3f;if(this.version=="1.3.0"){_3f=this._getExtent(_3c,_3e,this._useLatLong(_3e));}else{_3f=this._getExtent(_3c,_3e);}_37.allExtents[_3e]=_3f;if(!_37.extent){_37.extent=_3f;}}}else{if(_3d&&_3d.indexOf("CRS:")===0){_3e=parseInt(_3d.substring(4),10);if(_3e!==0&&!isNaN(_3e)){if(this._CRS_TO_EPSG[_3e]){_3e=this._CRS_TO_EPSG[_3e];}_37.allExtents[_3e]=this._getExtent(_3c,_3e);}}else{_3e=parseInt(_3d,10);if(_3e!==0&&!isNaN(_3e)){_37.allExtents[_3e]=this._getExtent(_3c,_3e);}}}}else{if(_3c.nodeName==_3b){var _40=_3c.text?_3c.text:_3c.textContent;var arr=_40.split(" ");_5.forEach(arr,function(val){if(val.indexOf(":")>-1){val=parseInt(val.split(":")[1],10);}else{val=parseInt(val,10);}if(val!==0&&!isNaN(val)){if(this._CRS_TO_EPSG[val]){val=this._CRS_TO_EPSG[val];}if(_5.indexOf(_37.spatialReferences,val)==-1){_37.spatialReferences.push(val);}}},this);}else{if(_3c.nodeName=="Style"){var _41=this._getTag("LegendURL",_3c);if(_41){var _42=this._getTag("OnlineResource",_41);if(_42){_37.legendURL=_42.getAttribute("xlink:href");}}}else{if(_3c.nodeName==="Layer"){_37.subLayers.push(this._getLayerInfo(_3c));}}}}}}}},this);_37.title=_37.title||_37.name;return _37;},_getImageFormat:function(_43){var _44=_43?_43.toLowerCase():"";switch(_44){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 _45=this.imageFormat?this.imageFormat.toLowerCase():"";switch(_45){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(_46,_47,_48){var _49;if(_46){_49=new _b();var _4a=parseFloat(_46.getAttribute("minx"));var _4b=parseFloat(_46.getAttribute("miny"));var _4c=parseFloat(_46.getAttribute("maxx"));var _4d=parseFloat(_46.getAttribute("maxy"));if(_48){_49.xmin=isNaN(_4b)?((-1)*Number.MAX_VALUE):_4b;_49.ymin=isNaN(_4a)?((-1)*Number.MAX_VALUE):_4a;_49.xmax=isNaN(_4d)?Number.MAX_VALUE:_4d;_49.ymax=isNaN(_4c)?Number.MAX_VALUE:_4c;}else{_49.xmin=isNaN(_4a)?((-1)*Number.MAX_VALUE):_4a;_49.ymin=isNaN(_4b)?((-1)*Number.MAX_VALUE):_4b;_49.xmax=isNaN(_4c)?Number.MAX_VALUE:_4c;_49.ymax=isNaN(_4d)?Number.MAX_VALUE:_4d;}_49.spatialReference=new _a({wkid:_47});}return _49;},_useLatLong:function(_4e){var _4f,i;for(i=0;i<this._REVERSED_LAT_LONG_RANGES.length;i++){var _50=this._REVERSED_LAT_LONG_RANGES[i];if(_4e>=_50[0]&&_4e<=_50[1]){_4f=true;break;}}return _4f;},_getTag:function(_51,xml){var _52=_2.query(_51,xml);if(_52&&_52.length>0){return _52[0];}else{return null;}},_getTagValue:function(_53,xml,_54){var _55=_2.query(_53,xml);if(_55&&_55.length>0){if(_55[0].text){return _55[0].text;}else{return _55[0].textContent;}}else{return _54;}},_getAttributeValue:function(_56,_57,xml,_58){var _59=_2.query(_56,xml);if(_59&&_59.length>0){return _59[0].getAttribute(_57);}else{return _58;}},_checkVisibleLayersList:function(_5a){if(_5a&&_5a.length>0&&this.layerInfos&&this.layerInfos.length>0){if((typeof _5a[0])=="number"){var _5b=[];_5.forEach(_5a,function(pos){if(pos<this.layerInfos.length){_5b.push(this.layerInfos[pos].name);}},this);_5a=_5b;}}return _5a;},_stripParameters:function(url,_5c){var obj=_9.urlToObject(url),_5d,qs=[];for(_5d in obj.query){if(_5.indexOf(_5c,_5d.toLowerCase())===-1){qs.push(_5d+"="+obj.query[_5d]);}}return obj.path+(qs.length?("?"+qs.join("&")):"");}});if(_6("extend-esri")){_4.setObject("layers.WMSLayer",_e,_7);}return _e;});