var intHWLevels = 11;

// marquee specs
 var iWidth = nMapWidth; //530; //
 var iHeight = nMapHeight; //300; //
/* var iWidth = 550; //530; */
/* var iHeight = 320; //300; */
var vspc=0;
var hspc=0;
        
// get the layer object called "name"
//2
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
//13
function moveLayer(layer, x, y) {
// alert("moveLayer");		 //
	if (layer != null) {
	  	if (document.layers) {
    		layer.moveTo(x, y);
	  	} else {
			layer.style.left = x;
			layer.style.top = y;
		}
	}
}

//u
function setLayerSize(layer, x, y, w, h) {
// alert("setLayerSize"); //
	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
//u
function hideLayer(layer) {
// alert("hideLayer");		 //
	if (layer != null) {
	  	if (document.layers)
    		layer.visibility = "hide";
		else
			layer.style.visibility = "hidden";
	}
}

// toggle layer to visible
//u
function showLayer(layer) {
// alert("showLayer");		 //
	if (layer != null) {
	  	if (document.layers) {
    		layer.visibility = "show";
		} else {
			layer.style.visibility = "visible";
			layer.style.display = "";
		}
	}
}

//3
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";
}

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

//1
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
//u
function mouseUp(e) {
// alert("mouseUp"); //
	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();
			}
			
		}
	}
}

//9
function mapTool(e) {
// alert("mapTool"); //
	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;
}

//5
function getImageXY(e) {
// alert("getImageXY"); //
	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
//4
function getMouse(e) {
// alert("getMouse");	  //
	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 )
//This is where we are hovering over a listing point
					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 & "" & HTML Content					
						buffer += (i > 0 ? "" : "") + mouseOverCoords[indices[i]].text;
					overOverLibLayer = false;
//fnct overlib is in mapSearchJS2.js and shows layer
					overlib(buffer,STICKY,MOUSEOFF,350,FIXX,tempX,FIXY,tempY,BGCOLOR,'#CCCCCC',FGCOLOR,'#EFEFEF');
				}
				
			// we are not over a listing, did we mouse off a listing?
			} 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;
}

//7
function checkIfShouldKillOverlib() {
// alert("checkIfShouldKillOverlib"); //
	if (indices.length == 0 && !overOverLibLayer)
		killOverlibLayer();
}

//8
function killOverlibLayer() {
// alert("killOverlibLayer"); //
	nd(200);
	overOverLibLayer = false;
}

// start zoom in.... box displayed
//11
function startZoomBox(e) {
// alert("startZoomBox"); //
	//	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
//14
function stopZoomBox(e) {
// alert("stopZoomBox"); //
	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
//12
function setClip() {
// alert("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);
	}
}

//u
function hideZoomBox() {
// alert("hideZoomBox"); //
	hideLayer(zoomBoxLayer);
}

//6
function getMousePosition() {
// alert("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;
	}
}

//10
function onResize() {
// alert("onResize"); //
	getMousePosition();
	moveLayer(zoomBoxLayer,hspc,vspc);
}

//u
function refreshMap() {
	var f = document.choiceForm;
	
	f.map_x.value = zleft;
	f.map_y.value = ztop;
	f.map_x2.value = zright;
	f.map_y2.value = zbottom;
	
	if (Math.floor(zleft + (zright - zleft)/2) > 0 && Math.floor(ztop + (zbottom - ztop)/2)) {
	document.choiceForm["map.x"].value = Math.floor(zleft + (zright - zleft)/2);
	document.choiceForm["map.y"].value = Math.floor(ztop + (zbottom - ztop)/2);
	}
	
	if( zleft != zright || ztop != zbottom) {
var mBreak = 1;			
	} else {
		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 ) {
				return;
			}
		}
		if (Math.floor(mouseX) > 0 && Math.floor(mouseY) > 0) {
document.choiceForm["map.x"].value = Math.floor(mouseX);
document.choiceForm["map.y"].value = Math.floor(mouseY);
		}
	}
refreshMapPostBack();
}

//u
function refreshMapPostBack() {
	hideZoomBox();
	SubmitForm();
}

function SubmitForm() {
/* alert(document.choiceForm.action.value); */
	document.choiceForm.action = 'mls.search';
	Loading();
	document.choiceForm.submit();
}
