/*
 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.MapImageLayer"]){dojo._hasResource["esri.layers.MapImageLayer"]=true;dojo.provide("esri.layers.MapImageLayer");dojo.require("esri.utils");dojo.require("esri.layers.layer");dojo.require("esri.geometry");dojo.declare("esri.layers.MapImageLayer",[esri.layers.Layer],{"-chains-":{constructor:"manual"},constructor:function(_1){this.inherited(arguments,[null,_1]);this._mapImages=[];var _2=dojo.hitch;this._panStart=_2(this,this._panStart);this._pan=_2(this,this._pan);this._extentChange=_2(this,this._extentChange);this._zoom=_2(this,this._zoom);this._zoomStart=_2(this,this._zoomStart);this._scale=_2(this,this._scale);this._resize=_2(this,this._resize);this.loaded=true;this.onLoad(this);},opacity:1,_transform:esri._getDOMAccessor("transform"),addImage:function(_3){var _4=this._mapImages.push(_3);_4=_4-1;_3._idx=_4;_3._layer=this;if(this._div){this._createImage(_3,_4);}},removeImage:function(_5){if(_5){var _6=_5._idx,_7=this._mapImages;if(_7[_6]===_5){delete _7[_6];var _8=_5._node;if(_8){this._clearEvents(_8);_8.e_idx=_8.e_bl=_8.e_tr=_8.e_l=_8.e_t=_8.e_w=_8.e_h=null;if(_8.parentNode){_8.parentNode.removeChild(_8);dojo.destroy(_8);}}_5._node=_5._idx=_5._layer=null;}}},removeAllImages:function(){var _9=this._mapImages,i,_a=_9.length;for(i=0;i<_a;i++){var _b=_9[i];if(_b){this.removeImage(_b);}}this._mapImages=[];},getImages:function(){var _c=this._mapImages,_d=[],i,_e=_c.length;for(i=0;i<_e;i++){if(_c[i]){_d.push(_c[i]);}}return _d;},setOpacity:function(_f){if(this.opacity!=_f){this.onOpacityChange(this.opacity=_f);}},onOpacityChange:function(_10){var div=this._div,i,len,_11;if(div){if(!dojo.isIE||dojo.isIE>8){dojo.style(div,"opacity",_10);}else{_11=div.childNodes;len=_11.length;for(i=0;i<len;i++){dojo.style(_11[i],"opacity",_10);}}}},_createImage:function(_12,idx){var _13=dojo.create("img");dojo.style(_13,{position:"absolute"});if(dojo.isIE<=8){dojo.style(_13,"opacity",this.opacity);}if(_12.rotation){var _14="rotate("+(360-_12.rotation)+"deg)";if(dojo.isIE<9){}else{dojo.style(_13,this._transform,_14);dojo.style(_13,"transform",_14);}}_12._node=_13;_13.e_idx=idx;_13.e_layer=this;_13.e_load=dojo.connect(_13,"onload",esri.layers.MapImageLayer.prototype._imageLoaded);_13.e_error=dojo.connect(_13,"onerror",esri.layers.MapImageLayer.prototype._imageError);_13.e_abort=dojo.connect(_13,"onabort",esri.layers.MapImageLayer.prototype._imageError);_13.src=_12.href;},_imageLoaded:function(evt,img){var _15=img||evt.target||evt.currentTarget,_16=_15.e_layer,_17=_16._mapImages[_15.e_idx];if(_16._map&&(_16._map.__zooming||_16._map.__panning)){_16._standby.push(_15);return;}_16._clearEvents(_15);if(!_17||_17._node!==_15){return;}if(_16._map){_16._attach(_17);}},_imageError:function(evt){var _18=evt.target||evt.currentTarget,_19=_18.e_layer,_1a=_19._mapImages[_18.e_idx];_19._clearEvents(_18);if(_1a){_1a._node=null;}},_clearEvents:function(_1b){var _1c=dojo.disconnect;_1c(_1b.e_load);_1c(_1b.e_error);_1c(_1b.e_abort);_1b.e_load=_1b.e_error=_1b.e_abort=_1b.e_layer=null;},_attach:function(_1d){var _1e=_1d.extent,_1f,_20=_1e.spatialReference,_21=this._sr,div=this._div,_22=_1d._node,_23=new esri.geometry.Point({x:_1e.xmin,y:_1e.ymin}),_24=new esri.geometry.Point({x:_1e.xmax,y:_1e.ymax});if(_21.wkid){_1f=(_21._isWebMercator()&&_20._isWebMercator())||(_21.wkid===_20.wkid);}else{if(_21.wkt){_1f=(_21.wkt===_20.wkt);}}if(!_1f){if(_21._isWebMercator()&&_20.wkid===4326){_23=esri.geometry.geographicToWebMercator(_23);_24=esri.geometry.geographicToWebMercator(_24);}else{if(_20._isWebMercator()&&_21.wkid===4326){_23=esri.geometry.webMercatorToGeographic(_23);_24=esri.geometry.webMercatorToGeographic(_24);}}}_22.e_bl=_23;_22.e_tr=_24;if(_1d.visible){this._setPos(_22,div._left,div._top);(this._active||div).appendChild(_22);}},_setPos:function(_25,_26,_27){var _28=_25.e_bl,_29=_25.e_tr,map=this._map;_28=map.toScreen(_28);_29=map.toScreen(_29);var _2a=_28.x-_26,top=_29.y-_27,_2b=Math.abs(_29.x-_28.x),_2c=Math.abs(_28.y-_29.y),css={width:_2b+"px",height:_2c+"px"},_2d=this._mapImages[_25.e_idx];if(map.navigationMode==="css-transforms"){css[esri._css.names.transform]=esri._css.translate(_2a,top)+(_2d.rotation?(" "+esri._css.rotate(360-_2d.rotation)):"");}else{css.left=_2a+"px";css.top=top+"px";}dojo.style(_25,css);_25.e_l=_2a;_25.e_t=top;_25.e_w=_2b;_25.e_h=_2c;},_setMap:function(map,_2e){this._map=map;this._sr=map.spatialReference;var div=this._div=dojo.create("div",null,_2e),_2f=esri._css.names,css={position:"absolute"},vd=map.__visibleDelta;if(!dojo.isIE||dojo.isIE>8){css.opacity=this.opacity;}if(map.navigationMode==="css-transforms"){css[_2f.transform]=esri._css.translate(vd.x,vd.y);dojo.style(div,css);div._left=vd.x;div._top=vd.y;css={position:"absolute",width:map.width+"px",height:map.height+"px",overflow:"visible"};this._active=dojo.create("div",null,div);dojo.style(this._active,css);this._passive=dojo.create("div",null,div);dojo.style(this._passive,css);}else{div._left=0;div._top=0;dojo.style(div,css);}this._standby=[];var _30=this._mapImages,i,len=_30.length;for(i=0;i<len;i++){var _31=_30[i],_32=_31._node;if(!_32){this._createImage(_31,_31._idx);}}this.onVisibilityChange(this.visible);return div;},_unsetMap:function(map,_33){this._disconnect();var div=this._div;if(div){var _34=this._mapImages,i,len=_34.length;for(i=0;i<len;i++){var _35=_34[i];if(_35){var _36=_35._node;if(_36){this._clearEvents(_36);_36.e_idx=_36.e_bl=_36.e_tr=_36.e_l=_36.e_t=_36.e_w=_36.e_h=null;}_35._node=null;}}_33.removeChild(div);dojo.destroy(div);}this._map=this._div=this._sr=this._active=this._passive=this._standby=null;},onVisibilityChange:function(_37){var div=this._div;if(div){if(_37){this._redraw();this._connect(this._map);esri.show(div);}else{this._disconnect();esri.hide(div);}}},_connect:function(map){if(!this._connections){var _38=dojo.connect,_39=(map.navigationMode==="css-transforms");this._connections=[_38(map,"onPanStart",this._panStart),_38(map,"onPan",this._pan),_38(map,"onExtentChange",this._extentChange),_39&&_38(map,"onZoomStart",this._zoomStart),_39?_38(map,"onScale",this._scale):_38(map,"onZoom",this._zoom),_39&&_38(map,"onResize",this._resize)];}},_disconnect:function(){if(this._connections){dojo.forEach(this._connections,dojo.disconnect);this._connections=null;}},_panStart:function(){this._panL=this._div._left;this._panT=this._div._top;},_pan:function(_3a,_3b){var div=this._div;div._left=this._panL+_3b.x;div._top=this._panT+_3b.y;if(this._map.navigationMode==="css-transforms"){dojo.style(div,esri._css.names.transform,esri._css.translate(div._left,div._top));}else{dojo.style(div,{left:div._left+"px",top:div._top+"px"});}},_extentChange:function(_3c,_3d,_3e){if(_3e){this._redraw(this._map.navigationMode==="css-transforms");}else{if(_3d){this._pan(_3c,_3d);}}var i,_3f=this._standby;if(_3f&&_3f.length){for(i=_3f.length-1;i>=0;i--){this._imageLoaded(null,_3f[i]);_3f.splice(i,1);}}},_redraw:function(_40){if(_40){var _41=this._passive,_42=esri._css.names;dojo.style(_41,_42.transition,"none");this._moveImages(_41,this._active);dojo.style(_41,_42.transform,"none");}var div=this._active||this._div,_43=this._div._left,_44=this._div._top,i,len=div.childNodes.length,_45;for(i=0;i<len;i++){_45=div.childNodes[i];this._setPos(_45,_43,_44);}},_zoom:function(_46,_47,_48){var div=this._div,_49=div._left,_4a=div._top,i,len=div.childNodes.length,_4b;for(i=0;i<len;i++){_4b=div.childNodes[i];var _4c=_4b.e_w*_47,_4d=_4b.e_h*_47,_4e=(_48.x-_49-_4b.e_l)*(_4c-_4b.e_w)/_4b.e_w,_4f=(_48.y-_4a-_4b.e_t)*(_4d-_4b.e_h)/_4b.e_h;_4e=isNaN(_4e)?0:_4e;_4f=isNaN(_4f)?0:_4f;dojo.style(_4b,{left:(_4b.e_l-_4e)+"px",top:(_4b.e_t-_4f)+"px",width:_4c+"px",height:_4d+"px"});}},_zoomStart:function(){this._moveImages(this._active,this._passive);},_moveImages:function(_50,_51){var _52=_50.childNodes,i,len=_52.length;if(len>0){for(i=len-1;i>=0;i--){_51.appendChild(_52[i]);}}},_scale:function(mtx,_53){var css={},_54=esri._css.names,_55=this._passive;dojo.style(_55,_54.transition,_53?"none":(_54.transformName+" "+esri.config.defaults.map.zoomDuration+"ms ease"));css[_54.transform]=esri._css.matrix(mtx);dojo.style(_55,_54.transform,esri._css.matrix(mtx));},_resize:function(_56,_57,_58){dojo.style(this._active,{width:_57+"px",height:_58+"px"});dojo.style(this._passive,{width:_57+"px",height:_58+"px"});}});dojo.extend(esri.layers.MapImage,{visible:true,getLayer:function(){return this._layer;},getNode:function(){return this._node;},show:function(){if(!this.visible){this.visible=true;var _59=this._node,_5a=this._layer,div;if(_59){div=_5a&&_5a._div;if(div){_5a._setPos(_59,div._left,div._top);(_5a._active||div).appendChild(_59);}esri.show(_59);}}},hide:function(){if(this.visible){this.visible=false;var _5b=this._node;if(_5b){esri.hide(_5b);if(_5b.parentNode){_5b.parentNode.removeChild(_5b);}}}}});}