// Copyright © 2006 Replisoft, LLC

// Generic map code

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;
	
	function AddOverlay(n,x,y) {
		if(!this.overlayCount) {
			this.overlayId=new Array();
			this.overlayX=new Array();
			this.overlayY=new Array();
		}
		this.overlayId[this.overlayCount]=n;
		this.overlayX[this.overlayCount]=x;
		this.overlayY[this.overlayCount]=y;
		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);
	}
	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;
		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);
			var y=Math.round(this.overlayY[i]/this.scales[this.curLev]-this.ypos);
			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;
	
	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);
			y=Math.round(y*sc+this.winHeight/2);
			var el=document.getElementById(this.overlayId[i]);
			if(el) {
				el.style.left=x;
				el.style.top=y;
			}
		}
	}
	this.showZoom=showZoom;

	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();",100);


// Bermuda map specific code

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 mapnameIcons=new Array("6I-","5I-","4I-","3-","2-","1-");
var mapnameNoIcons=new Array("6-","5-","4-","3-","2-","1-");

var homex=205;
var homey=80;
var homelev=5;

var bermudaMap=new MapObject("bmap",6,"http://www.informationpages.com/sys/maps/tiles/Lev",mapnameIcons,".gif","http://www.informationpages.com/sys/maps/tiles/Lev6I-0000.gif",200,
				new Array(13200,6600,3400,1800,1000,600),new Array(4600,2400,1200,600,400,200),
				new Array(1,2,4,8,16,32),homex,homey,homelev,true);

bermudaMap.SetZoomSlider("zoomslbmap",17,81,0,11);


var bmap_showicons=true;
function bmap_icons()
{
	bmap_showicons=!bmap_showicons;
	bermudaMap.names=bmap_showicons?mapnameIcons:mapnameNoIcons;
	bermudaMap.ctx=-999; bermudaMap.cty=-999;
	bermudaMap.ScrollMap();
	document.images["bicons"].src=bmap_showicons?"mapimg/iconson.gif":"mapimg/iconsoff.gif";
}
var bmap_showlegend=false;
function bmap_legend()
{
	bmap_showlegend=!bmap_showlegend;
	if(bmap_showlegend) document.getElementById("legendS").style.display="";
	else document.getElementById("legendS").style.display="none";
	document.images["blegend"].src=bmap_showlegend?"mapimg/legendon.gif":"mapimg/legendoff.gif";
}
var bmap_showhelp=false;
function bmap_help()
{
	bmap_showhelp=!bmap_showhelp;
	if(bmap_showhelp) document.getElementById("helpS").style.display="";
	else document.getElementById("helpS").style.display="none";
	document.images["bhelp"].src=bmap_showhelp?"mapimg/helpon.gif":"mapimg/helpoff.gif";
}

function bmap_print()
{
	document.images["bprint"].src="mapimg/printon.gif";
	setTimeout("document.images['bprint'].src='mapimg/printoff.gif';",700);
	if(window.print) window.print();
}

function bmap_zoom(lev)
{
	var iel=document.getElementById("bicons");
	var lel=document.getElementById("blegend");
	if(!iel || !lel) return;
	var o=(lev>2)?67:100;
	if(ie) {
		iel.style.filter="alpha(opacity="+o+")";
		lel.style.filter="alpha(opacity="+o+")";
	} else {
		iel.style.opacity=o/100;
		lel.style.opacity=o/100;
		iel.style.MozOpacity=o/100;
		lel.style.MozOpacity=o/100;
	}
}
bermudaMap.onZoomed=bmap_zoom;

function bmap_scroll(x,y,lev)
{
	var tel=document.getElementById("navtbmap");
	var iel=document.getElementById("navibmap");
	if(!iel || !tel) return;
	x*=bermudaMap.scales[lev];
	y*=bermudaMap.scales[lev];
	var sc=145;
	x=Math.round(bermudaMap.winWidth-105.5+x/sc);
	y=Math.round(53.5+y/sc);
	var x2=x+1+Math.round(bermudaMap.winWidth*bermudaMap.scales[lev]/sc);
	var y2=y+1+Math.round(bermudaMap.winHeight*bermudaMap.scales[lev]/sc);
	if(x<bermudaMap.winWidth-108) x=bermudaMap.winWidth-108;
	if(y<51) y=51;
	if(x2>bermudaMap.winWidth-12) x2=bermudaMap.winWidth-12;
	if(y2>88) y2=88;
	tel.style.left=x;
	tel.style.top=y;
	iel.style.width=x2-x;
	iel.style.height=y2-y;
}
bermudaMap.onScroll=bmap_scroll;

var pmdown=null,pmmove=null,pmup=null;
var psx=0,psy=0;
var bmap_panning=false;
function bmap_pan(event)
{
	if(bmap_panning) 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);
	pmdown=document.onmousedown;
	pmmove=document.onmousemove;
	pmup=document.onmouseup;
	document.onmousedown=donothing;	// prevent text sel in NS
	document.onmousemove=bmap_mmove;
	document.onmouseup=bmap_mup;
	psx=event.clientX;
	psy=event.clientY;
	bmap_panning=true;
	bermudaMap.noDrag=true;
	return false;
}
function bmap_mmove(event)
{
	if(!bmap_panning) return false;
	if(!event) event=window.event;
	var cx=event.clientX,cy=event.clientY;
	var dx=cx-psx,dy=cy-psy;
	psx=cx; psy=cy;
	var f=145/bermudaMap.scales[bermudaMap.curLev];
	dx=Math.round(dx*f);
	dy=Math.round(dy*f);
	bermudaMap.moveby(dx,dy,250);
	return false;
}
function bmap_mup(event)
{
	if(!bmap_panning) return false;
	bmap_panning=false;
	bermudaMap.noDrag=false;
	document.onmousedown=pmdown;	// re-enable text sel in NS
	document.onmousemove=pmmove;
	document.onmouseup=pmup;
	return false;
}

var panmappos=null;

function bmap_panto(event)
{
	if(bmap_panning) return false;
	if(!event) event=window.event;
	if(event.which) { if(event.which!=1) return; }
	else if(event.button!=1) return;
	var mx,my;
	if(ie) {
		mx=event.offsetX;
		my=event.offsetY;
	} else {
		if(!panmappos) panmappos=getPos(document.getElementById("navbmap"));
		mx=event.pageX-panmappos.x; my=event.pageY-panmappos.y;
	}
	var f=145/bermudaMap.scales[bermudaMap.curLev];
	mx=Math.round((mx-8)*f);
	my=Math.round((my-8)*f);
	bermudaMap.moveto(mx,my,350);
	bmap_pan(event);
}

function CreateMap(w,h,overHTML)
{
	document.write("<div style=\"position:relative;width:"+w+";height:"+h+";background-color:#b49520\">");
	document.write("<div style=\"position:absolute;top:1;left:1;z-index:1;width:"+(w-2)+";height:"+(h-2)+";background-color:#e2f4fd;overflow:hidden\">");
	mapObjects['bmap'].RenderMapWindow(null,"position:absolute;top:0;left:0;z-index:1;cursor:move;",w-2,h-2,true);
	if(overHTML) document.write(overHTML);
	document.write("</div>");
	document.write("<img style=\"position:absolute;bottom:5;left:5;z-index:2;cursor:move\" onmousedown=\"return mapObjects['bmap'].mdown(event)\" src=\"mapimg/byplogo.gif\" width=100 height=15 border=0>");

	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>");

	document.write("<a href=\"javascript:void mapObjects['bmap'].zoomin(true)\" title=\"zoom in\"><img alt=\"zoom in\" style=\"position:absolute;top:53;left:14;z-index:12\" src=\"mapimg/zoomplus.gif\" width=23 height=28 border=0></a>");
	document.write("<a href=\"javascript:void mapObjects['bmap'].zoomto(0,true)\" title=\"zoom\"><img name=zoombmap0 alt=\"zoom\" style=\"position:absolute;top:81;left:14;z-index:12\" src=\"mapimg/zoomticktop.gif\" width=23 height=11 border=0></a>");
	document.write("<a href=\"javascript:void mapObjects['bmap'].zoomto(1,true)\" title=\"zoom\"><img name=zoombmap1 alt=\"zoom\" style=\"position:absolute;top:92;left:14;z-index:12\" src=\"mapimg/zoomtickmid.gif\" width=23 height=11 border=0></a>");
	document.write("<a href=\"javascript:void mapObjects['bmap'].zoomto(2,true)\" title=\"zoom\"><img name=zoombmap2 alt=\"zoom\" style=\"position:absolute;top:103;left:14;z-index:12\" src=\"mapimg/zoomtickmid.gif\" width=23 height=11 border=0></a>");
	document.write("<a href=\"javascript:void mapObjects['bmap'].zoomto(3,true)\" title=\"zoom\"><img name=zoombmap3 alt=\"zoom\" style=\"position:absolute;top:114;left:14;z-index:12\" src=\"mapimg/zoomtickmid.gif\" width=23 height=11 border=0></a>");
	document.write("<a href=\"javascript:void mapObjects['bmap'].zoomto(4,true)\" title=\"zoom\"><img name=zoombmap4 alt=\"zoom\" style=\"position:absolute;top:125;left:14;z-index:12\" src=\"mapimg/zoomtickmid.gif\" width=23 height=11 border=0></a>");
	document.write("<a href=\"javascript:void mapObjects['bmap'].zoomto(5,true)\" title=\"zoom\"><img name=zoombmap5 alt=\"zoom\" style=\"position:absolute;top:136;left:14;z-index:12\" src=\"mapimg/zoomtickbot.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:147;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:81;left:17;z-index:13;cursor:n-resize\" src=\"mapimg/zoomslider.gif\" width=17 height=11 border=0>");

	document.write("<a href=\"javascript:void bmap_help()\" title=\"show/hide help\"><img name=bhelp id=bhelp alt=\"show/hide help\" style=\"position:absolute;top:5;left:"+(w-115)+";z-index:12\" src=\"mapimg/helpoff.gif\" width=52 height=16 border=0></a>");
	document.write("<a href=\"javascript:void bmap_print()\" title=\"print the map\"><img name=bprint id=bprint alt=\"print the map\" style=\"position:absolute;top:5;left:"+(w-59)+";z-index:12\" src=\"mapimg/printoff.gif\" width=52 height=16 border=0></a>");
	document.write("<a href=\"javascript:void bmap_icons()\" title=\"show/hide icons\"><img name=bicons id=bicons alt=\"show/hide icons\" style=\"position:absolute;top:25;left:"+(w-115)+";z-index:12\" src=\"mapimg/iconson.gif\" width=52 height=16 border=0></a>");
	document.write("<a href=\"javascript:void bmap_legend()\" title=\"show/hide legend\"><img name=blegend id=blegend alt=\"show/hide legend\" style=\"position:absolute;top:25;left:"+(w-59)+";z-index:12\" src=\"mapimg/legendoff.gif\" width=52 height=16 border=0></a>");

	document.write("<img name=navbmap id=navbmap title=\"click to pan\" style=\"position:absolute;top:46;left:"+(w-115)+";z-index:12;cursor:crosshair;filter:alpha(opacity=90);-moz-opacity:.90;opacity:.90\" onmousedown=\"return bmap_panto(event)\" src=\"mapimg/minimap.gif\" width=108 height=49 border=0>");
	document.write("<table name=navtbmap id=navtbmap border=1 cellpadding=0 cellspacing=0 style=\"position:absolute;top:-100;left:-100;z-index:13;cursor:move;border-width:1px;border-style:solid;border-color:#ff0000;filter:alpha(opacity=70);-moz-opacity:.70;opacity:.70\" onmousedown=\"return bmap_pan(event)\"><tr><td style=\"border-width:0px\"><img alt=\"drag to pan\" title=\"drag to pan\" name=navibmap id=navibmap src=\"mapimg/clr.gif\" width=10 height=10></td></tr></table>");

	document.write("<span name=legendS id=legendS style=\"display:none;position:absolute;top:"+(h-296)+";left:"+(w-368)+";z-index:13\"><img style=\"z-index:14\" src=\"mapimg/legend.gif\" width=361 height=271 border=0><a href=\"javascript:void bmap_legend()\" title=\"hide legend\"><img alt=\"hide legend\" style=\"position:absolute;top:8;left:341;z-index:15\" src=\"mapimg/clr.gif\" width=11 height=11 border=0></a></span>");
	document.write("<span name=helpS id=helpS style=\"display:none;position:absolute;top:"+(h-362)+";left:"+(w-376)+";z-index:13\"><img style=\"z-index:16\" src=\"mapimg/help.gif\" width=369 height=337 border=0><a href=\"javascript:void bmap_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>");
	document.write("</div>");
}

var overlayCount=0;

function CreateOverlay(bname,phone,addr,x,y,sethome,lev)
{
	overlayCount++;
	bermudaMap.AddOverlay("overlay"+overlayCount,x,y);
	if(sethome) {
		bermudaMap.homeX=x;
		bermudaMap.homeY=y;
		bermudaMap.homeLevel=lev||0;
	}

	return "<div id=overlay"+overlayCount+" style=\"position:absolute;top:-200;left:-200;z-index:5\"><a href=\"javascript:void showElement('balloon"+overlayCount+"','balloonx"+overlayCount+"')\" title=\"show listing\"><img alt=\"show listing\" src=\"mapimg/reddot.gif\" width=13 height=13 border=0 style=\"position:absolute;left:-6;top:-6\"></a>"
	+"<table id=balloon"+overlayCount+" cellspacing=0 cellpadding=0 border=0 style=\"position:absolute;top:-14;left:8;width:179;cursor:move;filter:alpha(opacity=93);-moz-opacity:.93;opacity:.93\" ondragstart=\"return false\" onselectstart=\"return false\" onmousedown=\"return mapObjects['bmap'].mdown(event)\">"
	+"<tr><td style=\"background-image:url('mapimg/balpoint.gif')\" width=29><img src=\"mapimg/clr.gif\" width=29 height=1 border=0></td>"
	+"<td style=\"background-image:url('mapimg/baltop.gif');width:150;padding-left:7px;padding-top:7px;padding-right:7px;font-family:arial;font-size:11px;line-height:13px\" width=150><div style='width:136px'><img src=\"mapimg/clr.gif\" width=10 height=8 border=0 align=right><b>"+bname+"</b><br>"+(phone&&phone.length?"Ph. "+phone+"<br>":"")+(addr&&addr.length?addr:"")+"</div></td></tr>"
	+"<tr><td><img src=\"mapimg/clr.gif\" width=29 height=8 border=0></td><td><img src=\"mapimg/balbottom.gif\" width=150 height=8 border=0></td></tr>"
	+"</table><a href=\"javascript:void hideElement('balloon"+overlayCount+"','balloonx"+overlayCount+"')\" title=\"hide listing\"><img id=balloonx"+overlayCount+" alt=\"hide listing\" src=\"mapimg/close.gif\" width=11 height=11 border=0 style=\"position:absolute;left:172;top:-10;z-index:6\"></a></div>";
}
