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