// Copyright © 2008 Replisoft, LLC

// Map module

var moz=(navigator.userAgent.toLowerCase().indexOf('mozilla')!=-1);
var ie=(navigator.userAgent.toLowerCase().indexOf('msie')!=-1);
if(ie) moz=false;
var safari=(navigator.userAgent.toLowerCase().indexOf('safari')!=-1);

function donothing() { return false; }

var mapObjects=new Array();
var mapDragging=null;
function mapMouseMoved(event) { if(mapDragging) return mapDragging.mmoved(event); else return false; }
function mapMouseUp(event) { if(mapDragging) return mapDragging.mup(event); else return false; }

function MapObject(onm,levs,base,fname,ext,blk,tsize,w,h,scale,homex,homey,homel,posc)
{
	this.oname=onm;
	mapObjects[onm]=this;
	this.levels=levs||1;
	this.baseName=base||"";
	this.names=fname;
	this.extension=ext||".gif";
	this.blankTile=blk||"clear.gif";
	this.tileSize=tsize||200;
	this.widths=w;
	this.heights=h;
	this.scales=scale||new Array(1,1);
	this.tmaxx=new Array();
	this.tmaxy=new Array();
	this.homeX=homex||0;
	this.homeY=homey||0;
	this.homeLevel=homel||0;
	this.positionCenter=posc||false;
	this.minx=0;
	this.miny=0;
	this.maxx=w[0];
	this.maxy=h[0];
	this.lminx=new Array();
	this.lminy=new Array();
	this.lmaxx=new Array();
	this.lmaxy=new Array();
	this.doubleBuffer=true;
	
	this.curLev=this.homeLevel;
	this.xpos=this.homeX;
	this.ypos=this.homeY;
	this.winWidth=0;
	this.winHeight=0;
	this.movect=0;
	this.endmx=0; this.endmy=0;
	this.dragging=false;
	this.curmx=0; this.curmy=0;
	this.downmx=0; this.downmy=0;
	this.downix=0; this.downiy=0;
	this.dtime=0;
	this.mox=0; this.moy=0; this.mvx=0; this.mvy=0;
	this.edgeTO=null;
	this.mozx=0; this.mozy=0;
	this.pmdown=null; this.pmmove=null; this.pmup=null;
	this.curbuf=0;
	this.ctx=-999; this.cty=-999;
	this.itxsize=0; this.itysize=0;
	this.zoomDragging=false;
	this.dragLev=0;
	this.onZoomed=null;
	this.onScroll=null;
	this.liveZoom=true;
	this.fixSize=new Array(false,false);
	this.goHome=false;
	this.noDrag=false;
	this.onDown=null;
	this.initialized=false;
	
	this.overlayCount=0;
	this.overlayId=null;
	this.overlayX=null;
	this.overlayY=null;
	this.overlayXoff=null;
	this.overlayYoff=null;
	
	function AddOverlay(n,x,y,ox,oy) {
		if(!this.overlayCount) {
			this.overlayId=new Array();
			this.overlayX=new Array();
			this.overlayY=new Array();
			this.overlayXoff=new Array();
			this.overlayYoff=new Array();
		}
		this.overlayId[this.overlayCount]=n;
		this.overlayX[this.overlayCount]=x;
		this.overlayY[this.overlayCount]=y;
		this.overlayXoff[this.overlayCount]=ox||0;
		this.overlayYoff[this.overlayCount]=oy||0;
		this.overlayCount++;
	}
	this.AddOverlay=AddOverlay;

	function SetWindowSize(w,h) {
		this.winWidth=w; this.winHeight=h;
		this.itxsize=Math.ceil(this.winWidth/this.tileSize)+1;
		this.itysize=Math.ceil(this.winHeight/this.tileSize)+1;
	}
	this.SetWindowSize=SetWindowSize;
	
	function mdown(event) {
		if(this.dragging || this.noDrag) return false;
		if(!event) event=window.event;
		if(this.onDown) { if(!this.onDown(event)) return; }
		if(event.which) { if(event.which!=1) return; }
		else if(event.button!=1) return;
		if(window.captureEvents) window.captureEvents(Event.MouseMove|Event.MouseDown|Event.MouseUp);
		this.pmdown=document.onmousedown;
		this.pmmove=document.onmousemove;
		this.pmup=document.onmouseup;
		document.onmousedown=donothing;	// prevent text sel in NS
		document.onmousemove=mapMouseMoved;
		document.onmouseup=mapMouseUp;
		mapDragging=this;
		this.dragging=true;
		this.downix=this.xpos;
		this.downiy=this.ypos;
		this.mox=event.clientX;
		this.moy=event.clientY;
		this.downmx=event.clientX;
		this.downmy=event.clientY;
		this.mvx=0; this.mvy=0;
		this.mmoved(event);
		return false;
	}
	this.mdown=mdown;
	function mmoved(event) {
		if(!event) event=window.event;
		this.curmx=event.clientX;
		this.curmy=event.clientY;
		if(this.dragging) {
			var pdx,pdy;
			if(moz) {
				if(this.edgeTO) { clearTimeout(this.edgeTO); this.edgeTO=null; }
				pdx=this.curmx-this.mox; pdy=this.curmy-this.moy;
			}
			var t=new Date().getTime();
			var dt=t-this.dtime;
			if(dt) {
				var f=dt*.02; if(f>1) f=1;
				this.mvx=this.mvx*(1.0-f)+f*(this.curmx-this.mox)/dt;
				this.mvy=this.mvy*(1.0-f)+f*(this.curmy-this.moy)/dt;
				this.dtime=t;
				this.mox=this.curmx; this.moy=this.curmy;
			}
			var dx=this.curmx-this.downmx,dy=this.curmy-this.downmy;
			this.xpos=this.downix-dx;
			this.ypos=this.downiy-dy;
			this.ScrollMap();
			if(moz) {	// if near edge and moving off, stop dragging - may have problems since in opera&others clientx is doc not window
				var px=this.curmx+pdx,py=this.curmy+pdy;
				pdx=Math.abs(pdx); pdy=Math.abs(pdy); if(pdx<5) pdx=5; if(pdy<5) pdy=5;
				if(px<pdx || px>=self.innerWidth-pdx || py<pdy || py>self.innerHeight-pdy) {
					dt=Math.round(dt*1.2+5); if(dt>500) dt=500; else if(dt<20) dt=20;
					this.edgeTO=setTimeout("mapObjects['"+this.oname+"'].offedge()",dt);
				}
			}
		} else if(this.zoomDragging) {
			if(this.edgeTO) { clearTimeout(this.edgeTO); this.edgeTO=null; }
			this.dragLev=this.curLev;
			var dist=0;
			if(this.zoomVertical) {
				this.dragLev+=(this.curmy-this.downmy)/this.ldy;
				dist=Math.abs(this.curmx-this.downmx);
			} else {
				this.dragLev+=(this.curmx-this.downmx)/this.ldx;
				dist=Math.abs(this.curmy-this.downmy);
			}
			if(this.dragLev<-2 || this.dragLev>this.levels+1 || dist>20) this.edgeTO=setTimeout("mapObjects['"+this.oname+"'].offzoom()",700);
			if(this.dragLev<0) this.dragLev=0; else if(this.dragLev>this.levels-1) this.dragLev=this.levels-1;
			this.onZoom(this.dragLev);
			if(this.liveZoom) this.showZoom(this.dragLev);
		}
		return false;
	}
	this.mmoved=mmoved;
	function mup(event) {
		this.mmoved(event);
		if(this.dragging) this.stopdragging();
		else if(this.zoomDragging) this.stopZoomDragging();
		return false;
	}
	this.mup=mup;
	function stopdragging(inertia)
	{
		if(this.dragging) {
			if(window.releaseEvents) window.releaseEvents(Event.MouseMove|Event.MouseDown|Event.MouseUp);
			document.onmousedown=this.pmdown;	// re-enable text sel in NS
			document.onmousemove=this.pmmove;
			document.onmouseup=this.pmup;
			this.mslide(inertia);
			this.dragging=false;
			mapDragging=null;
		}	
	}
	this.stopdragging=stopdragging;
	function mslide(inertia)
	{
		if(!inertia) inertia=30;
		var f=Math.pow(this.mvx*this.mvx+this.mvy*this.mvy,0.25);
		if(f<0.1) return;
		var dur=Math.round(inertia*2*f);	//3
		var stime=new Date().getTime()-dur-10;
		dx=Math.round(0-this.mvx*inertia*f); dy=Math.round(0-this.mvy*inertia*f);
		if(!this.movect) { this.endmx=this.xpos; this.endmy=this.ypos; }
		this.endmx+=dx; this.endmy+=dy;
		this.movect++;
		setTimeout("mapObjects['"+this.oname+"'].domove("+(dx*2)+","+(dy*2)+","+dx+","+dy+","+stime+","+(dur*2)+","+this.movect+");",10);
	}
	this.mslide=mslide;
	function offedge()
	{
		this.edgeTO=null;
		this.stopdragging(5);
	}
	this.offedge=offedge;
	function offzoom()
	{
		this.edgeTO=null;
		this.stopZoomDragging();
	}
	this.offzoom=offzoom;
	function moveby(dx,dy,dur)
	{
		if(!dur) dur=500;
		var stime=new Date().getTime();
		if(!this.movect) { this.endmx=this.xpos; this.endmy=this.ypos; }
		this.endmx+=dx; this.endmy+=dy;
		this.movect++;
		setTimeout("mapObjects['"+this.oname+"'].domove("+dx+","+dy+",0,0,"+stime+","+dur+","+this.movect+");",10);
		return dur;
	}
	this.moveby=moveby;
	function domove(dx,dy,cx,cy,stime,dur,mid)
	{
		if(this.dragging) { this.movect--; return; }
		var curt=new Date().getTime();
		var t=curt-stime;
		if(t>=dur) {
			this.xpos+=dx-cx; this.ypos+=dy-cy;
			this.ScrollMap();
			this.movect--;
			return;
		}
		var frac=0.5-0.5*Math.cos(3.14159265*t/dur);
		var nx=Math.round(dx*frac);
		var ny=Math.round(dy*frac);
		this.xpos+=nx-cx; this.ypos+=ny-cy;
		if(mid>=this.movect) this.ScrollMap();
		setTimeout("mapObjects['"+this.oname+"'].domove("+dx+","+dy+","+nx+","+ny+","+stime+","+dur+","+mid+");",10);
	}
	this.domove=domove;
	function moveto(x,y,dur)
	{
		if(!this.movect) { this.endmx=this.xpos; this.endmy=this.ypos; }
		if(this.positionCenter) { x-=Math.round(this.winWidth/2); y-=Math.round(this.winHeight/2); }
		var dx=x-this.endmx,dy=y-this.endmy;
		if(dx==0 && dy==0) return;
		if(!dur) dur=Math.round(Math.pow(dx*dx+dy*dy,0.28)*15);
		if(dur>1500) dur=1500;
		return this.moveby(x-this.endmx,y-this.endmy,dur);
	}
	this.moveto=moveto;
	function move(dy,dx)
	{
		this.moveby(Math.round(dx*this.winWidth*0.5),Math.round(dy*this.winHeight*0.5));
	}
	this.move=move;
	function movehome()
	{
		if(this.zoomDragging) return;
		if(this.homeLevel!=this.curLev) {
			this.goHome=true;
			this.zoomto(this.homeLevel,true);
		}
		else this.moveto(this.homeX,this.homeY);
	}
	this.movehome=movehome;
	function zoomto(lev,anim)
	{
		if(lev==this.curLev) return;
		if(this.dragging) this.stopdragging();
		if(this.zoomDragging) return;
		if(anim && this.liveZoom) {
			var stime=new Date().getTime();
			var dur=150+50*Math.abs(this.curLev-lev);
			this.zfirst=true;
			this.zoomDragging=true;
			this.dragging=true;
			setTimeout("mapObjects['"+this.oname+"'].zoomanim("+stime+","+dur+","+lev+");",20);
			return;
		}
		var x=this.xpos+this.winWidth/2;
		var y=this.ypos+this.winHeight/2;
		var f=this.scales[this.curLev]/this.scales[lev];
		this.xpos=Math.round(x*f-this.winWidth/2);
		this.ypos=Math.round(y*f-this.winHeight/2);
		this.ctx=-999; this.cty=-999;
		var plev=this.curLev;
		this.curLev=lev;
		if(this.onZoom) this.onZoom(lev);
		this.ScrollMap();
		if(this.onZoomed) this.onZoomed(lev);
		if(this.goHome) {
			this.goHome=false;
			this.moveto(this.homeX,this.homeY);
		}
	}
	this.zoomto=zoomto;
	function zoomanim(stime,dur,lev)
	{
		var ctime=new Date().getTime()-stime;
		if(ctime>=dur) {
			this.zoomDragging=false;
			this.dragging=false;
			this.fixSize[0]=true;
			this.fixSize[1]=true;
			this.ctx=-999; this.cty=-999;
			this.zoomto(lev,false);
			return;
		}
		var l=this.curLev+(lev-this.curLev)*ctime/dur;
		this.showZoom(l);
		setTimeout("mapObjects['"+this.oname+"'].zoomanim("+stime+","+dur+","+lev+");",20);
	}
	this.zoomanim=zoomanim;
	function zoomin(anim)
	{
		if(this.curLev>0) this.zoomto(this.curLev-1,anim);
	}
	this.zoomin=zoomin;
	function zoomout(anim)
	{
		if(this.curLev<this.levels-1) this.zoomto(this.curLev+1,anim);
	}
	this.zoomout=zoomout;
	function ScrollMap()
	{
		var x1,y1,x2,y2;
		if(this.positionCenter) {
			x1=this.lminx[this.curLev]-Math.round(this.winWidth/2);
			x2=this.lmaxx[this.curLev]-Math.round(this.winWidth/2);
			y1=this.lminy[this.curLev]-Math.round(this.winHeight/2);
			y2=this.lmaxy[this.curLev]-Math.round(this.winHeight/2);
		} else {
			x1=this.lminx[this.curLev];
			x2=this.lmaxx[this.curLev]-this.winWidth;
			y1=this.lminy[this.curLev];
			y2=this.lmaxy[this.curLev]-this.winHeight;
		}
		if(this.xpos<x1) this.xpos=x1;
		if(this.xpos>x2) this.xpos=x2;
		if(this.ypos<y1) this.ypos=y1;
		if(this.ypos>y2) this.ypos=y2;
		var tdx=this.xpos%this.tileSize,tdy=this.ypos%this.tileSize;
		if(tdx<0) tdx+=this.tileSize; if(tdy<0) tdy+=this.tileSize;
		var tx=(this.xpos-tdx)/this.tileSize,ty=(this.ypos-tdy)/this.tileSize;
		if(tx!=this.ctx || ty!=this.cty) {
			this.ctx=tx; this.cty=ty;
			var el=document.getElementById("imgdiv"+this.oname+(this.doubleBuffer?(1-this.curbuf):this.curbuf));
			el.style.left=0-tdx;
			el.style.top=0-tdy;
			this.SetImages(this.ctx,this.cty);
		} else {
			var el=document.getElementById("imgdiv"+this.oname+this.curbuf);
			el.style.left=0-tdx;
			el.style.top=0-tdy;
			el.style.display="";	// for Mac IE
		}
		for(var i=0;i<this.overlayCount;i++) {
			var x=Math.round(this.overlayX[i]/this.scales[this.curLev]-this.xpos)+this.overlayXoff[i];
			var y=Math.round(this.overlayY[i]/this.scales[this.curLev]-this.ypos)+this.overlayYoff[i];
			var el=document.getElementById(this.overlayId[i]);
			if(el) {
				el.style.left=x;
				el.style.top=y;
			}
		}
		if(this.onScroll) this.onScroll(this.xpos,this.ypos,this.curLev);
		return false;
	}
	this.ScrollMap=ScrollMap;
	function SetImages(x,y)
	{
		var bname="img"+this.oname;
		if(this.doubleBuffer) { this.curbuf=1-this.curbuf; bname=bname+this.curbuf; }
		for(var iy=0;iy<this.itysize;iy++) {
			for(var ix=0;ix<this.itxsize;ix++) {
				var nx=x+ix,ny=y+iy;
				document.images[(bname+iy)+ix].src=this.blankTile;
				document.images[(bname+iy)+ix].src=this.TileName(this.curLev,nx,ny);
				if(this.fixSize[this.curbuf]) {
					var el=document.getElementById((bname+iy)+ix);
					el.style.width=this.tileSize;
					el.style.height=this.tileSize;				
				}
			}
		}
		if(this.fixSize[this.curbuf]) {
			el=document.getElementById("imgdiv"+this.oname+this.curbuf);
			el.style.width=this.itxsize*this.tileSize;
			el.style.height=this.itysize*this.tileSize;
			this.fixSize[this.curbuf]=false;
		}
		if(this.doubleBuffer) {
			var el=document.getElementById("imgdiv"+this.oname+this.curbuf);
			el.style.zIndex=2;
			el.style.display="";
			el=document.getElementById("imgdiv"+this.oname+(1-this.curbuf));
			el.style.zIndex=1;
			if(ie || safari || this.dragging)
				el.style.display="none";
			else
				setTimeout("document.getElementById('imgdiv"+this.oname+(1-this.curbuf)+"').style.display='none';",1);
		}
	}
	this.SetImages=SetImages;
	function TileName(lev,x,y)
	{
		if(lev<0 || lev>=this.levels || x<0 || x>=this.tmaxx[lev] || y<0 || y>=this.tmaxy[lev]) return this.blankTile;
		return this.baseName+this.names[lev]+(y<10?"0":"")+y+(x<10?"0":"")+x+this.extension;
	}
	this.TileName=TileName;
	function Initialize(w,h)
	{
		if(this.initialized) return;
		var el=document.getElementById("mapwin"+this.oname);
		if(el) {
			if(!w) w=el.offsetWidth;
			if(!h) h=el.offsetHeight;
		}
		if(!w || !h) {
			if(el) setTimeout("mapObjects['"+this.oname+"'].Initialize()",500);
			return;
		}
		for(var l=0;l<this.levels;l++) {
			this.tmaxx[l]=this.widths[l]/this.tileSize;
			this.tmaxy[l]=this.heights[l]/this.tileSize;
			this.lminx[l]=Math.round(this.minx/this.scales[l]);
			this.lmaxx[l]=Math.round(this.maxx/this.scales[l]);
			this.lminy[l]=Math.round(this.miny/this.scales[l]);
			this.lmaxy[l]=Math.round(this.maxy/this.scales[l]);
		}
		this.curLev=this.homeLevel;
		this.SetWindowSize(w,h);
		this.xpos=this.homeX-Math.round(this.winWidth/2);
		this.ypos=this.homeY-Math.round(this.winHeight/2);
		this.onZoom(this.curLev);
		this.ScrollMap();
		if(this.onZoomed) this.onZoomed(this.curLev);
		this.initialized=true;
	}
	this.Initialize=Initialize;
	
	function Resize(w,h)
	{
		var el=document.getElementById("mapwin"+this.oname);
		if(!el) return;
		el.style.width=w;
		el.style.height=h;
		var owidth=this.winWidth;
		var oheight=this.winHeight;
		this.SetWindowSize(w,h);
		var tdx=this.xpos%this.tileSize,tdy=this.ypos%this.tileSize;
		if(tdx<0) tdx+=this.tileSize; if(tdy<0) tdy+=this.tileSize;
		var html="<div id=imgdiv"+this.oname+"0 style=\"width:"+(this.itxsize*this.tileSize)+";height:"+(this.itysize*this.tileSize)+";position:absolute;"+(this.curbuf==0?"":"display:none;")+"z-index:1;left:"+(0-tdx)+";top:"+(0-tdy)+"\">";
		for(var iy=0;iy<this.itysize;iy++) {
			for(var ix=0;ix<this.itxsize;ix++) {
				var nx=this.ctx+ix,ny=this.cty+iy;
				html+="<img id=img"+this.oname+"0"+iy+""+ix+" name=img"+this.oname+"0"+iy+""+ix+" src='"+this.TileName(this.curLev,nx,ny)+"' width="+this.tileSize+" height="+this.tileSize+" border=0>";
			}
			html+="<br>";
		}
		html+="</div><div id=imgdiv"+this.oname+"1 style=\"width:"+(this.itxsize*this.tileSize)+";height:"+(this.itysize*this.tileSize)+";position:absolute;"+(this.curbuf==1?"":"display:none;")+"z-index:1;left:"+(0-tdx)+";top:"+(0-tdy)+"\">";
		for(var iy=0;iy<this.itysize;iy++) {
			for(var ix=0;ix<this.itxsize;ix++) {
				var nx=this.ctx+ix,ny=this.cty+iy;
				html+="<img id=img"+this.oname+"1"+iy+""+ix+" name=img"+this.oname+"1"+iy+""+ix+" src='"+this.TileName(this.curLev,nx,ny)+"' width="+this.tileSize+" height="+this.tileSize+" border=0>";
			}
			html+="<br>";
		}
		html+="</div>";
		el.innerHTML=html;
		if(this.positionCenter) {
			this.xpos+=(owidth-w)/2;
			this.ypos+=(oheight-h)/2;
		}
		this.ScrollMap();
	}
	this.Resize=Resize;
	
	this.zoomEl="zoomsl"+this.oname;
	this.lev0xpos=0;
	this.lev0ypos=0;
	this.ldx=0;
	this.ldy=0;
	this.zoomVertical=true;
	function SetZoomSlider(zel,l0x,l0y,dx,dy)
	{
		this.zoomEl=zel;
		this.lev0xpos=l0x;
		this.lev0ypos=l0y;
		this.ldx=dx;
		this.ldy=dy;
		this.zoomVertical=(Math.abs(dy)>Math.abs(dx));
	}
	this.SetZoomSlider=SetZoomSlider;
	function onZoom(lev)
	{
		var el=document.getElementById(this.zoomEl);
		if(!el) return;
		el.style.left=Math.round(this.lev0xpos+this.ldx*lev);
		el.style.top=Math.round(this.lev0ypos+this.ldy*lev);
	}
	this.onZoom=onZoom;
	this.zfirst=false;
	function zdown(event) {
		if(this.dragging || this.zoomDragging) return false;
		if(!event) event=window.event;
		if(event.which) { if(event.which!=1) return; }
		else if(event.button!=1) return;
		if(window.captureEvents) window.captureEvents(Event.MouseMove|Event.MouseDown|Event.MouseUp);
		this.pmdown=document.onmousedown;
		this.pmmove=document.onmousemove;
		this.pmup=document.onmouseup;
		document.onmousedown=donothing;	// prevent text sel in NS
		document.onmousemove=mapMouseMoved;
		document.onmouseup=mapMouseUp;
		mapDragging=this;
		this.zoomDragging=true;
		this.mox=event.clientX;
		this.moy=event.clientY;
		this.downmx=event.clientX;
		this.downmy=event.clientY;
		this.mvx=0; this.mvy=0;
		this.zfirst=true;
		this.mmoved(event);
		return false;
	}
	this.zdown=zdown;
	function stopZoomDragging()
	{
		this.zoomDragging=false;
		var lev=Math.round(this.dragLev);
		if(this.liveZoom && Math.abs(lev-this.dragLev)>0.15) {
			this.dragLev=(lev+this.dragLev)/2;
			this.showZoom(this.dragLev);
			setTimeout("mapObjects['"+this.oname+"'].stopZoomDragging()",25);
			return;
		}
		if(window.releaseEvents) window.releaseEvents(Event.MouseMove|Event.MouseDown|Event.MouseUp);
		document.onmousedown=this.pmdown;	// re-enable text sel in NS
		document.onmousemove=this.pmmove;
		document.onmouseup=this.pmup;
		mapDragging=null;
		if(this.liveZoom) {
			this.fixSize[0]=true;
			this.fixSize[1]=true;
			this.ctx=-999; this.cty=-999;
		}
		if(lev!=this.curLev) this.zoomto(lev);
		else {
			this.onZoom(lev);
			if(this.liveZoom) this.ScrollMap();
		}
	}
	this.stopZoomDragging=stopZoomDragging;
	function showZoom(lev)
	{
		var ls=this.scales[Math.floor(lev)];
		if(lev<this.levels-1)
			ls*=Math.pow(this.scales[Math.floor(lev)+1]/ls,lev-Math.floor(lev));
		var sc=this.scales[this.curLev]/ls;
		var tsize=Math.round(this.tileSize*sc);
		sc=tsize/this.tileSize;
		var bname="img"+this.oname;
		if(this.doubleBuffer) { this.curbuf=1-this.curbuf; bname=bname+this.curbuf; }
		var donames=(this.zfirst && this.doubleBuffer);
		this.zfirst=false;
		for(var iy=0;iy<this.itysize;iy++) {
			for(var ix=0;ix<this.itxsize;ix++) {
				if(donames) {
					this.zfirst=false;
					var nx=this.ctx+ix,ny=this.cty+iy;
					document.images[(bname+iy)+ix].src=this.blankTile;
					document.images[(bname+iy)+ix].src=this.TileName(this.curLev,nx,ny);
				}
				el=document.getElementById((bname+iy)+ix);
				el.style.width=tsize;
				el.style.height=tsize;
			}
		}
		var cx=this.xpos+(1-1/sc)*this.winWidth/2;
		var cy=this.ypos+(1-1/sc)*this.winHeight/2;
		cx=Math.round(sc*(this.ctx*this.tileSize-cx));
		cy=Math.round(sc*(this.cty*this.tileSize-cy));
		el=document.getElementById("imgdiv"+this.oname+this.curbuf);
		el.style.left=cx;
		el.style.top=cy;
		el.style.width=this.itxsize*tsize;
		el.style.height=this.itysize*tsize;
		if(this.doubleBuffer) {
			el.style.zIndex=2;
			el.style.display="";
			el=document.getElementById("imgdiv"+this.oname+(1-this.curbuf));
			el.style.zIndex=1;
			if(ie || safari || !this.dragging)
				el.style.display="none";
			else
				setTimeout("document.getElementById('imgdiv"+this.oname+(1-this.curbuf)+"').style.display='none';",1);
		}
		for(var i=0;i<this.overlayCount;i++) {
			
			var x=this.overlayX[i]/this.scales[this.curLev]-(this.xpos+this.winWidth/2);
			var y=this.overlayY[i]/this.scales[this.curLev]-(this.ypos+this.winHeight/2);
			x=Math.round(x*sc+this.winWidth/2)+this.overlayXoff[i];
			y=Math.round(y*sc+this.winHeight/2)+this.overlayYoff[i];
			var el=document.getElementById(this.overlayId[i]);
			if(el) {
				el.style.left=x;
				el.style.top=y;
			}
		}
	}
	this.showZoom=showZoom;

	function MoveOverlay(i,x,y)
	{
		this.overlayX[i]=x;
		this.overlayY[i]=y;
		var x=Math.round(x/this.scales[this.curLev]-this.xpos)+this.overlayXoff[i];
		var y=Math.round(y/this.scales[this.curLev]-this.ypos)+this.overlayYoff[i];
		var el=document.getElementById(this.overlayId[i]);
		if(el) {
			el.style.left=x;
			el.style.top=y;
		}
		
	}
	this.MoveOverlay=MoveOverlay;

	function RenderMapWindow(sclass,sstyle,w,h,setSize)
	{
		sstyle="overflow:hidden;"+(setSize?"width:"+w+";height:"+h+";":"")+(sstyle?sstyle:"");
		document.write("<div id=mapwin"+this.oname+" "+ (sclass?"class=\""+sclass+"\" ":"") + "style=\""+sstyle+"\" onmousedown=\"return mapObjects['"+this.oname+"'].mdown(event)\" ondragstart=\"return false\" onselectstart=\"return false\">");
		if(!w) w=600; if(!h) h=600;
		var itxsize=Math.ceil(w/this.tileSize)+1;
		var itysize=Math.ceil(h/this.tileSize)+1;
		document.write("<div id=imgdiv"+this.oname+"0 style=\"width:"+(itxsize*this.tileSize)+";height:"+(itysize*this.tileSize)+";position:absolute;z-index:1\">");
		for(var iy=0;iy<itysize;iy++) {
			for(var ix=0;ix<itxsize;ix++) {
				document.write("<img id=img"+this.oname+"0"+iy+""+ix+" name=img"+this.oname+"0"+iy+""+ix+" src='"+this.blankTile+"' width="+this.tileSize+" height="+this.tileSize+" border=0>");
			}
			document.write("<br>");
		}
		document.write("</div><div id=imgdiv"+this.oname+"1 style=\"width:"+(itxsize*this.tileSize)+";height:"+(itysize*this.tileSize)+";position:absolute;display:none;z-index:1\">");
		for(var iy=0;iy<itysize;iy++) {
			for(var ix=0;ix<itxsize;ix++) {
				document.write("<img id=img"+this.oname+"1"+iy+""+ix+" name=img"+this.oname+"1"+iy+""+ix+" src='"+this.blankTile+"' width="+this.tileSize+" height="+this.tileSize+" border=0>");
			}
			document.write("<br>");
		}
		document.write("</div></div>");
	}
	this.RenderMapWindow=RenderMapWindow;

	return this;
}

function initMaps()
{
	for(m in mapObjects) mapObjects[m].Initialize();
}

setTimeout("initMaps();",1);


function showElement()
{
	var a=showElement.arguments;
	for(var i=0;i<a.length;i++) {
		var el=document.getElementById(a[i]);
		if(el) el.style.display="";
	}
}

function hideElement()
{
	var a=hideElement.arguments;
	for(var i=0;i<a.length;i++) {
		var el=document.getElementById(a[i]);
		if(el) el.style.display="none";
	}
}

function getPos(el)
{
	var px=0,py=0;
	while(el) {
		px+=el.offsetLeft;
		py+=el.offsetTop;
		el=el.offsetParent;
	}
	return {x:px,y:py};
}


var map_showhelp=false;
function map_help()
{
	map_showhelp=!map_showhelp;
	if(map_showhelp) document.getElementById("helpS").style.display="";
	else document.getElementById("helpS").style.display="none";
	document.images["bhelp"].src=map_showhelp?"mapimg/helpon.gif":"mapimg/helpoff.gif";
}

function map_print()
{
	document.images["bprint"].src="mapimg/printon.gif";
	setTimeout("document.images['bprint'].src='mapimg/printoff.gif';",700);
	if(window.print) window.print();
}

function CreateMap(w,h,overHTML)
{
	document.write("<div id=container style=\"position:absolute;top:0;left:0;z-index:1;width:"+w+";height:"+h+";overflow:hidden\">");
	mapObjects['bmap'].RenderMapWindow(null,"position:absolute;top:0;left:0;z-index:1;cursor:move;width:100%;height:100%",w,h,true);
	if(overHTML) document.write(overHTML);
	document.write("</div>");

	document.write("<span style='z-index:12;position:absolute;top:23;right:0;width:56;height:150'>");
	document.write("<a href=\"javascript:void mapObjects['bmap'].movehome()\" title=\"pan home\"><img alt=\"pan home\" style=\"position:absolute;top:21;left:21;z-index:12\" src=\"mapimg/panhome.gif\" width=9 height=9 border=0></a>");
	document.write("<a href=\"javascript:void mapObjects['bmap'].move(-1,0)\" title=\"pan up\"><img alt=\"pan up\" style=\"position:absolute;top:5;left:18;z-index:12\" src=\"mapimg/panup.gif\" width=15 height=15 border=0></a>");
	document.write("<a href=\"javascript:void mapObjects['bmap'].move(1,0)\" title=\"pan down\"><img alt=\"pan down\" style=\"position:absolute;top:31;left:18;z-index:12\" src=\"mapimg/pandown.gif\" width=15 height=15 border=0></a>");
	document.write("<a href=\"javascript:void mapObjects['bmap'].move(0,-1)\" title=\"pan left\"><img alt=\"pan left\" style=\"position:absolute;top:18;left:5;z-index:12\" src=\"mapimg/panleft.gif\" width=15 height=15 border=0></a>");
	document.write("<a href=\"javascript:void mapObjects['bmap'].move(0,1)\" title=\"pan right\"><img alt=\"pan right\" style=\"position:absolute;top:18;left:31;z-index:12\" src=\"mapimg/panright.gif\" width=15 height=15 border=0></a>");
	document.write("<a href=\"javascript:void mapObjects['bmap'].move(-1,-1)\"><img style=\"position:absolute;top:17;left:17;z-index:13\" src=\"mapimg/clr.gif\" width=4 height=4 border=0></a>");
	document.write("<a href=\"javascript:void mapObjects['bmap'].move(-1,1)\"><img style=\"position:absolute;top:17;left:30;z-index:13\" src=\"mapimg/clr.gif\" width=4 height=4 border=0></a>");
	document.write("<a href=\"javascript:void mapObjects['bmap'].move(1,-1)\"><img style=\"position:absolute;top:30;left:17;z-index:13\" src=\"mapimg/clr.gif\" width=4 height=4 border=0></a>");
	document.write("<a href=\"javascript:void mapObjects['bmap'].move(1,1)\"><img style=\"position:absolute;top:30;left:30;z-index:13\" src=\"mapimg/clr.gif\" width=4 height=4 border=0></a>");

	var zpos=53;
	document.write("<a href=\"javascript:void mapObjects['bmap'].zoomin(true)\" title=\"zoom in\"><img alt=\"zoom in\" style=\"position:absolute;top:"+zpos+";left:14;z-index:12\" src=\"mapimg/zoomplus.gif\" width=23 height=28 border=0></a>");
	var levels=mapObjects['bmap'].levels;
	for(var l=0;l<levels;l++)
		document.write("<a href=\"javascript:void mapObjects['bmap'].zoomto("+l+",true)\" title=\"zoom\"><img name=zoombmap"+l+" alt=\"zoom\" style=\"position:absolute;top:"+(zpos+28+l*11)+";left:14;z-index:12\" src=\"mapimg/zoomtick"+(l==0?"top":l==levels-1?"bot":"mid")+".gif\" width=23 height=11 border=0></a>");
	document.write("<a href=\"javascript:void mapObjects['bmap'].zoomout(true)\" title=\"zoom out\"><img alt=\"zoom out\" style=\"position:absolute;top:"+(zpos+28+levels*11)+";left:14;z-index:12\" src=\"mapimg/zoomminus.gif\" width=23 height=13 border=0></a>");
	document.write("<img name=zoomslbmap id=zoomslbmap alt=\"zoom\" onmousedown=\"return mapObjects['bmap'].zdown(event)\" style=\"position:absolute;top:"+(zpos+28)+";left:17;z-index:13;cursor:n-resize\" src=\"mapimg/zoomslider.gif\" width=17 height=11 border=0>");
	document.write("</span>");
	mobj.SetZoomSlider("zoomslbmap",17,zpos+28,0,11);

	document.write("<a href=\"javascript:void map_help()\" title=\"show/hide help\"><img name=bhelp id=bhelp alt=\"show/hide help\" style=\"position:absolute;top:5;right:5;z-index:12\" src=\"mapimg/helpoff.gif\" width=52 height=16 border=0></a>");
	document.write("<span name=helpS id=helpS style=\"display:none;position:absolute;bottom:10;right:10;z-index:13\"><img style=\"z-index:16\" src=\"mapimg/help.gif\" width=369 height=211 border=0><a href=\"javascript:void map_help()\" title=\"hide help\"><img alt=\"hide help\" style=\"position:absolute;top:8;left:349;z-index:17\" src=\"mapimg/clr.gif\" width=11 height=11 border=0></a></span>");
}


var mappos=null;
function mclick(e)
{
	if(mobj.noDrag || Math.abs(mobj.downmx-mobj.curmx)+Math.abs(mobj.downmy-mobj.curmy)>3) return;
	
	if(!mappos) mappos=getPos(document.getElementById("mcoords"));
	if(!e) e=window.event;
	var x=0,y=0;
	x=e.clientX+document.body.scrollLeft;
	y=e.clientY+document.body.scrollTop;
	x-=mappos.x; y-=mappos.y;
	var sf=mobj.scales[mobj.curLev];
	x=(mobj.xpos+x)*sf;
	y=(mobj.ypos+y)*sf;
	var mind=8*sf,curc=-1;
	/*
	for(i=0;i<advertisers.length;i++) {
		if(!advertisers[i][0] && !advertisers[i][1]) continue;
		var dx=advertisers[i][0]-x; if(dx<0) dx=-dx;
		if(dx>=mind) continue;
		var dy=advertisers[i][1]-y; if(dy<0) dy=-dy;
		if(dy>=mind) continue;
		mind=dx<dy?dy:dx;
		curc=i;
	}
	if(curc>=0) {
		document.getElementById("asel").options[0].selected=true;
		overDot(curc);
		var mdur=mobj.moveto(advertisers[curc][0]/sf,advertisers[curc][1]/sf);
		if(mobj.curLev>0) setTimeout("mobj.zoomto(0,true)",mdur+100);
		mobj.noDrag=true;
		setTimeout("mobj.noDrag=false;",mdur+(mobj.curLev>0?1000:200));
	}
	*/
}

var cpending=-1;
var cto=null;
function mmove(e)
{
	if(!mappos) mappos=getPos(document.getElementById("mcoords"));
	if(!e) e=window.event;
	var x=0,y=0;
	x=e.clientX+document.body.scrollLeft;
	y=e.clientY+document.body.scrollTop;
	x-=mappos.x; y-=mappos.y;
	var sf=mobj.scales[mobj.curLev];
	x=(mobj.xpos+x)*sf;
	y=(mobj.ypos+y)*sf;
	var mind=9*sf,curc=-1;
/*	for(i=0;i<advertisers.length;i++) {
		if(!advertisers[i][0] && !advertisers[i][1]) continue;
		var dx=advertisers[i][0]-x; if(dx<0) dx=-dx;
		if(dx>=mind) continue;
		var dy=advertisers[i][1]-y; if(dy<0) dy=-dy;
		if(dy>=mind) continue;
		mind=dx<dy?dy:dx;
		curc=i;
	}
	if(curc>=0) {
		if(cpending<0) document.getElementById("mcoords").style.cursor="crosshair";
		cpending=curc;
		if(cto) clearTimeout(cto);
		cto=setTimeout("cto=null;if(cpending>=0) { overDot(cpending); document.getElementById('asel').options[0].selected=true; }",200);
	}
	else if(cpending!=-1) {
		document.getElementById("mcoords").style.cursor="move";
		cpending=-1;
	}
	*/
}

function selectChanged()
{
	var s=document.getElementById("asel");
	var v=parseInt(s.options[s.selectedIndex].value);
	if(v<0) return;
	overDot(v);
	if(!advertisers[v][0] && !advertisers[v][1]) return;
	var sf=mobj.scales[mobj.curLev];
	var mdur=mobj.moveto(advertisers[v][0]/sf,advertisers[v][1]/sf);
	if(mobj.curLev>0) setTimeout("mobj.zoomto(0,true)",mdur+100);
	mobj.noDrag=true;
	setTimeout("mobj.noDrag=false;",mdur+(mobj.curLev>0?1000:200));
}

var rto=null;
function resized()
{
	var el=document.getElementById("container");
	el.style.width=document.body.clientWidth;
	el.style.height=document.body.clientHeight;
	if(rto) clearTimeout(rto);
	rto=setTimeout("doresize()",10);
}

function doresize() {
	rto=null;
	var el=document.getElementById("container");
	el.style.width=document.body.clientWidth;
	el.style.height=document.body.clientHeight;
	mobj.Resize(document.body.clientWidth,document.body.clientHeight);
}

var mmoveOverlay="<img id=mcoords style='position:absolute;z-index:4;cursor:move' src='mapimg/clr.gif' width=2000 height=1200 border=0 onmousemove='mmove(event)' ondragstart='return false' onselectstart='return false' onmousedown=\"return mapObjects['bmap'].mdown(event)\" onclick='mclick(event)'>";

function getVar(v)
{
	var q=window.location.search.substring(1);
	var vars=q.split("&");
	for(var i=0;i<vars.length;i++) {
		var pair=vars[i].split("=");
		if(pair[0]==v) return unescape(pair[1]);
	}
	return "";
}

