var originalOffsetTop=0;
var originalOffsetLeft=0;
var stateSaved = "";
var state = "";
var mouseX=0;
var mouseY=0;
var x1=0;
var y1=0;
var x2=0;
var y2=0;
var zminx=0;
var zmaxx=0;
var zmaxy=0;
var zminy=0;
var zoomBoxWidth = 1;
var zooming=false;
var panning=false;
var bottomBorderHeight = 0;
var isIE = false;
var isNav = (navigator.appName.indexOf("Netscape")>=0);
var isNav4 = false;
var isIE4 = false;
var is5up = false;
var isMac = false;
var isWin = false;
var hspc = 0;
var vspc = 0;
	if (navigator.appName.indexOf("Netscape")>=0) {hspc=4;vspc =4;} 
var sMapProcessor;
var sIDProcessor;
var sImage;
//var iWidth='300';
//var iHeight='250';
var minx;
var maxx;
var maxy;
var miny;
if (isNav) {
	if (parseFloat(navigator.appVersion)<5) {
		isNav4=true;
	} else {
		is5up = true;
	}
} else {
	isIE4=true;
	isIE=true;
	if (navigator.appVersion.indexOf("MSIE 5")>0) {
		isIE4 = false;
		is5up = true;
	}
	if (navigator.appVersion.indexOf("MSIE 6")>0) {
		isIE4 = false;
		is5up = true;
	}
	if (navigator.appVersion.indexOf("MSIE 7")>0) {
		isIE4 = false;
		is5up = true;
	}
}	
if (navigator.userAgent.indexOf("Win") >= 0)
	isWin = true;
else
	isMac = true;
function chkMouseUp(e) {
	if (zooming || panning) {
		if (mouseX<0)
		 	mouseX = 0;
		if (mouseX>iWidth)
			mouseX = iWidth;
		if (mouseY<0)
			mouseY = 0;
		if (mouseY>iHeight)
			mouseY = iHeight;
		mapTool(e);
	}
}
function getScrollY() {
  scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
  } else if( document.documentElement &&
      ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
  }
  return scrOfY;
}
function getScrollX() {
  var scrOfX = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement &&
      ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfX = document.documentElement.scrollLeft;
  }
  return scrOfX;
}
function getImageXY(e) {
	if (isNav) {
		mouseX=e.pageX;
		mouseY=e.pageY;
	} else {
		var sX=getScrollX();
		var sY=getScrollY();
		mouseX=event.clientX + sX;
		mouseY=event.clientY + sY;
//		mouseX=event.clientX + document.body.scrollLeft;
//		mouseY=event.clientY + document.body.scrollTop;
	}
	mouseX = mouseX - originalOffsetLeft;
	mouseY = mouseY - originalOffsetTop;
}
function getOriginalOffset(){
	var lyr = getLayerObject(mapImgName);
	originalOffsetTop = lyr.offsetTop;
	originalOffsetLeft = lyr.offsetLeft;
	thingy = lyr;
	while( thingy.offsetParent )
	{	
		thingy = thingy.offsetParent;				
		originalOffsetTop += thingy.offsetTop;
		originalOffsetLeft += thingy.offsetLeft;
	//alert('top: ' + originalOffsetTop);
	}
}
function getMouse(e) {
	window.status="";
	getImageXY(e);
	if (zooming) {
		if (mouseX<0)
		 	mouseX = 0;
		if (mouseX>iWidth)
			mouseX = iWidth;
		if (mouseY<0)
			mouseY = 0;
		if (mouseY>iHeight-bottomBorderHeight)
			mouseY = iHeight-bottomBorderHeight;
		x2=mouseX;
		y2=mouseY;
		setClip();
		return false;
	} else if (panning) {
		x2=mouseX;
		y2=mouseY;
		panMouse();	
		return false;
	} else
	return true;
}
function hideZoomBox() {
	crshmRubberBandLayer.clear();
}
function mapTool (e) {
	getImageXY(e);
//	alert(mouseX.toString() + ":" + mouseY.toString());
	if ((!zooming) && (!panning) &&
		(mouseX >= 0) && (mouseX <= iWidth) &&
		(mouseY >= 0) && (mouseY <= iHeight)) {
		if (state == "PanOnDrag")
			startPan(e);
		else if( state == "ZoomToWindow" )
			startZoomBox(e);
		else if (state == "ZoomOut" || state == "ZoomIn" || state == "ElementInformation")
	    	refreshMap(state);
		return false;
	} else if ((!zooming) && (!panning) && (mouseY > 0) &&
		((mouseX < 0) || (mouseX > iWidth) || (mouseY > iHeight))) {
		setNoActiveCRSMapCommand();
	} else if (zooming) {
//		alert(mouseX.toString() + ":" + mouseY.toString());
		if( checkMouse(mouseX, mouseY) ){
			getMouse(e);
			stopZoomBox(e);
		}
	} else if (panning) {
		if( checkMouse(mouseX, mouseY) ){
			getMouse(e);
			stopPan(e);
		}
	}
	return true;
}
function checkMouse(mouseX, mouseY) {
	return true;
	if( state != '' )
	{
		if( (mouseX <= -5) || (mouseX > (iWidth+5)) ||
		    (mouseY <= -5) || (mouseY > (iHeight+5)) ) {
			setNoActiveCRSMapCommand();
			return false;
		}
	}
	return true;
}
function panMouse() {
	var xMove = x2-x1;
	var yMove = y2-y1;
	var cLeft = -xMove;
	var cTop = -yMove;
	var cRight = iWidth;
	var cBottom = iHeight;
	if (xMove>0) {
		cLeft = 0;
		cRight = iWidth - xMove;
	}
	if (yMove>0) {
		cTop = 0;
		cBottom = iHeight - yMove;
	}
	clipLayer(mapImgName + "_theMap",cLeft,cTop,cRight,cBottom);
	theLayer = getLayerObject(mapImgName + '_theMapSelection');
	if( theLayer ) clipLayer(mapImgName + "_theMapSelection",cLeft,cTop,cRight,cBottom);
	moveLayer(mapImgName + "_theMap",xMove+hspc,yMove+vspc);
	if( theLayer ) moveLayer(mapImgName + "_theMapSelection",xMove+hspc,yMove+vspc);
	return false;
}
function recenter(e) {
		hideZoomBox();
}
function refreshMap(sCommand) {
	if( state == '' ) return;
	var mapContainerWidth = $('#divMapContainer').width() - 7;
	var hostWidth = $('#divMapContainerHost').width();
    var diff = mapContainerWidth - hostWidth;
   	var theform = document.forms['Form1'];
    if (!theform) {
        theform = document.Form1;
		}
	if( sCommand == 'PanOnDrag' ){
		eval("theform." + mapImgName + "_command.value = '" + sCommand + "';");
		eval("theform." + mapImgName + "_input.value = x1 + ',' + y1 + ',' + x2 + ',' + y2;");
		eval("__doCRSMapPostBack(mapImgName);");	
	}else if( sCommand == 'ZoomIn' || sCommand == 'ZoomOut' || sCommand == 'ElementInformation'){
		if( (sCommand == 'ZoomIn' || sCommand == 'ZoomOut' ) && (diff > 0) )	
		{
			try
			{
			mouseX += diff;
			}
			catch(err){}
		}
		eval("theform." + mapImgName + "_command.value = '" + sCommand + "';");
		eval("theform." + mapImgName + "_input.value = mouseX + ',' + mouseY;");
		eval("__doCRSMapPostBack(mapImgName);");	
	}else if( sCommand == 'ZoomToWindow' ) {
		hideZoomBox();
		var temp1 = x1;
		var temp2 = x2;
		if( x2 < x1 ){
			x1 = temp2;
			x2 = temp1;
		}
		temp1 = y1;
		temp2 = y2;
		if( y2 < y1 ){
			y1 = temp2;
			y2 = temp1;
		}
		if( diff > 0 )
		{
			try
			{
				var xDiff = x2-x1;
//				alert('x1:' + x1 + ' x2:' + x2);
				if( xDiff == 0 )
				{
					x1 += (diff/4);
					x2 = x1;
				}else{
//					alert(diff);
//					alert(xDiff);
//					alert(xDiff * (xDiff/diff));
					x2 += (diff * (xDiff/diff));
				}
//				alert('x1:' + x1 + ' x2:' + x2);
			}
			catch(err){}
		}
		eval("theform." + mapImgName + "_command.value = '" + sCommand + "';");
		eval("theform." + mapImgName + "_input.value = x1 + ',' + y1 + ',' + x2 + ',' + y2;");
		eval("__doCRSMapPostBack(mapImgName);");	
	}else if( sCommand == 'ZoomPrevious' || sCommand == 'ZoomNext' || sCommand == 'ZoomHome' || sCommand == 'ZoomLevel1' || sCommand == 'ZoomLevel2' || sCommand == 'ZoomLevel3' || sCommand == 'ZoomLevel4' || sCommand == 'ZoomLevel5'){
		eval("theform." + mapImgName + "_command.value = '" + sCommand + "';");
		eval("theform." + mapImgName + "_input.value = 'something'");
		eval("__doCRSMapPostBack(mapImgName);");	
	}else if( sCommand == 'ElementInformationByArea' ){
		eval("theform." + mapImgName + "_command.value = '" + sCommand + "';");
		eval("theform." + mapImgName + "_input.value = ''");
		eval("__doCRSMapPostBack(mapImgName);");	
	}
}
function setNoActiveCRSMapCommand() {
   	var theform = document.forms['Form1'];
        if (!theform) theform = document.Form1;
	eval("theform." + mapImgName + "_command.value = '';");
	eval("theform." + mapImgName + "_input.value = ''");
	setCRSMapCommandState('');
	eval("crsmap_NoActiveButtons();");
}
function setClip() {
	if (x1>x2) {
		zmaxx=x1;
		zminx=x2;
	} else {
		zminx=x1;
		zmaxx=x2;
	}
	if (y1>y2) {
		zminy=y1;
		zmaxy=y2;
	} else {
		zmaxy=y1;
		zminy=y2;
	}
	if (x1>x2) {
		tempX=x2;
		xWidth = x1 - x2;
	} else {
		tempX=x1;
		xWidth = x2 - x1;
	}
	if (y1>y2) {
		tempY=y2;
		yHeight = y1 - y2;
	} else {
		tempY=y1;
		yHeight = y2 - y1;
	}
	if ((x1 != x2) && (y1 != y2)) {
		crshmRubberBandLayer.clear();
		crshmRubberBandLayer.setColor(crshmRubberBandLayerOutlineColor);
		crshmRubberBandLayer.setOpacity(crshmRubberBandLayerOutlineOpacity);
		crshmRubberBandLayer.drawRect(tempX-1,tempY-1,xWidth-1,yHeight-1);
		crshmRubberBandLayer.paint();
		if( crshmRubberBandLayerFillOpacity > 0 ) {
			crshmRubberBandLayer.setColor(crshmRubberBandLayerFillColor);
			crshmRubberBandLayer.setOpacity(crshmRubberBandLayerFillOpacity);
			crshmRubberBandLayer.fillRect(tempX-1,tempY-1,xWidth-1,yHeight-1);
			crshmRubberBandLayer.paint();
		}
	}
}
function setExtent(_minx,_miny,_maxx,_maxy) {
	minx = _minx;
	miny = _miny;
	maxx = _maxx;
	maxy = _maxy;
}
function setCRSMapCommandState(newState) {
	state = newState;
	vspc=0;
	hspc=0;
    if (state == "PanOnDrag") {
		theLayer = getLayerObject(mapImgName + '_theMap');
		if( theLayer ) theLayer.style.cursor = "move";
		theLayer = getLayerObject(mapImgName + '_theMapRubberBand');
		if( theLayer ) theLayer.style.cursor = "move";
		theLayer = getLayerObject(mapImgName + '_theHotAreaMap');
		if( theLayer ) theLayer.style.cursor = "move";
		vspc=4;
		hspc=4;
		}
	else if (state == "ZoomToWindow") {
		theLayer = getLayerObject(mapImgName + '_theMap');
		if( theLayer ) theLayer.style.cursor = "crosshair";
		theLayer = getLayerObject(mapImgName + '_theMapRubberBand');
		if( theLayer ) theLayer.style.cursor = "crosshair";
		theLayer = getLayerObject(mapImgName + '_theHotAreaMap');
		if( theLayer ) theLayer.style.cursor = "crosshair";
		}
	else if (state == "ZoomIn" || state == "ZoomOut") {
		theLayer = getLayerObject(mapImgName + '_theMap');
		if( theLayer ) theLayer.style.cursor = "crosshair";
		theLayer = getLayerObject(mapImgName + '_theMapRubberBand');
		if( theLayer ) theLayer.style.cursor = "crosshair";
		theLayer = getLayerObject(mapImgName + '_theHotAreaMap');
		if( theLayer ) theLayer.style.cursor = "crosshair";
		}
	else if (state == "ElementInformation") {
		theLayer = getLayerObject(mapImgName + '_theMap');
		if( theLayer ) theLayer.style.cursor = "help";
		theLayer = getLayerObject(mapImgName + '_theMapRubberBand');
		if( theLayer ) theLayer.style.cursor = "help";
		theLayer = getLayerObject(mapImgName + '_theHotAreaMap');
		if( theLayer ) theLayer.style.cursor = "help";
		}
	else if (state == "ClientElementInformation") {
		theLayer = getLayerObject(mapImgName + '_theMap');
		if( theLayer ) theLayer.style.cursor = "help";
		theLayer = getLayerObject(mapImgName + '_theMapRubberBand');
		if( theLayer ) theLayer.style.cursor = "help";
		theLayer = getLayerObject(mapImgName + '_theHotAreaMap');
		if( theLayer ) theLayer.style.cursor = "help";
		}
	else if ( state == "ZoomLevel1" || state == "ZoomLevel2" || state == "ZoomLevel3" || state == "ZoomLevel4" || state == "ZoomLevel5" ){
		panning=false;
		zooming=false;
		hideZoomBox();
		refreshMap(state);	
		}
	else if ( state == "ZoomHome" || state == "ZoomPrevious" || state == "ZoomNext"){
		panning=false;
		zooming=false;
		hideZoomBox();
		refreshMap(state);	
		}
	else if (state == "") {
		theLayer = getLayerObject(mapImgName + '_theMap');
		if( theLayer ) theLayer.style.cursor = "crosshair";
		theLayer = getLayerObject(mapImgName + '_theMapRubberBand');
		if( theLayer ) theLayer.style.cursor = "crosshair";
		theLayer = getLayerObject(mapImgName + '_theHotAreaMap');
		if( theLayer ) theLayer.style.cursor = "crosshair";
		panning=false;
		zooming=false;
		hideZoomBox();
		}
	if (state == "ZoomToWindow") {
		setZoomBoxColor("#ff0000");
		setZoomBoxWidth(1);
	} else if (state == "PanOnDrag") {
	}
}
function setZoomBoxColor(color) {
	return;
}
function setZoomBoxSettings() {
	getOriginalOffset();
	if (isNav && is5up) {
		document.captureEvents(Event.MOUSEMOVE);
		document.captureEvents(Event.MOUSEDOWN);
		document.captureEvents(Event.MOUSEUP);
		document.onmousemove = getMouse;
		document.onmousedown = mapTool;
		document.onmouseup = chkMouseUp;
	} else if (isNav4) {
		getLayer(mapImgName + "_theMap").captureEvents(Event.MOUSEMOVE);
		getLayer(mapImgName + "_theMap").captureEvents(Event.MOUSEDOWN);
		getLayer(mapImgName + "_theMap").captureEvents(Event.MOUSEUP);
		getLayer(mapImgName + "_theMap").onmousemove = getMouse;
		getLayer(mapImgName + "_theMap").onmousedown = mapTool;
		getLayer(mapImgName + "_theMap").onmouseup = chkMouseUp;
	} else {
		document.onmousemove = getMouse;
		document.onmousedown = mapTool;
		document.onmouseup = chkMouseUp;
	}
}
function setZoomBoxWidth(size) {
	zoomBoxWidth = size;
}
function showZoomBox() {
	return
}
function startPan(e) {
	getOriginalOffset();
	moveLayer(mapImgName + "_theMap",vspc,hspc);
	getImageXY(e);
	if ((mouseX>=0) && (mouseY>=0) && (mouseX<iWidth) && (mouseY<iHeight)) {
		if (panning) {
			stopPan(e);
		} else {
			x1=mouseX;
			y1=mouseY
			x2=x1+1;
			y2=y1+1;
			panning=true;
		}
	}
	return false;
}
function startZoomBox(e) {
	getOriginalOffset();
	getImageXY(e);	
	if ((mouseX<iWidth) && (mouseY<iHeight-bottomBorderHeight)) {
		if (!zooming) {
			x1=mouseX;
			y1=mouseY;
			x2=x1+1;
			y2=y1+1;
			zooming=true;
			crshmRubberBandLayer.clear();
			crshmRubberBandLayer.setColor(crshmRubberBandLayerOutlineColor);
			crshmRubberBandLayer.setOpacity(crshmRubberBandLayerOutlineOpacity);
			crshmRubberBandLayer.drawRect(x1,y1,x2-x1,y2-y1);
			crshmRubberBandLayer.paint();
			if( crshmRubberBandLayerFillOpacity > 0 ) {
				crshmRubberBandLayer.setColor(crshmRubberBandLayerFillColor);
				crshmRubberBandLayer.setOpacity(crshmRubberBandLayerFillOpacity);
				crshmRubberBandLayer.fillRect(x1,y1,x2-x1,y2-y1);
				crshmRubberBandLayer.paint();
			}
		}
	} else {
		if (zooming) {
			stopZoomBox(e);
		}
	}
	return false;
}
function stopPan(e) {
	if ((Math.abs(x2-x1) < 2) && (Math.abs(y2-y1) < 2)) {
		recenter(e);
		panning=false;
	} else  {
		panning=false;
		refreshMap(state);
	}
	return true;
}
function stopZoomBox(e) {
	zooming=false;
	crshmRubberBandLayer.clear();
	if ((zmaxx>zminx+2) && (zmaxy>zminy+2)) {
		recenter(e);
	} else {
		refreshMap(state);
	}
	return true;
}
function clipLayer(name, clipleft, cliptop, clipright, clipbottom) {
	var layer = getLayer(name);
	if (layer != null) {
		if (isNav4) {
			layer.clip.left   = clipleft;
			layer.clip.top    = cliptop;
		    layer.clip.right  = clipright;
			layer.clip.bottom = clipbottom;
		} else if (isIE) {
			layer.clip = 'rect(' + cliptop + 'px ' +  clipright + 'px ' + clipbottom + 'px ' + clipleft +'px)';
	    } else if (isNav) {
			layer.clip = 'rect(' + cliptop + 'px ' +  clipright + 'px ' + clipbottom + 'px ' + clipleft +'px)';
	    } else {
		layer.height = (clipbottom - cliptop).toString() + 'px';
			layer.width	= (clipright - clipleft).toString() + 'px';
			layer.top	= (cliptop+vspc) + "px";
			layer.left	= (clipleft+hspc) + "px";
		}
	}
}
function getLayer(name) {
	  if (isNav4)
	    return(document.layers[name]);
	  else if (isIE4) {
	  	if ( eval('document.all.' + name) != null) {
		    layer = eval('document.all.' + name + '.style');
		    return(layer);
		} else
			return(null);
	  } else if (is5up) {
		var theObj = document.getElementById(name);
		return theObj.style
	  } else
	    return(null);
}
function getLayerObject(name) {
	  if (isNav4)
	    return(document.layers[name]);
	  else if (isIE4) {
	  	if ( eval('document.all.' + name) != null) {
		    layer = eval('document.all.' + name );
		    return(layer);
		} else
			return(null);
	  } else if (is5up) {
			var theObj = document.getElementById(name);
			return theObj
	  } else
			return(null);
}
function hideLayer(name) {
	var layer = getLayer(name);
	if (layer != null) {
	 	if (isNav4)
			layer.visibility = "hide";
		else
			 layer.visibility = "hidden";
	}
}
function moveLayer(name, x, y) {
	var layer = getLayer(name);
	if (layer != null) {
	  	if (isNav4)
			layer.moveTo(x, y);
	 	else if (isIE) {
			layer.left = x + "px";
			layer.top  = y + "px";
		} else {
			layer.height = (iHeight - y).toString() + 'px';
			layer.width	= (iWidth - x).toString + 'px';
		layer.left = x + "px";
			layer.top  = y + "px";
	  	}
	}
}
function replaceLayerContent(name, content) {
	  if (isNav4) {
		    var layer = getLayer(name);
			if (layer != null) {
			    layer.document.open();
			    layer.document.writeln(content);
			    layer.document.close();
			}
	  }  else if (isIE) {
			if (eval("document.all." + name) != null) {
		  	content = content.replace(/\'/g,"\\'");
			    var str = "document.all." + name + ".innerHTML = '" + content + "'";
			    eval(str);
			}
	  }
}
function setLayerBackgroundColor(name, color) {
	var layer = getLayer(name);
	if (layer != null) {
		if (isNav4)
			layer.bgColor = color;
		else
			layer.backgroundColor = color;
	}
}
function showLayer(name) {		
	var layer = getLayer(name);		
	if (layer != null) {
	  	if (isNav4)
			layer.visibility = "show";
		else
		 	layer.visibility = "visible";
	}
}

function initMapOps(){
    setZoomBoxSettings();
}
//initMapOps();
