var LEFT_BUTTON = 1;
var boxWidth = 2;
var boxColor = "#FF0000";

var Zooming = false;
var Panning = false;

var mouseX=0;
var mouseY=0;
var zminx=0;
var zmaxx=0;
var zminy=0;
var zmaxy=0;
var x1=0;
var y1=0;
var x2=0;
var y2=0;
var blnStateImg = true;
var blnCntyImg = true;
var blnEnvImg = true;

function createLoadFlag(left, top)
{
	content = '<IMG src="images/processing.gif" border=0 name="process1"><IMG src="images/process_ani.gif" border="0" name="process2">';
	createLayer("theLoadFlag", left, top, 103, 24, false, content, "#FFFFFF");
}

function createZoomBox()
{
	//Zoom Box
	content = '<img name="zoomImageTop" src="images/pixel.gif" width="1" height="1">';
	createLayer("zoomBoxTop", hspc, vspc, mWidth, mHight, false, content, boxColor);
	content = '<img name="zoomImageLeft" src="images/pixel.gif" width="1" height="1">';
	createLayer("zoomBoxLeft", hspc, vspc, mWidth, mHight, false, content, boxColor);
	content = '<img name="zoomImageRight" src="images/pixel.gif" width="1" height="1">';
	createLayer("zoomBoxRight", hspc, vspc, mWidth, mHight, false, content, boxColor);
	content = '<img name="zoomImageBottom" src="images/pixel.gif" width="1" height="1">';
	createLayer("zoomBoxBottom", hspc, vspc, mWidth, mHight, false, content, boxColor);
}

function startCaptureEvents()
{
    //if (document.layers) {
    if (isNav4) {

        getLayer("theMapImage").captureEvents(Event.MOUSEMOVE);
		getLayer("theMapImage").captureEvents(Event.MOUSEDOWN);
		getLayer("theMapImage").captureEvents(Event.MOUSEUP);
		getLayer("theMapImage").onmousemove = onMouseMove;
		getLayer("theMapImage").onmousedown = onMouseDown;
		getLayer("theMapImage").onmouseup = onMouseUp;
	}
	else if (isNav && is5up) {
		document.captureEvents(Event.MOUSEMOVE);
		document.captureEvents(Event.MOUSEDOWN);
		document.captureEvents(Event.MOUSEUP);
		document.onmousemove = onMouseMove;
	    document.onmousedown = onMouseDown;
	    document.onmouseup = onMouseUp;
	}
    else {
		document.onmousemove = onMouseMove;
		document.onmousedown = onMouseDown;
		document.onmouseup = onMouseUp;
	}
	document.ShowOrthoImage.onload = completeImgLoad1;
	document.mapImage.onload = completeImgLoad2;
	document.envanlysImage.onload = completeImgLoad3;
}

function completeImgLoad1(){
	//alert("one");
	blnStateImg = true;
	completeImgLoad();
}
function completeImgLoad2(){
	//alert("two");
	blnCntyImg = true;
	completeImgLoad();
}
function completeImgLoad3(){
	//alert("three");
	blnEnvImg = true;
	completeImgLoad();
}
function completeImgLoad(){
	if (blnStateImg && blnCntyImg && blnEnvImg){
		//alert("coming here 1234");
		hideLayer('theLoadFlag');
	}
}

function onMouseDown(e)
{
 	var button;
	//if (document.layers) {
	if(isNav){
        button = e.which;
    }
	else {
		button = event.button;
	}

	if(button != LEFT_BUTTON) return;

	getImageXY(e);

	if ((mouseX >= 0) && (mouseX <= mWidth) && (mouseY >= 0) && (mouseY <= mHight)) {
	   if ((!Zooming) && (!Panning)) {
		   switch (curMode) {
			   case "zoin": startBox(e); break;
			   case "zout": startBox(e); break;
			   case "info": zminx = mouseX; zminy = mouseY;refreshMap(); break;
			   case "move": startPan(e); break;
			   case "geocode": geocodePoint(e); break;
		   }
     	   return false;
       } else {
           return true;
       }
    }
}

// check for mouseup
function onMouseUp(e)
{
 	var button;

	//if (document.layers) {
	if(isNav){
		button = e.which;
	}
	else {
		button = event.button;
	}

	if(button != LEFT_BUTTON) return;

    if (Zooming) {
		stopBox(e);
	} else if (Panning) {
		stopPan(e);
	}

	return true;
}

// get the coords at mouse position
function onMouseMove(e) {

    setStatus();

	if (Zooming || Panning) {
		getImageXY(e);

        if ((mouseX >= 0) && (mouseX <= mWidth) && (mouseY >= 0) && (mouseY <= mHight)) {
            x2 = mouseX;
		    y2 = mouseY;

      		if (Zooming) {
	    		if (x2 < 0) x2 = 0;
		        if (y2 < 0) y2 = 0;
          		if (x2 > mWidth) x2 = mWidth;
		    	if (y2 > mHight) y2 = mHight;
			    setClip();
       		} else {
			     panMouse();
		    }
            return false;
        } else {
            onMouseUp ();
        }
	} else {
		return true;
	}

}

function getImageXY(e)
{
    if (isNav) {
	    mouseX=e.pageX;
		mouseY=e.pageY;
	}
	else {
		mouseX=event.clientX + document.body.scrollLeft-2; //custom values
		mouseY=event.clientY + document.body.scrollTop-2;
	}
	// subtract offsets from page left and top
	mouseX = mouseX-hspc;
    mouseY = mouseY-vspc;
}

// start zoom box display
function startBox(e) {
//	moveLayer("map",hspc,vspc);
	getImageXY(e);
	// keep it within the MapImage
	if ((mouseX<=mWidth) && (mouseY<=mHight)) {
		if (!Zooming) {
			x1=mouseX;
			y1=mouseY
			x2=x1+1;
			y2=y1+1;
			clipLayer("zoomBoxTop",x1,y1,x2,y2);
			clipLayer("zoomBoxLeft",x1,y1,x2,y2);
			clipLayer("zoomBoxRight",x1,y1,x2,y2);
			clipLayer("zoomBoxBottom",x1,y1,x2,y2);
			Zooming=true;
			showLayer("zoomBoxTop");
			showLayer("zoomBoxLeft");
			showLayer("zoomBoxRight");
			showLayer("zoomBoxBottom");
		}
	} else {
		if (Zooming) {
			stopBox(e);
		}
	}
	return false;
}

// stop zoom box display
function stopBox(e)
{
	Zooming=false;

	// if the zoom box is too small
	if ((zmaxx-zminx < 4) && (zmaxy-zminy < 4))
	{
		zmaxx = zminx;
		zmaxy = zminy;
	}

	hideBox();
	refreshMap();

	return true;
}

// obtain the geocoded point x,ys
function geocodePoint(e)
{
    getImageXY(e);

    Zooming=false;
	x1 = mouseX;
	y1 = mouseY;
	zminx = x1;
	zminy = y1;
	zmaxx = x1;
	zmaxy = y1;

    hideBox();
	refreshMap();

	return true;

}

function boxIt(theLeft, theTop, theRight, theBottom)
{
	clipLayer("zoomBoxTop", theLeft, theTop, theRight, theTop+boxWidth);
	clipLayer("zoomBoxLeft", theLeft, theTop, theLeft+boxWidth, theBottom);
	clipLayer("zoomBoxRight", theRight-boxWidth, theTop, theRight, theBottom);
	clipLayer("zoomBoxBottom", theLeft, theBottom-boxWidth, theRight, theBottom);
	showLayer("zoomBoxTop");
	showLayer("zoomBoxLeft");
	showLayer("zoomBoxRight");
	showLayer("zoomBoxBottom");
}

	// start pan.... image will move
function startPan(e)
{
	document.pixel.src = "images/pixel.gif";
    showLayer("theMapImage");
	getImageXY(e);

	// keep it within the Map
	if ((mouseX <= mWidth) && (mouseY <= mHight)) {
		if (Panning)
			stopPan(e);
		else {
			x1 = mouseX;
			y1 = mouseY
			x2 = x1+1;
			y2 = y1+1;

			Panning = true;
		}
	}
	else {
		if (Panning) stopPan(e);
	}

	return false;
}

// Stop Pan
function stopPan(e)
{
	Panning=false;

	zminx = x1;
	zminy = y1;
	zmaxx = x2;
	zmaxy = y2;

	//alert (zminx +"::::"+ zmaxx +"::::"+ zminy +"::::"+ zmaxy);

	hideBox();
	refreshMap();
	return true;
}

// move map image with mouse
function panMouse(e)
{
	var xMove = x2-x1;
	var yMove = y2-y1;
	var cTop = -yMove;
	var cLeft = -xMove;
	var cRight = mWidth;
	var cBottom = mHight;

	if (xMove>0) {
		cLeft = 0;
		cRight = mWidth - xMove;
	}
	if (yMove>0) {
		cTop = 0;
		cBottom = mHight - yMove;
	}

	clipLayer("theMapImage", cLeft, cTop, cRight, cBottom);
	moveLayer("theMapImage", xMove+hspc, yMove+vspc);
	clipLayer("theShowOrthoImage", cLeft, cTop, cRight, cBottom);
	moveLayer("theShowOrthoImage", xMove+hspc, yMove+vspc);
	//need to do the following only if theEAImage layer is visible
	clipLayer("theEAImage", cLeft, cTop, cRight, cBottom);
	moveLayer("theEAImage", xMove+hspc, yMove+vspc);

	return false;
}

// clip zoom box layer to mouse coords
function setClip()
{
	if (x1>x2) {
		zmaxx=x1;
		zminx=x2;
	} else {
		zminx=x1;
		zmaxx=x2;
	}

	if (y1>y2) {
		zmaxy=y1;
		zminy=y2;
	} else {
		zminy=y1;
		zmaxy=y2;
	}

	if ((x1 != x2) && (y1 != y2)) {
		boxIt(zminx, zminy, zmaxx, zmaxy);
	}
}

function hideBox()
{
	hideLayer("zoomBoxTop");
	hideLayer("zoomBoxLeft");
	hideLayer("zoomBoxRight");
	hideLayer("zoomBoxBottom");
}

function setStatus()
{
	window.status="";
	switch (curMode) {
		case "zoin":
			if (Zooming) window.status = "Zooming in map...";
			else window.status = "Click or draw a box to zoom in map";
			break;
		case "zout":
			if (Zooming) window.status = "Zooming out map...";
			else window.status = "Click or draw a box to zoom out map";
			break;
		case "move":
			if (Panning) window.status = "Panning map...";
			else window.status = "Hold mouse down to move map";
			break;
		case "info":
			if (Zooming) window.status = "Querying features...";
			else window.status = "Click or draw box to query a feature";
			break;
		default: window.status = "";
	}

	return true;
}

function refreshMap()
{
	document.forms["mapForm"].boxMinX.value = zminx;
	document.forms["mapForm"].boxMinY.value = zminy;
	document.forms["mapForm"].boxMaxX.value = zmaxx;
	document.forms["mapForm"].boxMaxY.value = zmaxy;
	
	
	showLayer('theLoadFlag');
	if (document.forms["mapForm"].cmdName.value == "info"){
	   //alert("shshsh");	
	   document.forms["mapForm"].showJustLocWithNoAddr.value = "";	
	   changeLinkColor('noneofthese');
	   js_addrSrch = "";
	   js_showZoningImg = "";
	   
	   //----------------------------------------
	   //Process to find x,y of the clicked point
	   //----------------------------------------	
	   //Current map extents
	   var iCurMinX = parseFloat(document.forms["mapForm"].curMinX.value);
	   var iCurMaxX = parseFloat(document.forms["mapForm"].curMaxX.value);
	   var iCurMinY = parseFloat(document.forms["mapForm"].curMinY.value);
	   var iCurMaxY = parseFloat(document.forms["mapForm"].curMaxY.value);

	   //box extents
	   var iBoxMinX = parseFloat(document.forms["mapForm"].boxMinX.value);
	   var iBoxMaxX = parseFloat(document.forms["mapForm"].boxMaxX.value);
	   var iBoxMinY = parseFloat(document.forms["mapForm"].boxMinY.value);
	   var iBoxMaxY = parseFloat(document.forms["mapForm"].boxMaxY.value);

	   //Calculating the Range
       var iRangeX = iCurMaxX - iCurMinX;  
       var iRangeY = iCurMaxY - iCurMinY;

       //Calculating x,y of the clicked point
       var iGeoX = iCurMinX + iBoxMinX * iRangeX / mWidth;
       var iGeoY = iCurMinY + (mHight - iBoxMinY) * iRangeY / mHight;

	   var iWhatAction = "";
	   if (js_showPrclLyrId){
			iWhatAction = "showPrclDtl";	
	   }
	   else {
			iWhatAction = "showEnvDtl";
	   }
	   
	   populateMapParams("","","","",iCurMinX,iCurMaxX,iCurMinY,iCurMaxY,iBoxMinX,iBoxMaxX,iBoxMinY,iBoxMaxY,iWhatAction,"",iGeoX,iGeoY);

	   hide('rightpane');
    }
	else {
		document.forms["mapForm"].submit();
	}
}
