var intHWLevels = 11;

// marquee specs
var iWidth = 490; //530;
var iHeight = 277; //300;
var vspc=0;
var hspc=0;
        
function print_Map() {
	window.open( document.images['mapimg'].src, 'printmap');
}

function open_legend() {
	window.open( 'legend/legend.htm', 'legend', 'status=no,scrollbars=no,width=337,height=425,resizable=yes, top=2, left=440');
}

function open_help() {
	window.open( 'help/index.html', '_blank', 'status=no,scrollbars=yes,width=500,height=540,resizable=yes, top=2, left=290');
}

function GetDirections() {
	//validate the form
	if ((trim(document.forms[0].FromAddr.value) == "") && (trim(document.forms[0].FromPostalCd.value) == "")) {
		alert("Please enter the address or the zip code.");
		document.forms[0].FromAddr.focus();
		document.forms[0].FromAddr.select();
	} else if (trim(document.forms[0].FromAddr.value) != ""  && trim(document.forms[0].FromPostalCd.value) == "") {
		if (!validateString(document.forms[0].FromCity, "Please enter the city."))
			return;
		if (!validateString(document.forms[0].FromStateProvCd, "Please enter the state."))
			return;
		document.forms[0].RCmd.value = 'Route'; 
		document.forms[0].submit();
	} else {
		document.forms[0].RCmd.value = 'Route'; 
		document.forms[0].submit();
	}
}

// get the layer object called "name"
function getLayer(name) {
	if (document.all) {
		return eval('document.all.' + name);
	} else if (document.layers) {
		return(document.layers[name]);
	} else {
		return document.getElementById(name);
	}
}

// move layer to x,y
function moveLayer(layer, x, y) {		
	if (layer != null) {
	  	if (document.layers) {
    		layer.moveTo(x, y);
	  	} else {
			layer.style.left = x;
			layer.style.top = y;
		}
	}
}

function setLayerSize(layer, x, y, w, h) {
	if (layer != null) {
		w = Math.abs(w);
		h = Math.abs(h);
	  	if (document.layers) {
    		layer.moveTo(x, y);
			layer.resizeBy(w,h);
	  	} else {
			layer.style.left = x;
			layer.style.top = y;
			layer.style.width = w;
			layer.style.height = h;
		}
	}
}

// toggle layer to invisible
function hideLayer(layer) {		
	if (layer != null) {
	  	if (document.layers)
    		layer.visibility = "hide";
		else
			layer.style.visibility = "hidden";
	}
}

// toggle layer to visible
function showLayer(layer) {		
	if (layer != null) {
	  	if (document.layers) {
    		layer.visibility = "show";
		} else {
			layer.style.visibility = "visible";
			layer.style.display = "";
		}
	}
}

// clip layer display to clipleft, cliptip, clipright, clipbottom
function clipLayer(layer, clipleft, cliptop, clipright, clipbottom) {		
	if (layer != null) {
		if (document.layers) {
			layer.clip.left   = clipleft;
			layer.clip.top    = cliptop;
			layer.clip.right  = clipright;
			layer.clip.bottom = clipbottom;
		} else {
			layer.style.clip = "rect(" + cliptop + "px " +  clipright + "px " + clipbottom + "px " + clipleft +"px)";
		}
	}
}

function doMap(act) {
	var f = document.Form;

	var X = iWidth;
	var Y = iHeight;

	var T = parseFloat(f.T.value);
	var B = parseFloat(f.B.value);
	var L = parseFloat(f.L.value);
	var R = parseFloat(f.R.value);

	var stepY = Math.abs(T - B)/Y;
	var stepX = Math.abs(R - L)/X;

	switch (act) {
		case "left":		L = L - stepX * X/4;	R = R - stepX * X/4;	break;
		case "right":		L = L + stepX * X/4;	R = R + stepX * X/4;	break;
		case "up":			T = T + stepY * Y/4;	B = B + stepY * Y/4;	break;
		case "down":		T = T - stepY * Y/4;	B = B - stepY * Y/4;	break;
		case "zoomfull":	T = initT; L = initL; B = initB; R = initR;		break;
	}

	var ext = normExt(new Extent(T, L, B, R));

	f.T.value = ext.T;
	f.L.value = ext.L;
	f.B.value = ext.B;
	f.R.value = ext.R;

	__doPostBack(ext.T, ext.L, ext.B, ext.R, gMode);
}

function setCursor(mode) {
	if( document.layers)
		return;
	if( mode == "zoomin" || mode == "zoomout")
		mapImgStyle.cursor = "crosshair";
	else if( mode == "pan") {
		mapImgStyle.cursor = "pointer";
	} else if( mode == "id")
		mapImgStyle.cursor = "help";
	else
		mapImgStyle.cursor = "default";
}

function Extent(T, L, B, R) {
	this.T = T;
	this.L = L;
	this.B = B;
	this.R = R;                     
}

function normExt(ext) {
	var iRatio = iWidth/iHeight;
	
	var eX = ext.R - ext.L;
	var eY = ext.T - ext.B;
	var D;
	
	cX = ext.R - eX/2; //center coordinates
	cY = ext.T - eY/2;
	
	if( Math.abs(iRatio - ((ext.R-ext.L)/(ext.T-ext.B))) > 0.0001) {
		if( eX < eY || eX < (eY * iRatio)) {
			D = (eY * iRatio - eX) / 2;
			ext.L = ext.L - D;
			ext.R = ext.R + D;
		} else {
			D = (eX/iRatio - eY)/2
			ext.T = ext.T + D;
			ext.B = ext.B - D;
		}
	}
	
	var minStepX = 0.0002817;
	var minStepY = 0.0002815;
	var maxStepX = 17.466;
	var maxStepY = 17.452;
	
	eX = ext.R - ext.L;
	eY = ext.T - ext.B;
	
	if( eX/iWidth < minStepX || eY/iHeight < minStepY) {
		ext.L = cX - (minStepX * iWidth)/2;
		ext.R = cX + (minStepX * iWidth)/2;
		ext.T = cY + (minStepY * iHeight)/2;
		ext.B = cY - (minStepY * iHeight)/2;
	}
	if( eX/iWidth > maxStepX || eY/iHeight > maxStepY) {
		ext.L = cX - (maxStepX * iWidth)/2;
		ext.R = cX + (maxStepX * iWidth)/2;
		ext.T = cY + (maxStepY * iHeight)/2;
		ext.B = cY - (maxStepY * iHeight)/2;
	}
	return ext;
}

var overOverLibLayer = false;
var indices = new Array();
var prevIndices = new Array();
var magnifyIconMin = 25;

var LEFT_BUTTON = 1;

var mouseX=0;
var mouseY=0;
var x1=0;
var y1=0;
var x2=0;
var y2=0;
var zleft=0;
var zright=0;
var ztop=0;
var zbottom=0;

var zooming=false;
var usemap = true;
var ovBoxSize = 1;
var clickedOverlib = false;
var zoomBoxLayer = null;
var overLibLayer = null;
var mapImgStyle = null;

//correction for IE
var add = -2; // no frames
if( window.name == "map") //'map' frame
	add = 0;

function initializeMap(zoomBoxLayerName,overLibLayerName,mapImgName) {
	// Set up event capture for mouse movement
	if (document.layers) {
		document.captureEvents(Event.MOUSEMOVE);
		document.captureEvents(Event.MOUSEDOWN);
		document.captureEvents(Event.MOUSEUP);
	}
	document.onmousemove = getMouse;
	document.onmousedown = mapTool;
	document.onmouseup = mouseUp;
	
	zoomBoxLayer = getLayer(zoomBoxLayerName);
	overLibLayer = getLayer(overLibLayerName);
	mapImgStyle = document.images[mapImgName].style;
}

// check for mouseup
function mouseUp(e) {
	if (usemap && !overOverLibLayer && !clickedOverlib) {	
		var button = document.all ? event.button : e.which;
		
		if (button != LEFT_BUTTON) {
			zooming = false;
			hideZoomBox();
			return;
		}
		
		if (zooming) {
		
			if (mouseX<0)
		 		mouseX = 0;
			if (mouseX>iWidth)
				mouseX = iWidth;
			if (mouseY<0)
				mouseY = 0;
			if (mouseY>iHeight)
				mouseY = iHeight;
			mapTool(e);
			
		} else if (gMode != "zoomin" && gMode != "zoomout") {
		
			getImageXY(e);
			
			if (mouseX >= 0 && mouseX < iWidth && mouseY >= 0 && mouseY < iHeight) {
				zleft = mouseX;
				ztop = mouseY;
				refreshMap();
			}
			
		}
	}
}

function mapTool(e) {
	if (usemap && !overOverLibLayer && !clickedOverlib) {
		var button = document.all ? event.button : e.which;
		
		if (button != LEFT_BUTTON)
			return;
		
		onResize();
		getImageXY(e);
		
		if (!zooming && ((document.all && document.body.style.overflow != 'hidden') || (!document.all && window.scrollbars.visible))) {
			if ((mouseX + hspc) - (document.all ? document.body.scrollLeft : window.pageXOffset) > (document.all ? document.body.offsetWidth : window.innerWidth) - 20)
				return false;
			if ((mouseY + vspc) - (document.all ? document.body.scrollTop : window.pageYOffset) > (document.all ? document.body.offsetHeight : window.innerHeight) - 20)
				return false;
		}
		
		if ( (gMode == "zoomin" || gMode == "zoomout") && !zooming && mouseX >= 0 && mouseX < iWidth && mouseY >= 0 && mouseY < iHeight) {
			startZoomBox(e);
			return false;
		} else if (zooming) {
			getMouse(e);
			stopZoomBox(e);
		}
	}
	return true;
}

function getImageXY(e) {
	if (document.all) {
		mouseX = event.clientX + document.body.scrollLeft + add - hspc;
		mouseY = event.clientY + document.body.scrollTop + add - vspc;
	} else {
		mouseX = e.pageX - hspc;
		mouseY = e.pageY - vspc;
	}
}

// get the coords at mouse position
function getMouse(e) {	 
	if (usemap) {
		getImageXY(e);
		
		if (zooming) {
		
			if (overOverLibLayer)
				killOverlibLayer();
			
			if (mouseX < 0)
		 		mouseX = 0;
			if (mouseX > iWidth)
				mouseX = iWidth;
			if (mouseY < 0)
				mouseY = 0;
			if (mouseY > iHeight)
				mouseY = iHeight;
			x2 = mouseX;
			y2 = mouseY;
			setClip();
			return false;
			
		// we aren't zooming, so let's see if we are hovering over any of the markers
		} else if (mouseOverCoords.length > 0) {
			
			getMousePosition();
			
			mapImgStyle.cursor = "crosshair";
			
			// loop through looking for a match
			indices = new Array();
			for (var i=0; !overOverLibLayer && i < mouseOverCoords.length; i++)
				if ( mouseX >= mouseOverCoords[i].x-6 && mouseX <= mouseOverCoords[i].x+4 && mouseY >= mouseOverCoords[i].y-8 && mouseY <= mouseOverCoords[i].y+3 )
					indices[indices.length] = i;
			
			// if there were any indices we are over, then display
			if (indices.length > 0) {
				if (navigator.userAgent.toLowerCase().indexOf("msie 5.0") != -1)
					mapImgStyle.cursor = "hand";
				else
					mapImgStyle.cursor = "pointer";
				var tempX = hspc + mouseOverCoords[indices[0]].x + 3;
				var tempY = vspc + mouseOverCoords[indices[0]].y - 10;
				
				var indicesChanged = false;
				if (indices.length == prevIndices.length) {
					for (var x=0; !indicesChanged && x < indices.length; x++)
						if (indices[x] != prevIndices[x])
							indicesChanged = true;
				} else indicesChanged = true;
				prevIndices = indices;
				
				// we are over a different dot, so recreate the layer
				if (indicesChanged) {
					var buffer = "";
					for (var i=0; i < indices.length; i++)
						buffer += (i > 0 ? "" : "") + mouseOverCoords[indices[i]].text;
					overOverLibLayer = false;
					overlib(buffer,STICKY,MOUSEOFF,350,FIXX,tempX,FIXY,tempY,BGCOLOR,'#ffffff',FGCOLOR,'#000000');
				}
				
			// we are not over a dot, did we mouse off a dot?
			} else if (prevIndices.length > 0) {
				prevIndices = new Array();
				
				// check if the popup layer is visible
				if (overLibLayer.style.visibility == "visible") {
					// give them 1/3 second to mouse over the layer or back to the same dot or else kill it
					setTimeout('checkIfShouldKillOverlib()',350);
				}
			}
			
		}
	}
	return true;
}

function checkIfShouldKillOverlib() {
	if (indices.length == 0 && !overOverLibLayer)
		killOverlibLayer();
}

function killOverlibLayer() {
	nd(200);
	overOverLibLayer = false;
}

// start zoom in.... box displayed
function startZoomBox(e) {
	//	moveLayer("map",hspc,vspc);
	getImageXY(e);	
	// keep it within the MapImage
	if (zooming) {
		stopZoomBox(e);
	} else if (mouseX < iWidth && mouseY < iHeight) {
		x1=mouseX;
		y1=mouseY
		x2=x1+1;
		y2=y1+1;
		zooming=true;
		setLayerSize(zoomBoxLayer,hspc+x1,vspc+y1,x2-x1,y2-y1);
		showLayer(zoomBoxLayer);
	}
	return false;
}

// stop zoom box display... zoom in/out
function stopZoomBox(e) {
	zooming = false;
	
	if (zright < zleft+2 && ztop < zbottom+2) {
		// if the zoom box is too small
		zright = zleft;
		zbottom = ztop;
	}
	
	refreshMap();
	
	return true;
}

// clip zoom box layer to mouse coords
function setClip() {	
	if (x1 != x2 && y1 != y2) {
		if (x1>x2) {
			zright=x1;
			zleft=x2;
		} else {
			zleft=x1;
			zright=x2;
		}
		
		if (y1>y2) {
			zbottom=y1;
			ztop=y2;
		} else {
			ztop=y1;
			zbottom=y2;
		}
		
		document.getElementById("MapMag").style.display = (zright-zleft < magnifyIconMin) || (zbottom-ztop < magnifyIconMin) ? "none" : "";
		
		setLayerSize(zoomBoxLayer,hspc + zleft,vspc + ztop,zright-zleft,zbottom-ztop);
	}
}

function hideZoomBox() {
	hideLayer(zoomBoxLayer);
}

function getMousePosition() {
	var oMap = document.images['mapimg'];
	if (document.all || document.getElementById) {
		vspc = 0; //custom values
		hspc = 0;
		var element = oMap;
		do {
			vspc += element.offsetTop;
			hspc += element.offsetLeft;
		} while (element = element.offsetParent);
	} else {
		vspc = oMap.y;
		hspc = oMap.x;
	}
}

function onResize() {
	getMousePosition();
	moveLayer(zoomBoxLayer,hspc,vspc);
}

function refreshMap() {
	var f = document.Form;
	var X = iWidth;
	var Y = iHeight;
	
	var T = parseFloat(f.T.value);
	var B = parseFloat(f.B.value);
	var L = parseFloat(f.L.value);
	var R = parseFloat(f.R.value);
	
	var stepY = (T - B)/Y;
	var stepX = (R - L)/X;
	
	f.map_x.value = zleft;
	f.map_y.value = ztop;
	f.map_x2.value = zright;
	f.map_y2.value = zbottom;
	
	switch (gMode) {
		case "pan":
			var pX = L + ((zleft) * stepX);
			var pY = T - ((ztop) * stepY);
			T = pY + Y/2 * stepY;
			L = pX - X/2 * stepX;
			B = pY - Y/2 * stepY;
			R = pX + X/2 * stepX;
			refreshMapPostBack(f,new Extent(T, L, B, R));
			break;
		
		case "zoomin":
			if( zleft != zright || ztop != zbottom) {
				//This is for Box zooming
				L = L + (zleft * stepX);
				R = R - ((X-zright) * stepX);
				T = T - (ztop * stepY);	
				B = B + ((Y-zbottom) * stepY);
			} else {
				//This is for the click zooming
				for (var i=0; i < mouseOverCoords.length; i++) {
					if ( mouseX >= mouseOverCoords[i].x-6 && mouseX <= mouseOverCoords[i].x+4 && mouseY >= mouseOverCoords[i].y-8 && mouseY <= mouseOverCoords[i].y+3 ) {
						self.location.href = arr[i][2];
						return;
					}
				}
				
				var offsetX = (R - L) * ((mouseX / X) - .5);
				var offsetY = (T - B) * ((mouseY / Y) - .5);
				
				L = (L + X * stepX / 4) + offsetX;
				R = (R - X * stepX / 4) + offsetX;
				T = (T - Y * stepY / 4) - offsetY;
				B = (B + Y * stepY / 4) - offsetY;
			}
			refreshMapPostBack(f,new Extent(T, L, B, R));
			break;
		
		case "zoomout":
			if (zleft != zright || ztop != zbottom) {
				var x = zleft + (zright-zleft)/2;
				var y = ztop + (zbottom-ztop)/2;
				
				var zWidth = zright-zleft;
				var zHeight = zbottom-ztop;
				var ZO = zWidth > zHeight?X/zWidth:Y/zHeight;
				ZO = ZO > 9?9:ZO;
				
				var cX = L + (x * stepX);
				var cY = T - (y * stepY);
	
				L = cX - (X/2)*(stepX*ZO);
				R = cX + (X/2)*(stepX*ZO);
				
				T = cY + (Y/2)*(stepY*ZO);
				B = cY - (Y/2)*(stepY*ZO);
			} else {
				var cX = L + (zleft * stepX);
				var cY = T - (ztop * stepY);
				L = cX - (X/2)*(stepX*2);
				R = cX + (X/2)*(stepX*2);
				T = cY + (Y/2)*(stepY*2);
				B = cY - (Y/2)*(stepY*2);
			}
			refreshMapPostBack(f,new Extent(T, L, B, R));
			break;
		
		default:
			f.submit();
	}
}

function refreshMapPostBack(f,ext) {
	ext = normExt(ext);
	f.T.value = ext.T;
	f.L.value = ext.L;
	f.B.value = ext.B;
	f.R.value = ext.R;
	hideZoomBox();
	__doPostBack(ext.T, ext.L, ext.B, ext.R, gMode);
}

function buttonZoom(zoomMode) {
	var f = document.Form;
	var X = iWidth;
	var Y = iHeight;
	
	var T = parseFloat(f.T.value);
	var B = parseFloat(f.B.value);
	var L = parseFloat(f.L.value);
	var R = parseFloat(f.R.value);
	
	var stepY = (T - B) / Y;
	var stepX = (R - L) / X;
	
	f.map_x.value = zleft;
	f.map_y.value = ztop;
	f.map_x2.value = zright;
	f.map_y2.value = zbottom;
	
	switch (zoomMode) {
		case "zoomin":
			L = L + stepX * X / 4;
			R = R - stepX * X / 4;
			T = T - stepY * Y / 4;
			B = B + stepY * Y / 4;
			break;
		
		case "zoomout":
			L = L - stepX * X / 2;
			R = R + stepX * X / 2;
			T = T + stepY * Y / 2;
			B = B - stepY * Y / 2;
			break;
	}
	
	var ext = normExt(new Extent(T, L, B, R));
	f.T.value = ext.T;
	f.L.value = ext.L;
	f.B.value = ext.B;
	f.R.value = ext.R;
	hideZoomBox();
	__doPostBack(ext.T, ext.L, ext.B, ext.R, gMode)
}

function buttonZoomLevel(z) {
	var f = document.Form;
	var X = iWidth;
	var Y = iHeight;
	
	var T = parseFloat(f.T.value);
	var B = parseFloat(f.B.value);
	var L = parseFloat(f.L.value);
	var R = parseFloat(f.R.value);
	
	document.frmListings.ZoomLevel.value = z * 2;
	document.frmListings.CenterX.value = L + ((R - L) / 2);
	document.frmListings.CenterY.value = T - ((T - B) / 2);
	
	if (validateSearchCriteria())
		SubmitForm();
}

function resetMap() {
	var f = document.Form;
	
	// retrieve original map coordinates
	var T = f.T.value = parseFloat(f.original_T.value);
	var B = f.B.value = parseFloat(f.original_B.value);
	var L = f.L.value = parseFloat(f.original_L.value);
	var R = f.R.value = parseFloat(f.original_R.value);
	
	// reset map image src with original coordinates
    document.images['mapimg'].src = setMapImgSrc(T, L, B, R);
}

function __doPostBack(T, L, B, R, zoom) {
	// NE corner map coordinates	
	document.frmGeoMarquee.__top.value = T;
	document.frmGeoMarquee.__right.value = R;
	// SW corner map coordinates
	document.frmGeoMarquee.__bottom.value = B;
	document.frmGeoMarquee.__left.value = L;
	document.frmGeoMarquee.__zoommode.value = zoom;
	document.frmGeoMarquee.__map.value = "";
	
	// copy the searching criteria from the frmListings form to the map form
	if (document.frmListings != null) {
		document.frmListings.__top.value = T
		document.frmListings.__bottom.value = B
		document.frmListings.__left.value = L
		document.frmListings.__right.value = R
		document.frmListings.__map.value = ""
		document.frmListings.__zoommode.value = zoom;
		//document.frmGeoMarquee.Addr.value = document.frmListings.Addr.value;
	}
	
	if (validateSearchCriteria())
		SubmitForm();
}

