//<script>
var aims="/servlet/com.esri.esrimap.Esrimap?ServiceName=";

function aimsMap(o,service,left,top,right,bottom,imageUrl){
	aims+=service;
	o.xh=null;
	o.extent={
		minx:left, miny:top, maxx:right, maxy:bottom,
		iminx:left, iminy:top, imaxx:right, imaxy:bottom,
		width:function(){
			return this.maxx-this.minx;
		},
		height:function(){
			return this.maxy-this.miny;
		},
		mapX:function(pixX){
			return this.minx+(pixX+1)/o.clientWidth*this.width();
		},
		mapY:function(pixY){
			return this.miny+(o.clientHeight-pixY-1)/o.clientHeight*this.height();
		},
		pixX:function(mapX){
			return Math.ceil((mapX-this.minx)*o.clientWidth/(this.maxx-this.minx))-1;
		},
		pixY:function(mapY){
			return o.clientHeight-Math.ceil((mapY-this.miny)*o.clientHeight/(this.maxy-this.miny));
		},
		panBy:function(deltax,deltay){
			var dx=deltax*this.width(),dy=deltay*this.height();
			this.zoomTo(this.minx+dx,this.miny+dy,this.maxx+dx,this.maxy+dy);
		},
		zoomBy:function(delta){
			if(delta>0.99)delta=0.99; if(delta<-0.99)delta=-0.99;
			if(delta<0)delta*=2;
			var dx=delta*this.width()/2,dy=delta*this.height()/2;
			this.zoomTo(this.minx+dx,this.miny+dy,this.maxx-dx,this.maxy-dy);
		},
		zoomTo:function(left,top,right,bottom,bg){
			this.minx=left; this.maxx=right;
			this.miny=top;  this.maxy=bottom;
			getMap(bg);
		},
		centerAt:function(x,y,zoomBy){
			var w2=this.width()/2,h2=this.height()/2;
			this.minx=x-w2; this.maxx=x+w2;
			this.miny=y-h2; this.maxy=y+h2;
			if(zoomBy)
				this.zoomBy(zoomBy);
			else
				getMap();
		},
		history:{
			stack:new Array(),stackPos:-1,
			gotMap:function(left,top,right,bottom){
				this.stack[++this.stackPos]={left:left,top:top,right:right,bottom:bottom,bg:o.style.background};
			},
			back:function(){
				if(this.stackPos>0){
					var e=this.stack[--this.stackPos];
					if(e!=null)
					o.extent.zoomTo(e.left,e.top,e.right,e.bottom,e.bg);
				}
			},
			forward:function(){
				if(this.stackPos<this.stack.length){
					var e=this.stack[++this.stackPos];
					if(e)
					o.extent.zoomTo(e.left,e.top,e.right,e.bottom,e.bg);
				}
			}
		}
	};

	o.selBox=function(func){
		var x=event.offsetX,y=event.offsetY,e=event.srcElement;
		while(e!=o){
			x+=e.offsetLeft; y+=e.offsetTop;
			e=e.offsetParent; if(!e)return;
		}
	    o.insertAdjacentHTML("beforeEnd",
							 "<v:rect style='left:"+x+";top:"+y+"'>"+
							 "<v:fill color='gray' opacity='0.25'/><v:stroke dashstyle='dash'/>"+
							 "</v:rect>");
	    var e=o.lastChild, dx=event.x, dy=event.y, 
			ch=new cursorHint(func(e,"hint"));
	    o.setCapture();
	    o.onmousemove=function(){
			event.cancelBubble=true;
	        e.style.posWidth=event.x-dx;
	        e.style.posHeight=event.y-dy;
			ch.show(func(e,"hint"));
	    }
	    o.onmouseup=function(){
			o.releaseCapture();
	        o.onmousemove=o.onmouseup=null;
			try{
				func(e);
			}catch(err){
				alert("?selBox.func: "+err.description);
			}
	        e.removeNode(true);
	        if(ch)ch.removeNode();
	    }
	}
	o.boxZoomer=function(box,a){
		with(box.style){
			var x=posWidth<0?posLeft+posWidth:posLeft,w=Math.abs(posWidth),
				y=posHeight<0?posTop+posHeight:posTop,h=Math.abs(posHeight),
				zin=posWidth>0 && posWidth>0;
			with(map.extent)
				if(w>6 && h>6)
					if(zin)
						return a?"Zoom in":zoomTo(mapX(x),mapY(y),mapX(x+w),mapY(y+h));
					else
						return a?"Zoom out":centerAt(mapX(x+w/2),mapY(y+h/2),(w+h)/(map.clientWidth+map.clientHeight)-1);
				else
					if(o.boxHook)
						return a?o.boxHook(box,a):o.boxHook(box);
					else
						return a?"Center":centerAt(mapX(x),mapY(y));
		}
	}	
	o.arcXML=function(q,callback){
		_trace("request",q);
		var xh=new ActiveXObject("MSXML2.XMLHTTP");
		try{
			xh.open("POST",aims+(q.indexOf("SPATIALQUERY")==-1?"":"&CustomService=Query"),callback!=null);
			if(callback)xh.onreadystatechange=callback;
			xh.send(newXML("<?xml version='1.0' encoding='utf-8'?><ARCXML version='1.1'><REQUEST>"+q+"</REQUEST></ARCXML>"));
		}catch(err){
			alert("?arcXML: "+err.description);
		}
		if(callback){
			_trace("response","[async]");
			return xh;
		}else{
			var x=xh2x(xh);
			_trace("response",x?x.xml:"null");
			return x;
		}

	}
	function xh2x(xh){
		if(!xh || xh.status==0)return;
		var x=newXML();
		if(xh.status==200){
			if(!x.load(xh.responseBody))
				alert("?arcXML parseError: "+x.parseError.reason+"\n"+xh.responseText);
		}else
			alert("?arcXML: status="+xh.statusText+"\n"+xh.responseText);
		//_info(x.xml);
		return x;
	}
	//
	//
	//
	o.getMap=function getMap(bg){
		if(o.clientWidth<32||o.clientHeight<32)return;
		if(bg){
			o.style.background=bg;
			return notify();
		}
		if(o.xh)o.xh.abort();
		if(o.tmw){
			clearTimeout(o.tmw);
			showWait();
		}else
			o.tmw=setTimeout(showWait,500);
		window.status="Querying map server...";
		var env="<ENVELOPE minx='@1' maxx='@2' miny='@3' maxy='@4'/>";
		with(o.extent)
			if(minx!=null)
				env=_f(env,minx,maxx,miny,maxy);
			else if(iminx!=null)
				env=_f(env,iminx,imaxx,iminy,imaxy);
			else
				env="";
		o.xh=o.arcXML(_f("<GET_IMAGE><PROPERTIES>"+env+
				(window.imgLegend?"<LEGEND/>":"")+(o.extra?o.extra:"")+
				"<IMAGESIZE width='@1' height='@2'/></PROPERTIES></GET_IMAGE>",
				o.clientWidth,o.clientHeight),parseImage);
		function parseImage(){
			if(!o.xh || o.xh.readyState!=4)return;
			var x=xh2x(o.xh); o.xh=null; if(!x)return;
			try{
				_trace("response",x?x.xml:"null");
				if(n=x.selectSingleNode("/ARCXML/RESPONSE/IMAGE")){
					var env=n.selectSingleNode("ENVELOPE"),
						url=n.selectSingleNode("OUTPUT/@url");
					o.style.background="no-repeat 0 0 url("+(url?url.text:"i/maperr.gif")+")";
					if(window.imgLegend)
						if(url=n.selectSingleNode("LEGEND/@url"))
							imgLegend.src=url.text;
					if(env)
						with(o.extent){
							minx=parseFloat(env.getAttribute("minx"));
							maxx=parseFloat(env.getAttribute("maxx"));
							miny=parseFloat(env.getAttribute("miny"));
							maxy=parseFloat(env.getAttribute("maxy"));
							history.gotMap(minx,miny,maxx,maxy);
						}
				}else{
					o.style.background="no-repeat center center url(i/maperr.gif)";
					if(n=x.selectSingleNode("//ERROR"))
						try{popHint("stop:"+n.text,0,o,0,0);}catch(e){alert(e.description)}
					else
						alert("?arcXML?\n"+x.xml);
				}
				notify();
			}catch(err){
				alert("?parseImage: "+err.description);
			}
		}
		function notify(){
			window.status="";
			clearTimeout(o.tmw); o.tmw=null;
			showWait("off");
			try{
				window[o.id+"_gotMap"]();
			}catch(err){
				//alert(o.id+"_gotMap(): "+err.description);
			}
		}
		function showWait(off){
			o.all.mapWait.style.display=off?'none':'';
			o.tmw=null;
		}
	}
	o.onresize=function(){
		clearTimeout(this.tmrz);
		this.tmrz=setTimeout(reSize,250);
	}
	o.onselectstart=shutup;
	o.oncontextmenu=function(){
		o.ctx={x:event.offsetX,y:event.offsetY}
		popMenu(o.popmenu?o.popmenu:o.id+".pop:CENTER_HERE|ZOOM_IN|ZOOM_OUT|-|PREVIOUS_EXTENT|NEXT_EXTENT|-|FULL_EXTENT");
	}
	o.pop_ACTION=function(a){
		if(!o.ctx)return;
		with(o.extent){
			var x=mapX(o.ctx.x),y=mapY(o.ctx.y);
			switch(a){
				case "CENTER_HERE":
					centerAt(x,y);
					break;
				case "ZOOM_IN":
					zoomBy(0.5);
					break;
				case "ZOOM_OUT":
					zoomBy(-0.5);
					break;
				case "PREVIOUS_EXTENT":
					history.back();
					break;
				case "NEXT_EXTENT":
					history.forward();
					break;
				case "FULL_EXTENT":
					zoomTo();
					break;
			}
		}
		o.ctx=null;
	}
	function reSize(nomap){
		if(o.lastWidth==o.clientWidth && o.lastHeight==o.clientHeight)return;
		with(o.all){
			pan[0].style.posLeft=pan[2].style.posLeft=o.clientWidth/2-12;
			pan[1].style.posTop=pan[3].style.posTop=o.clientHeight/2-8;
			mapWait.style.posLeft=o.clientWidth/2-64;
			mapWait.style.posTop=o.clientHeight/2-16;
		}
		o.lastWidth=o.clientWidth;
		o.lastHeight=o.clientHeight;
		if(!nomap)getMap();
	}
	function _trace(src,s){
		if(RegExp(src,"i").test(o.debug))_info(src+": "+s);
	}
	function _pan(a,s){	
		var e=_i("<v:shape id=pan "+a+" style='position:absolute;z-index:100;width:24;height:16;cursor:hand;"+s+"' "+
				 "coordsize='100,75' fillcolor='red' strokecolor='white' "+
				 "path='m50,0 l100,50 75,50 75,75 25,75 25,50 0,50 xe'>"+
				 "<v:fill opacity='.25'/></v:shape>");
		e.onmouseover=e.onmouseout=function(){
			this.firstChild.Opacity=event.type=="mouseover"?1:0.25;
		}
		e.onmousedown=function(){
			if(event.button!=1)return;
			event.cancelBubble=true;
			o.extent.panBy(e.dx,e.dy);
		}
		e.oncontextmenu=function(){
			event.cancelBubble=true;
			return false;
		}
	}
	function _i(s){
		o.insertAdjacentHTML("beforeEnd",s);
		return o.lastChild;
	}

	_i("<span id=mapWait style='position:absolute;z-index:100;background:infobackground;color:red;border:2 outset;padding:14 18;display:none'>"+
		"<img src='i/w.gif' align=absmiddle>&nbsp;Loading Map...</span>");
	_pan("dx=0 dy=0.4 title='Pan North'","left:-100;top:2;rotation:0");
	_pan("dx=0.4 dy=0  title='Pan East'", "right:1;top:-100;rotation:90");
	_pan("dx=0 dy=-0.4  title='Pan South'","left:-100;bottom:0;rotation:180");
	_pan("dx=-0.4 dy=0 title='Pan West'", "left:0;top:-100;rotation:270");
	reSize("nomap");
	if(imageUrl)
		o.style.background="#f0f0f0 no-repeat center center url("+imageUrl+")";
}
//</script>