/*
 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.wmts"],["require","esri.layers.tiled"],["require","esri.layers.agscommon"],["require","esri.WKIDUnitConversion"],["require","dojox.xml.parser"]],defineResource:function(_4,_5,_6){if(!_4._hasResource["esri.layers.wmts"]){_4._hasResource["esri.layers.wmts"]=true;_4.provide("esri.layers.wmts");_4.require("esri.layers.tiled");_4.require("esri.layers.agscommon");_4.require("esri.WKIDUnitConversion");_4.require("dojox.xml.parser");_4.declare("esri.layers.WMTSLayer",[esri.layers.TiledMapServiceLayer],{copyright:null,extent:null,tileUrl:null,layerInfo:null,spatialReference:null,tileInfo:null,constructor:function(_7,_8){this.version="1.0.0";this.tileUr=this._url=_7;this.serviceMode="RESTful";this._parseCapabilities=_4.hitch(this,this._parseCapabilities);this._getCapabilitiesError=_4.hitch(this,this._getCapabilitiesError);if(!_8){_8={};}if(_8.serviceMode){if(_8.serviceMode==="KVP"||_8.serviceMode==="RESTful"){this.serviceMode=_8.serviceMode;}else{console.error("WMTS mode could only be 'KVP' or 'RESTful'");return;}}if(_8.layerInfo){this.layerInfo=_8.layerInfo;this._identifier=_8.layerInfo.identifier;this._tileMatrixSetId=_8.layerInfo.tileMatrixSet;this.format="image/"+_8.layerInfo.format;this._style=_8.layerInfo.style;this.title=_8.layerInfo.title;}if(_8.resourceInfo){this.version=_8.resourceInfo.version;if(_8.resourceInfo.getTileUrl){this._url=this.tileUrl=_8.resourceInfo.getTileUrl;}this.copyright=_8.resourceInfo.copyright;this.layerInfos=_8.resourceInfo.layerInfos;this._parseResourceInfo();this.loaded=true;this.onLoad(this);}else{this._getCapabilities();}this._formatDictionary={"image/png":".png","image/png8":".png","image/png24":".png","image/png32":".png","image/jpg":".jpg","image/jpeg":".jpg","image/gif":".gif","image/bmp":".bmp","image/tiff":".tif"};},setActiveLayer:function(_9){this.layerInfo=_9;this._identifier=_9.identifier;this._tileMatrixSetId=_9.tileMatrixSet;if(_9.format){this.format="image/"+_9.format;}this._style=_9.style;this.title=_9.title;this._parseResourceInfo();this.refresh(true);},getTileUrl:function(_a,_b,_c){var _d;if(this.serviceMode==="KVP"){_d=this._url+"SERVICE=WMTS&VERSION="+this.version+"&REQUEST=GetTile"+"&LAYER="+this._identifier+"&STYLE="+this._style+"&FORMAT="+this.format+"&TILEMATRIXSET="+this._tileMatrixSetId+"&TILEMATRIX="+_a+"&TILEROW="+_b+"&TILECOL="+_c;}else{if(this.serviceMode==="RESTful"){var _e;if(this._formatDictionary[this.format]){_e=this._formatDictionary[this.format];}_d=this._url+this._identifier+"/"+this._style+"/"+this._tileMatrixSetId+"/"+_a+"/"+_b+"/"+_c+_e;}}return _d;},_parseResourceInfo:function(){var _f=this.layerInfos;if(this.serviceMode==="KVP"){this._url+=(this._url.substring(this._url.length-1,this._url.length)=="?")?"":"?";}for(var i=0;i<_f.length;i++){if((!this._identifier||_f[i].identifier===this._identifier)&&(!this.title||_f[i].title===this.title)&&(!this._tileMatrixSetId||_f[i].tileMatrixSet===this._tileMatrixSetId)&&(!this.format||"image/"+_f[i].format===this.format)&&(!this._style||_f[i].style===this._style)){_4.mixin(this,{"description":_f[i].description,tileInfo:_f[i].tileInfo,spatialReference:_f[i].tileInfo.spatialReference,fullExtent:_f[i].fullExtent,initialExtent:_f[i].initialExtent,_identifier:_f[i].identifier,_tileMatrixSetId:_f[i].tileMatrixSet,format:"image/"+_f[i].format,_style:_f[i].style});break;}}},_getCapabilities:function(){var _10;if(this.serviceMode==="KVP"){_10=this._url+"?request=GetCapabilities&service=WMTS&version="+this.version;}else{if(this.serviceMode==="RESTful"){_10=this._url+"/"+this.version+"/WMTSCapabilities.xml";}}esri.request({url:_10,handleAs:"text",load:this._parseCapabilities,error:this._getCapabilitiesError});},_parseCapabilities:function(_11){_11=_11.replace(/ows:/gi,"");var xml=_6.xml.parser.parse(_11);var _12=_4.query("OperationsMetadata",xml)[0];var _13=_4.query("[name='GetTile']",_12)[0];var _14=this.tileUrl;if(this._getAttributeValue("Get","xlink:href",_13)){_14=this._getAttributeValue("Get","xlink:href",_13);}if(_14.indexOf("/1.0.0/")===-1&&this.serviceMode==="RESTful"){_14+="/1.0.0/";}if(this.serviceMode==="KVP"){_14+=(_14.substring(_14.length-1,_14.length)=="?")?"":"?";}this._url=_14;var _15=_4.query("Contents",xml)[0];var _16,_17,_18,_19,lod,_1a=[];if(!this._identifier){this._identifier=this._getTagValues("Capabilities>Contents>Layer>Identifier",xml)[0];}this.copyright=this._getTagValues("Capabilities>ServiceIdentification>AccessConstraints",xml)[0];var _1b=this._getTagWithChildTagValue("Layer","Identifier",this._identifier,_15);this.description=this._getTagValues("Abstract",_1b)[0];this.title=this._getTagValues("Title",_1b)[0];if(!this._style){var _1c=_4.query("[isDefault='true']",_1b)[0];if(_1c){this._style=this._getTagValues("Identifier",_1c)[0];}this._style=this._getTagValues("Identifier",_4.query("Style",_1b)[0])[0];}var _1d=this._getTagValues("Format",_1b);if(!this.format){this.format=_1d[0];}if(_4.indexOf(_1d,this.format)===-1){console.error("The format "+this.format+" is not supported by the service");}var _1e=this._getTagValues("TileMatrixSet",_1b);if(!this._tileMatrixSetId){if(_4.indexOf(_1e,"GoogleMapsCompatible")!==-1){this._tileMatrixSetId="GoogleMapsCompatible";}else{this._tileMatrixSetId=_1e[0];}}var _1f=this._getTagWithChildTagValue("TileMatrixSetLink","TileMatrixSet",this._tileMatrixSetId,_1b);var _20=this._getTagValues("TileMatrix",_1f);var _21=this._getTagWithChildTagValue("TileMatrixSet","Identifier",this._tileMatrixSetId,_15);var crs=this._getTagValues("SupportedCRS",_21)[0];_19=crs.split(":").pop();if(_19==900913){_19=3857;}this.spatialReference=new esri.SpatialReference({"wkid":_19});var _22=_4.query("TileMatrix",_21)[0];_16=parseInt(this._getTagValues("TileWidth",_22)[0],10);_17=parseInt(this._getTagValues("TileHeight",_22)[0],10);var _23=this._getTagValues("TopLeftCorner",_22)[0].split(" ");var top=_23[0],_24=_23[1];if(top.split("E").length>1){var _25=top.split("E");top=_25[0]*Math.pow(10,_25[1]);}if(_24.split("E").length>1){var _26=_24.split("E");_24=_26[0]*Math.pow(10,_26[1]);}_18={"x":parseInt(top,10),"y":parseInt(_24,10)};if(_19==3857||_19==102113||_19==102100){_18={"x":-20037508.342787,"y":20037508.342787};}else{if(_19==4326){_18={"x":-180,"y":90};}}var _27=this._getTagValues("MatrixWidth",_22)[0];var _28=this._getTagValues("MatrixHeight",_22)[0];if(_20.length===0){var _29=_4.query("TileMatrix",_21);for(var j=0;j<_29.length;j++){lod=this._getLodFromTileMatrix(_29[j],_19);_1a.push(lod);}}else{for(var i=0;i<_20.length;i++){var _2a=this._getTagWithChildTagValue("TileMatrix","Identifier",_20[i],_21);lod=this._getLodFromTileMatrix(_2a,_19);_1a.push(lod);}}var _2b=_18.x;var _2c=_18.y;var _2d=_27>_28?_27:_28;var _2e=_27>_28?_28:_27;var _2f=_2b+_2d*_17*_1a[0].resolution;var _30=_2c-_2e*_16*_1a[0].resolution;var _31=new esri.geometry.Extent(_2b,_30,_2f,_2c);this.fullExtent=this.initialExtent=_31;this.tileInfo=new esri.layers.TileInfo({"dpi":90.71428571428571});_4.mixin(this.tileInfo,{"spatialReference":this.spatialReference},{"format":this.format},{"height":_16},{"width":_17},{"origin":_18},{"lods":_1a});this.loaded=true;this.onLoad(this);},_getCapabilitiesError:function(err){console.error("Failed to get capabilities xml");},_getLodFromTileMatrix:function(_32,_33){var id=this._getTagValues("Identifier",_32)[0];var _34=this._getTagValues("ScaleDenominator",_32)[0];if(_34.split("E").length>1){var _35=_34.split("E");_34=_35[0]*Math.pow(10,_35[1]);}else{_34=parseFloat(_34);}var _36;if(esri._isDefined(esri.WKIDUnitConversion[_33])){_36=esri.WKIDUnitConversion.values[esri.WKIDUnitConversion[_33]];}else{_36=111194.6519066546;}var _37=_34*7/25000/_36;var lod={"level":id,"scale":_34,"resolution":_37};return lod;},_getTag:function(_38,xml){var _39=_4.query(_38,xml);if(_39&&_39.length>0){return _39[0];}else{return null;}},_getTagValues:function(_3a,xml){var _3b=[];var _3c=_3a.split(">");var tag,_3d;tag=_4.query(_3c[0],xml)[0];if(_3c.length>1){for(var i=1;i<_3c.length-1;i++){tag=_4.query(_3c[i],tag)[0];}_3d=_4.query(_3c[_3c.length-1],tag);}else{_3d=_4.query(_3c[0],xml);}if(_3d&&_3d.length>0){_4.forEach(_3d,function(_3e){if(_4.isIE){_3b.push(_3e.childNodes[0].nodeValue);}else{_3b.push(_3e.textContent);}});}return _3b;},_getAttributeValue:function(_3f,_40,xml,_41){var _42=_4.query(_3f,xml);if(_42&&_42.length>0){return _42[0].getAttribute(_40);}else{return _41;}},_getTagWithChildTagValue:function(_43,_44,_45,xml){var _46=xml.childNodes;var _47;for(var j=0;j<_46.length;j++){if(_46[j].nodeName===_43){if(_4.isIE){if(esri._isDefined(_4.query(_44,_46[j])[0])){_47=_4.query(_44,_46[j])[0].childNodes[0].nodeValue;}}else{if(esri._isDefined(_4.query(_44,_46[j])[0])){_47=_4.query(_44,_46[j])[0].textContent;}}if(_47===_45){return _46[j];}}}}});_4.declare("esri.layers.WMTSLayerInfo",null,{identifier:null,tileMatrixSet:null,format:null,style:null,tileInfo:null,title:null,fullExtent:null,initialExtent:null,description:null,constructor:function(_48){if(_48){this.title=_48.title;this.tileMatrixSet=_48.tileMatrixSet;this.format=_48.format;this.style=_48.style;this.tileInfo=_48.tileInfo;this.fullExtent=_48.fullExtent;this.initialExtent=_48.initialExtent;this.identifier=_48.identifier;this.description=_48.description;}}});}}};});