/*
'-------------------------------------------------
'------------For map interaction--------------- 
'-------------------------------------------------
*/
//Current xy position of mouse
var x;
var y;

//Point where you clicked down.
var x1;
var y1;

//Point where you released the button.
var x2;
var y2;

//Current center of map
var Cx;
var Cy;

//Current xy offset position for moving the map image.
var ty;
var tx;

//If mouse button has been released 
var StateOfMouse;

//For dynamic zoom box
var startx;
var starty;

//Lower browser boarder
var LowerBorderLimit;
/*
//Assign events
window.onload = window_onload;
window.onresize = window_onresize;
window.onbeforeunload = window_onbeforeunload;
document.onstop = document_onstop;
*/
function AttachEvents()
{
	//Assign events
	//window.onload = window_onload;
	//window.onresize = window_onresize;
	window.onbeforeunload = window_onbeforeunload;
	document.onstop = document_onstop;
	
	document.getElementById("GlassLayer").onmousedown = GlassLayer_onmousedown;
	document.getElementById("GlassLayer").onmouseup = GlassLayer_onmouseup;
	document.getElementById("GlassLayer").onmousemove = GlassLayer_onmousemove;
};

function GrabXY()
{
/*
'This grabs the current xy position of the cursor and adjusts 
'for the position of the image in relation to the document.
'It also grabs the current center of the map.
*/

	var Px = window.event.clientX + document.body.scrollLeft;
	var Py = window.event.clientY + document.body.scrollTop;
	var Offx;
	var Offy;
		
	var Tool = document.Form1.CurrentTool.value;
 
	switch(Tool)
	{
		case "Pan":
			Offx = document.getElementById("GlassLayer").offsetLeft + 14;
			Offy = document.getElementById("GlassLayer").offsetTop + 19;
			break;
		default:
			Offx = document.getElementById("imgMap").offsetLeft + 2;
			Offy = document.getElementById("imgMap").offsetTop + 2;
	}
	
	x = (Px - Offx)
	y = (Py - Offy)
    
    window.status = "X=" + x + "Y=" + y;
	//window.status = "X=" & Px & "Y=" & Py
	//window.status = "X=" & (Px - Offx) & "Y=" & (Py-Offy)
	
	Cx = document.getElementById("imgMap").offsetWidth / 2;
    Cy = document.getElementById("imgMap").offsetHeight / 2;
    //msgbox "Current center:" & Cx & " : " & Cy
};

function GlassLayer_onmousedown()
{
	GrabXY();
	x1 = x;
	y1 = y;
	//window.status = "X=" & x1 & "Y=" & y1

		ty = window.event.clientY - document.getElementById("imgMap").style.posTop - 79;//89
		tx = window.event.clientX - document.getElementById("imgMap").style.posLeft;
	
	StateOfMouse = true;
	
	var minX;
	var maxX;
	var minY;
	var maxY;
	var Tool = document.Form1.CurrentTool.value;
	
	switch(Tool)
	{
		case "Zoom In":
			startx = window.event.x;
			starty = window.event.y + document.body.scrollTop;
			
				minX = 0;
				maxX = 2000;//document.getElementById("imgMap").offsetWidth - 4				
				minY = 0;
				maxY = 2000;//document.getElementById("imgMap").offsetHeight - 4

			if (startx >= minX && startx <= maxX && starty >= minY && starty <= maxY)
			{
				document.getElementById("rubberBand").style.visibility = "visible";
				document.getElementById("rubberBand").style.width = 0;//1
				document.getElementById("rubberBand").style.height = 0;//1
				document.getElementById("rubberBand").style.pixelLeft = window.event.x;
				document.getElementById("rubberBand").style.pixelTop = window.event.y + document.body.scrollTop;
		 	 }
		 	 break;
		 	 
		case "Select Tool":
			startx = window.event.x;
			starty = window.event.y + document.body.scrollTop;
			
				minX = 0;
				maxX = 2000;//document.getElementById("imgMap").offsetWidth - 4				
				minY = 0;
				maxY = 2000;//document.getElementById("imgMap").offsetHeight - 4

			if (startx >= minX && startx <= maxX && starty >= minY && starty <= maxY)
			{ 
				document.getElementById("rubberBand").style.visibility = "visible";
				document.getElementById("rubberBand").style.width = 0;//1
				document.getElementById("rubberBand").style.height = 0;//1
				document.getElementById("rubberBand").style.pixelLeft = window.event.x;
				document.getElementById("rubberBand").style.pixelTop = window.event.y + document.body.scrollTop;
		 	}
	}
};

function GlassLayer_onmouseup()
{
	GrabXY();
	x2 = x;
	y2 = y;
	//window.status = "X=" & x2 & "Y=" & y2

	//New projected center of map
	var NCx; //center point x
	var NCy; //center point y
	var Ax;
	var Ay;
	
	Ax = (x2-x1);
	Ay = (y2-y1);
	
	NCx = Cx - Ax;
	NCy = Cy - Ay;
	
	StateOfMouse = false;
	
	var Tool = document.Form1.CurrentTool.value;
	
	switch(Tool)
	{
		case "Identify":
			document.Form1.PointX.value = x1;
			document.Form1.PointY.value = y1;
			//.submit 'Don't need to submit because the zindex has been changed to 1 for imgMap
			break;
			
		case "Zoom In":
			document.Form1.MinX.value = x1;
			document.Form1.MinY.value = y1;
			document.Form1.MaxX.value = x2;
			document.Form1.MaxY.value = y2;
			//.submit 'Don't need to submit because the zindex has been changed to 1 for imgMap
			break;
			
		case "Select Tool": 
			document.Form1.MinX.value = x1;
			document.Form1.MinY.value = y1;
			document.Form1.MaxX.value = x2;
			document.Form1.MaxY.value = y2;
			//.submit 'Don't need to submit because the zindex has been changed to 1 for imgMap
			break;
			
		case "Zoom Out":
			document.Form1.PointX.value = x1;
			document.Form1.PointY.value = y1;
			document.Form1.submit();
			break;
			
		case "Pan":
			document.Form1.PanX.value = NCx;
			document.Form1.PanY.value = NCy;
			document.Form1.submit();
			break;
			
		case "Pictometry Tool":
			document.Form1.PointX.value = x1;
			document.Form1.PointY.value = y1;
			//.submit
			break;
        case "AmCAD Tool":
            document.Form1.PointX.value = x1;
            document.Form1.PointY.value = y1;
            break;
	}
};

function GlassLayer_onmousemove()
{
	if(StateOfMouse)
	{
		var curX;
		var curY;
		var Tool = document.Form1.CurrentTool.value;
		
		switch(Tool)
		{
			case "Zoom In":
				curX = window.event.x;
				curY = window.event.y + document.body.scrollTop;
				
				if(curX < startx)
				{
					document.getElementById("rubberBand").style.pixelLeft = curX;
					document.getElementById("rubberBand").style.width = (startx - document.getElementById("rubberBand").style.pixelLeft);
				}
				
				if(curX > startx)
				{
					document.getElementById("rubberBand").style.width = (curX - startx);
				}
				
				if(curY < starty)
				{
					document.getElementById("rubberBand").style.pixelTop = curY;
					document.getElementById("rubberBand").style.height = (starty - document.getElementById("rubberBand").style.pixelTop);
				}
				
				if(curY > starty)
				{
					document.getElementById("rubberBand").style.height = (curY - starty);
				}
				break;

			case "Select Tool":
				curX = window.event.x;
				curY = window.event.y + document.body.scrollTop;
				
				if(curX < startx)
				{
					document.getElementById("rubberBand").style.pixelLeft = curX;
					document.getElementById("rubberBand").style.width = (startx - document.getElementById("rubberBand").style.pixelLeft);
				}
				
				if(curX > startx)
				{
					document.getElementById("rubberBand").style.width = (curX - startx);
				}
				
				if(curY < starty)
				{
					document.getElementById("rubberBand").style.pixelTop = curY;
					document.getElementById("rubberBand").style.height = (starty - document.getElementById("rubberBand").style.pixelTop);
				}
				
				if(curY > starty)
				{
					document.getElementById("rubberBand").style.height = (curY - starty);
				}
				break;
			
			case "Pan":
				document.getElementById("imgMap").style.left = window.event.clientX - tx;
				document.getElementById("imgMap").style.top = window.event.clientY - ty;
				break;
		}
	}
};
/*
'-------------------------------------------------
'-------------------------------------------------
'-------------------------------------------------
*/
function window_onload()
{
	LoadProgressAnima("hidden");
	CheckMapSize();
	NavigationTools(document.Form1.CurrentTool.value);
};

function window_onresize()
{
	document.Form1.CurrentTool.value = "";
	MapResize();
};

function window_onbeforeunload()
{
	LoadProgressAnima("visible");
};

function document_onstop()
{
	document.getElementById("rubberBand").style.visibility = "hidden";
	document.getElementById("rubberBand").style.width = 0;
	document.getElementById("rubberBand").style.height = 0;
	document.getElementById("rubberBand").style.pixelLeft = 0;
	document.getElementById("rubberBand").style.pixelTop = 0;
};




