/*
 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.tiled"]){dojo._hasResource["esri.layers.tiled"]=true;dojo.provide("esri.layers.tiled");dojo.require("dojox.collections.ArrayList");dojo.require("esri.layers.layer");dojo.require("esri.geometry");dojo.require("dojox.gfx.matrix");dojo.declare("esri.layers.TiledMapServiceLayer",esri.layers.Layer,{constructor:function(_1,_2){dojo.connect(this,"onLoad",this,"_initTiledLayer");this._displayLevels=_2?_2.displayLevels:null;var dh=dojo.hitch;this._addImage=dh(this,this._addImage);this._tileLoadHandler=dh(this,this._tileLoadHandler);this._tileErrorHandler=dh(this,this._tileErrorHandler);this._tilePopPop=dh(this,this._tilePopPop);this._cleanUpRemovedImages=dh(this,this._cleanUpRemovedImages);this._fireOnUpdateEvent=dh(this,this._fireOnUpdateEvent);this._transitionEnd=dh(this,this._transitionEnd);},opacity:1,isPNG32:false,_initTiledLayer:function(){var ti=this.tileInfo,_3=ti.lods;this._tileW=ti.width;this._tileH=ti.height;this._normalizedScales=[];var _4=(this.scales=[]),dl=this._displayLevels,fe=this.fullExtent,ul=new esri.geometry.Point(fe.xmin,fe.ymax),lr=new esri.geometry.Point(fe.xmax,fe.ymin),_5=esri.TileUtils.getContainingTileCoords,_6,_7,i,_8=_3.length;for(i=0;i<_8;i++){_7=_3[i];_6=_5(ti,ul,_7);_7.startTileRow=_6.row<0?0:_6.row;_7.startTileCol=_6.col<0?0:_6.col;_6=_5(ti,lr,_7);_7.endTileRow=_6.row;_7.endTileCol=_6.col;if(!dl||dojo.indexOf(dl,_7.level)!==-1){_4[i]=_7.scale;this._normalizedScales[i]=_7.scale/ti.dpi;}}this._patchIE=dojo.isIE>=6&&dojo.isIE<7&&(this.isPNG32||ti.format==="Mixed");},_setMap:function(_9,_a,_b,_c){this._map=_9;var d=(this._div=dojo.create("div",null,_a));this._layerIndex=_b;var _d=_9.__visibleDelta,dc=dojo.connect,_e=esri._css.names,_f={position:"absolute",width:_9.width+"px",height:_9.height+"px",overflow:"visible"};if(_9.navigationMode==="css-transforms"){_f[_e.transform]=esri._css.translate(-_d.x,-_d.y);dojo.style(d,_f);delete _f[_e.transform];_f[_e.transition]=_e.transformName+" "+esri.config.defaults.map.zoomDuration+"ms ease";dojo.style((this._active=dojo.create("div",null,d)),_f);this._active._remove=0;this._passives=[];this._onScaleHandler_connect=dc(_9,"onScale",this,this._onScaleHandler);if(esri.isTouchEnabled){this._standby=[];var _10=this,_11=function(){_10._noDom=1;};this._onPanStartHandler_connect=dc(_9,"onPanStart",_11);this._onZoomStartHandler_connect=dc(_9,"onZoomStart",_11);}}else{_f.left=-_d.x+"px";_f.top=-_d.y+"px";dojo.style(d,_f);this._onZoomHandler_connect=dc(_9,"onZoom",this,"_onZoomHandler");}this._onPanHandler_connect=dc(_9,"onPan",this,"_onPanHandler");this._onExtentChangeHandler_connect=dc(_9,"onExtentChange",this,"_onExtentChangeHandler");this._onResizeHandler_connect=dc(_9,"onResize",this,"_onResizeHandler");this._opacityChangeHandler_connect=dc(this,"onOpacityChange",this,"_opacityChangeHandler");this._visibilityChangeHandler_connect=dc(this,"onVisibilityChange",this,"_visibilityChangeHandler");this._tileIds=[];this._tiles=[];this._tileBounds=[];this._ct=null;this._removeList=new dojox.collections.ArrayList();this._loadingList=new dojox.collections.ArrayList();var _12=this.tileInfo,sr=_12.spatialReference,_13=sr._getInfo();this._wrap=_9.wrapAround180&&sr._isWrappable()&&Math.abs(_13.origin[0]-_12.origin.x)<=_13.dx;if(this._wrap){esri.TileUtils._addFrameInfo(_12,_13);}var _14=_9.extent;if(!this.visible){this._visibilityChangeHandler(this.visible);}if(_14&&_9.loaded){this._onExtentChangeHandler(_14,null,null,_c);}return d;},_unsetMap:function(map,_15){if(_15){this._div=_15.removeChild(this._div);}dojo.destroy(this._div);this._map=this._layerIndex=this._div=this._standby=null;var dd=dojo.disconnect;dd(this._onExtentChangeHandler_connect);dd(this._onPanHandler_connect);dd(this._onZoomHandler_connect);dd(this._onScaleHandler_connect);dd(this._onLayerReorderHandler_connect);dd(this._onResizeHandler_connect);dd(this._opacityChangeHandler_connect);dd(this._visibilityChangeHandler_connect);dd(this._onPanStartHandler_connect);dd(this._onZoomStartHandler_connect);},_visibilityChangeHandler:function(v){if(v){esri.show(this._div);var map=this._map;if(map.navigationMode==="css-transforms"){this._onScaleHandler_connect=dojo.connect(map,"onScale",this,this._onScaleHandler);}else{this._onZoomHandler_connect=dojo.connect(map,"onZoom",this,"_onZoomHandler");}this._onPanHandler_connect=dojo.connect(map,"onPan",this,"_onPanHandler");this._onExtentChangeHandler(map.extent,null,true);}else{esri.hide(this._div);dojo.disconnect(this._onPanHandler_connect);dojo.disconnect(this._onZoomHandler_connect);dojo.disconnect(this._onScaleHandler_connect);}},_onResizeHandler:function(_16,_17,_18){var css={width:_17+"px",height:_18+"px"},ds=dojo.style,i;ds(this._div,css);if(this._map.navigationMode==="css-transforms"){if(this._active){ds(this._active,css);}for(i=this._passives.length-1;i>=0;i--){ds(this._passives[i],css);}}},_onExtentChangeHandler:function(_19,_1a,_1b,lod){var i,_1c=this._standby,img,_1d;if(this._map.navigationMode==="css-transforms"){if(_1b){for(i=this._passives.length-1;i>=0;i--){_1d=this._passives[i];dojo.style(_1d,esri._css.names.transition,"none");if(_1d._marked){this._passives.splice(i,1);if(_1d.parentNode){_1d.parentNode.removeChild(_1d);}dojo.destroy(_1d);}else{if(_1d.childNodes.length>0){_1d._multiply=_1d._multiply?dojox.gfx.matrix.multiply(_1d._matrix,_1d._multiply):_1d._matrix;}}}}this._noDom=0;if(_1c&&_1c.length){for(i=_1c.length-1;i>=0;i--){img=_1c[i];dojo.style(img,"visibility","visible");this._tilePopPop(img);_1c.splice(i,1);}}}var _1e=true;this._refreshArgs={extent:_19,lod:lod};if(!this.visible){_1e=false;}var map=this._map,_1f;if(lod){_1f=dojo.indexOf(this.scales,lod.scale)===-1;if(this.declaredClass==="esri.layers.WMTSLayer"){var _20=map._params.tileInfo.dpi;var _21=map.width>map.height?map.width:map.height;_1f=true;var s1,s2=lod.scale/_20;for(i=0;i<this._normalizedScales.length;i++){s1=this._normalizedScales[i];if(Math.abs((s1-s2)/s1)<(1/_21)){_1f=false;break;}}}}else{var _22=map.getLevel(),_23=(_22!==-1)?map._params.tileInfo.lods[_22].scale:-1;_1f=(dojo.indexOf(this.scales,_23)===-1);}if(_1e){var dd=dojo.disconnect;if(_1f){_1e=false;esri.hide(this._div);dd(this._onPanHandler_connect);dd(this._onZoomHandler_connect);dd(this._onScaleHandler_connect);}else{this._fireUpdateStart();esri.show(this._div);dd(this._onPanHandler_connect);dd(this._onZoomHandler_connect);dd(this._onScaleHandler_connect);if(map.navigationMode==="css-transforms"){this._onScaleHandler_connect=dojo.connect(map,"onScale",this,this._onScaleHandler);}else{this._onZoomHandler_connect=dojo.connect(map,"onZoom",this,"_onZoomHandler");}this._onPanHandler_connect=dojo.connect(map,"onPan",this,"_onPanHandler");}}this._rrIndex=0;var ct=esri.TileUtils.getCandidateTileInfo(map,this.tileInfo,_19),mv=map.__visibleDelta,id;if(!this._ct||ct.lod.level!==this._ct.lod.level||_1b){var _24=(ct&&this._ct&&ct.lod.level!==this._ct.lod.level);this._ct=ct;var _25=this._tiles,_26=this._tileIds,_27=this._tileBounds,_28=this._removeList,_29,il=_26.length;this._cleanUpRemovedImages();for(i=0;i<il;i++){id=_26[i];_29=_25[id];_27[id]=_26[i]=null;if((map.navigationMode==="css-transforms")&&_24&&_29.parentNode&&map.fadeOnZoom&&_1e){_29._fadeOut=_24;_29.parentNode._remove++;}_28.add(_29);}if(_1b){this._tileIds=[];this._tiles=[];this._tileBounds=[];}}var mx=mv.x,my=mv.y;if(map.navigationMode==="css-transforms"){var css={};css[esri._css.names.transform]=esri._css.translate(mx,my);dojo.style(this._div,css);}else{dojo.style(this._div,{left:mx+"px",top:my+"px"});}if(_1e&&!_1f){this.__coords_dx=mx;this.__coords_dy=my;this._updateImages(new esri.geometry.Rect(0,0,mv.width,mv.height));if(this._loadingList.count===0){this.onUpdate();this._fireUpdateEnd();}else{this._fireOnUpdate=true;}}else{this._cleanUpRemovedImages();}var _2a,_2b,_2c=this._tileW,_2d=this._tileH;mv=new esri.geometry.Rect(-mv.x,-mv.y,mv.width,mv.height);for(i=this._tileIds.length-1;i>=0;i--){id=this._tileIds[i];if(id){img=this._tiles[id];_2a=dojo.coords(img);_2b=new esri.geometry.Rect(_2a.l,_2a.t,_2c,_2d);if(map.navigationMode==="css-transforms"){_2b.x=img._left;_2b.y=img._top;}if(mv.intersects(_2b)){this._tileBounds[id]=_2b;}else{if(this._loadingList.contains(id)){this._tilePopPop(img);}dojo.destroy(img);this._tileIds.splice(i,1);delete this._tileBounds[id];delete this._tiles[id];}}else{this._tileIds.splice(i,1);delete this._tileBounds[id];delete this._tiles[id];}}},_onPanHandler:function(_2e,_2f){var map=this._map,mv=map.__visibleDelta.offset(_2f.x,_2f.y);this.__coords_dx=this.__coords_dy=0;if(map.navigationMode==="css-transforms"){var css={};css[esri._css.names.transform]=esri._css.translate(mv.x,mv.y);dojo.style(this._div,css);if(!esri.isTouchEnabled){this._updateImages({x:-mv.x,y:-mv.y,width:mv.width,height:mv.height});}}else{dojo.style(this._div,{left:mv.x+"px",top:mv.y+"px"});this._updateImages({x:-mv.x,y:-mv.y,width:mv.width,height:mv.height});}if(this._loadingList.count>0){this._fireUpdateStart();this._fireOnUpdate=true;}},_onScaleHandler:function(mtx,_30){var i,css={},_31=esri._css.names,map=this._map;for(i=this._passives.length-1;i>=0;i--){var _32=this._passives[i];if(_32.childNodes.length===0){this._passives.splice(i,1);dojo.destroy(_32);}else{if(_32.style[_31.transition]==="none"){dojo.style(_32,_31.transition,_31.transformName+" "+esri.config.defaults.map.zoomDuration+"ms ease");}dojo.style(_32,_31.transition,_30?"none":(_31.transformName+" "+esri.config.defaults.map.zoomDuration+"ms ease"));_32._matrix=mtx;css[_31.transform]=esri._css.matrix(_32._multiply?dojox.gfx.matrix.multiply(mtx,_32._multiply):mtx);dojo.style(_32,css);}}if(this._active&&this._active.childNodes.length===0){return;}dojo.style(this._active,_31.transition,_30?"none":(_31.transformName+" "+esri.config.defaults.map.zoomDuration+"ms ease"));this._active._matrix=mtx;css[_31.transform]=esri._css.matrix(this._active._matrix);dojo.style(this._active,css);this._passives.push(this._active);css={position:"absolute",width:map.width+"px",height:map.height+"px",overflow:"visible"};css[_31.transition]=_31.transformName+" "+esri.config.defaults.map.zoomDuration+"ms ease";dojo.style((this._active=dojo.create("div",null,this._div)),css);this._active._remove=0;if(map.fadeOnZoom){dojo.place(this._active,this._div,"first");}},_onZoomHandler:function(_33,_34,_35){var _36=dojo.coords(this._div);_35=_35.offset(-_36.l,-_36.t);var _37,_38=this._tileW*_34,_39=this._tileH*_34,_3a=this._tileBounds,_3b=this._tiles,es=dojo.style;var _3c=dojo.isIE;if(_3c&&_3c<8){dojo.forEach(this._tileIds,function(id){_37=_3a[id];es(_3b[id],{left:(_37.x-((_38-_37.width)*(_35.x-_37.x)/_37.width))+"px",top:(_37.y-((_39-_37.height)*(_35.y-_37.y)/_37.height))+"px",zoom:_34});});}else{dojo.forEach(this._tileIds,function(id){_37=_3a[id];es(_3b[id],{left:(_37.x-((_38-_37.width)*(_35.x-_37.x)/_37.width))+"px",top:(_37.y-((_39-_37.height)*(_35.y-_37.y)/_37.height))+"px",width:_38+"px",height:_39+"px"});});}},_updateImages:function(_3d){var id,_3e=this._tileW,_3f=this._tileH,_40=this._ct,lod=_40.lod,_41=_40.tile,off=_41.offsets,_42=_41.coords,cr=_42.row,cc=_42.col,_43=lod.level,_44=this.opacity,_45=this._tileIds,_46=this._loadingList,_47=this._addImage,mId=this._map.id,tId=this.id,rx=_3d.x,ry=_3d.y,str=lod.startTileRow,etr=lod.endTileRow,stc=lod.startTileCol,etc=lod.endTileCol,_48=dojo.indexOf,r,c,mvx=-_3d.x,mvy=-_3d.y,_49=off.x-this.__coords_dx,_4a=off.y-this.__coords_dy,vx=((_3e-_49)+mvx),vy=((_3f-_4a)+mvy),_4b=Math.ceil,_4c=(vx>0)?(vx%_3e):((_3e-(Math.abs(vx)%_3e))),_4d=(vy>0)?(vy%_3f):((_3f-(Math.abs(vy)%_3f))),_4e=(rx>0)?Math.floor((rx+_49)/_3e):_4b((rx-(_3e-_49))/_3e),_4f=(ry>0)?Math.floor((ry+_4a)/_3f):_4b((ry-(_3f-_4a))/_3f),_50=_4e+_4b((_3d.width-_4c)/_3e),_51=_4f+_4b((_3d.height-_4d)/_3f),_52,_53,_54,_55,col,row;if(this._wrap){_52=lod._frameInfo;_53=_52[0];_54=_52[1];_55=_52[2];}for(col=_4e;col<=_50;col++){for(row=_4f;row<=_51;row++){r=cr+row;c=cc+col;if(this._wrap){if(c<_54){c=c%_53;c=c<_54?c+_53:c;}else{if(c>_55){c=c%_53;}}}if(r>=str&&r<=etr&&c>=stc&&c<=etc){id=mId+"_"+tId+"_tile_"+_43+"_"+row+"_"+col;if(_48(_45,id)===-1){_46.add(id);_45.push(id);_47(_43,row,r,col,c,id,_3e,_3f,_44,_41,off);}}}}},_cleanUpRemovedImages:function(){var _56=this._removeList,dd=dojo.destroy,i,_57=esri._css.names;_56.forEach(function(img){if(!img._fadeOut){img.style.filter="";img.style.zoom=1;dd(img);}});if(this._map.navigationMode==="css-transforms"){for(i=this._passives.length-1;i>=0;i--){var _58=this._passives[i];if(_58.childNodes.length===0){this._passives.splice(i,1);dd(_58);}else{if(this._map.fadeOnZoom&&!_58._marked&&(_58._remove===_58.childNodes.length)){dojo.style(_58,_57.transition,"opacity 0.65s");dojo.style(_58,"opacity",0);_58._marked=1;if(dojo.isIE>=10){_58.addEventListener(_57.endEvent,this._transitionEnd,false);}else{_58._endHandle=dojo.connect(_58,_57.endEvent,this._transitionEnd);}}}}}_56.clear();},_transitionEnd:function(evt){var _59=evt.target,idx;if(evt.propertyName!=="opacity"){return;}if(dojo.isIE>=10){_59.removeEventListener(esri._css.names.endEvent,this._transitionEnd,false);}else{dojo.disconnect(_59._endHandle);_59._endHandle=null;}idx=dojo.indexOf(this._passives,_59);if(idx>-1){this._passives.splice(idx,1);}if(_59.parentNode){_59.parentNode.removeChild(_59);}dojo.destroy(_59);},_addImage:function(_5a,row,r,col,c,id,_5b,_5c,_5d,_5e,_5f){if(this._patchIE){var div=(this._tiles[id]=dojo.create("div"));div.id=id;dojo.addClass(div,"layerTile");dojo.style(div,{left:((_5b*col)-_5f.x)+"px",top:((_5c*row)-_5f.y)+"px",width:_5b+"px",height:_5c+"px",filter:"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+this.getTileUrl(_5a,r,c)+"', sizingMethod='scale')"});if(_5d<1){dojo.style(div,"opacity",_5d);}var _60=div.appendChild(dojo.create("div"));dojo.style(_60,{opacity:0,width:_5b+"px",height:_5c+"px"});this._div.appendChild(div);div=null;this._loadingList.remove(id);this._fireOnUpdateEvent();}else{var img=(this._tiles[id]=dojo.create("img")),dc=dojo.connect;img.id=id;dojo.addClass(img,"layerTile");var _61=(_5b*col)-_5f.x,top=(_5c*row)-_5f.y,map=this._map,_62=esri._css.names,css={width:_5b+"px",height:_5c+"px",visibility:"hidden"};if(map.navigationMode==="css-transforms"){css[_62.transform]=esri._css.translate(_61,top);dojo.style(img,css);img._left=_61;img._top=top;}else{css.left=_61+"px";css.top=top+"px";dojo.style(img,css);}if(_5d<1){dojo.style(img,"opacity",_5d);}img._onload_connect=dc(img,"onload",this,"_tileLoadHandler");img._onerror_connect=dc(img,"onerror",this,"_tileErrorHandler");img._onabort_connect=dc(img,"onabort",this,"_tileErrorHandler");var url=this.getTileUrl(_5a,r,c,img);if(url){img.src=url;}if(map.navigationMode==="css-transforms"){this._active.appendChild(img);}else{this._div.appendChild(img);}img=null;}},getTileUrl:function(_63,row,col){},refresh:function(){var ra=this._refreshArgs;this._onExtentChangeHandler(ra.extent,null,true,ra.lod);},_tilePopPop:function(img){var dd=dojo.disconnect;dd(img._onload_connect);dd(img._onerror_connect);dd(img._onabort_connect);img._onload_connect=img._onerror_connect=img._onabort_connect=null;this._loadingList.remove(img.id);this._fireOnUpdateEvent();},_tileLoadHandler:function(evt){var img=evt.currentTarget;if(this._noDom){this._standby.push(img);return;}dojo.style(img,"visibility","visible");this._tilePopPop(img);},_tileErrorHandler:function(evt){var img=evt.currentTarget;this.onError(new Error(esri.bundle.layers.tiled.tileError+": "+img.src));dojo.style(img,"visibility","hidden");this._tilePopPop(img);},_fireOnUpdateEvent:function(){if(this._loadingList.count===0){this._cleanUpRemovedImages();if(this._fireOnUpdate){this._fireOnUpdate=false;this.onUpdate();this._fireUpdateEnd();}}},setOpacity:function(o){if(this.opacity!=o){this.onOpacityChange(this.opacity=o);}},onOpacityChange:function(){},_opacityChangeHandler:function(_64){var djs=dojo.style,i,j,_65;if(this._map.navigationMode==="css-transforms"){if(this._active){_65=this._active.childNodes;for(i=_65.length-1;i>=0;i--){djs(_65[i],"opacity",_64);}}for(i=this._passives.length-1;i>=0;i--){_65=this._passives[i].childNodes;for(j=_65.length-1;j>=0;j--){djs(_65[j],"opacity",_64);}}return;}_65=this._div.childNodes;for(i=_65.length-1;i>=0;i--){djs(_65[i],"opacity",_64);}}});dojo.declare("esri.layers.TileInfo",null,{constructor:function(_66){this.width=_66.cols||_66.width;this.height=_66.rows||_66.height;this.dpi=_66.dpi;this.format=_66.format;var sr=_66.spatialReference,ori=_66.origin;if(sr){sr=(this.spatialReference=new esri.SpatialReference(sr.declaredClass?sr.toJson():sr));}if(ori){ori=(this.origin=new esri.geometry.Point(ori.declaredClass?ori.toJson():ori));if(!ori.spatialReference&&sr){ori.setSpatialReference(new esri.SpatialReference(sr.toJson()));}}var _67=(this.lods=[]);dojo.forEach(_66.lods,function(lod,i){_67[i]=new esri.layers.LOD(lod);});}});dojo.declare("esri.layers.LOD",null,{constructor:function(_68){dojo.mixin(this,_68);}});}