//---------------------------------------------------------------------------------------------
//	Cartology.NET CartologyMAPControl library Javascript
//	
//---------------------------------------------------------------------------------------------

var StartX;
var StartY;
var moveon = false;
var numclick = 0;
var recton = false;

var currentX = 0;
var currentY = 0;
var whichEl = null;

var g_mouseX;
var g_mouseY;

//---------------------------------------------------------------------------------------------
// SetMapMode function used to place map control into required mode
//
function SetMapMode(inNewMode)
{
	MapMode = inNewMode;

	//activeEl = null;
	//objMap.onmouseover=null;
	//objMap.onmousedown=null;
	//objMap.onmousemove=null;
	//objMap.onmouseup=null;

	switch(MapMode)
    {
        case 'MeasureLine':
			objMap.style.cursor = "crosshair";
			break
        case 'MeasurePolygon':
			objMap.style.cursor = "crosshair";
			break
        case 'PointCenter':
			objMap.style.cursor = "crosshair";
			break
        case 'ZoomRect':
			objMap.style.cursor = "crosshair";
			//objMap.onmousemove = MapMove;
			break;
        case 'DrawBoundingBox':
			objMap.style.cursor = "crosshair";
			objMap.onmousemove=MapMove;
			var objRect = document.getElementById("zoom2");
			objRect.style.borderColor = 'black';
			objRect.style.borderStyle = 'dashed';
			break
        case 'PointQuery':
			objMap.style.cursor = "crosshair";
			break
        case 'PanMap':
			activeEl = objMap;

			objMap.onmouseover=cursEl;
			objMap.onmousedown=grabEl;
			objMap.onmousemove=moveEl;
			objMap.onmouseup=dropEl;
			break
		case 'QueryAllInView':
			QueryAll();
			break
        case 'GetPoint':
			objMap.style.cursor = "crosshair";
			break;
        case 'ShowRect':
			var objRect = document.getElementById("zoom2");
			objRect.style.borderColor = 'black';
			objRect.style.borderStyle = 'dashed';
			recton = true;
			objMap.style.cursor = "move";
			objMap.onmousemove = RectMove;
			objRect.onmousemove = RectMove;
			objRect.style.cursor = "move";
			activateRect();
			break;
        default:
			break
	}
}
//---------------------------------------------------------------------------------------------

//---------------------------------------------------------------------------------------------
//

function MapClick(event)
{
	switch(MapMode)
	{
		case 'PointCenter':
			PointCenter(event);
			break
		case 'ZoomRect':
			ZoomRect(event);
			break
		case 'DrawBoundingBox':
			ZoomRect(event);
			break
		case 'PointQuery':
			PointQuery(event);
			break
		case 'MeasureLine':
			MeasureLine(event);
			break
		case 'MeasurePolygon':
			MeasureLine(event);
			break
		case 'GetPoint':
			FindPoint(event);
			break
		case 'ShowRect':
			getBounds();
			break
		default:
			break
	}
}
//---------------------------------------------------------------------------------------------

//---------------------------------------------------------------------------------------------
//

function getNewMap() {
	CentreXWorld = parseFloat(document.forms[0].WorldX.value);
	CentreYWorld = parseFloat(document.forms[0].WorldY.value);
	UnitsPerPixel = parseFloat(document.forms[0].WPD.value);

	bitmap_width_pixel = parseInt(document.forms[0].PixelWidth.value.replace("px",""));
	bitmap_height_pixel = parseInt(document.forms[0].PixelHeight.value.replace("px",""));

	CurrentStartX = parseInt((document.forms[0].zoom2.style.left).replace("px","")) - objMapHolder.offsetLeft - parseInt(objMapHolder.style.borderWidth.replace("px",""));
	CurrentStartY = parseInt((document.forms[0].zoom2.style.top).replace("px","")) - objMapHolder.offsetTop - parseInt(objMapHolder.style.borderWidth.replace("px",""));
	CurrentEndX = parseInt(CurrentStartX) + parseInt((document.forms[0].zoom2.style.width).replace("px",""))
	CurrentEndY = parseInt(CurrentStartY) + parseInt((document.forms[0].zoom2.style.height).replace("px",""))
	CurrentCenterXPixel = (parseInt(CurrentStartX) + parseInt(((CurrentEndX - CurrentStartX)/2)))
	CurrentCenterYPixel = (parseInt(CurrentStartY) + parseInt(((CurrentEndY - CurrentStartY)/2)))

	CurrentWidth = parseInt((document.forms[0].zoom2.style.width).replace("px",""))
	CurrentHeight = parseInt((document.forms[0].zoom2.style.height).replace("px",""))

	ZoomCentreXWorld = CentreXWorld - UnitsPerPixel * ((bitmap_width_pixel/2) - CurrentCenterXPixel)
	ZoomCentreYWorld = CentreYWorld + UnitsPerPixel * ((bitmap_height_pixel/2) - CurrentCenterYPixel)

	NewUnitsPerPixelX = UnitsPerPixel * CurrentWidth / bitmap_width_pixel
	NewUnitsPerPixelY = UnitsPerPixel * CurrentHeight / bitmap_height_pixel

	if (NewUnitsPerPixelX > NewUnitsPerPixelY) {
		document.forms[0].NewWPD.value = NewUnitsPerPixelX;
	}
	else if (NewUnitsPerPixelY > NewUnitsPerPixelX) {
		document.forms[0].NewWPD.value = NewUnitsPerPixelY;
	}
	else {
		document.forms[0].NewWPD.value = NewUnitsPerPixelX;
	}
	document.forms[0].MapMode.value = 'ZoomRect';
	document.forms[0].X.value = ZoomCentreXWorld;
	document.forms[0].Y.value = ZoomCentreYWorld;

	document.forms[0].submit();
	
}
//---------------------------------------------------------------------------------------------

//---------------------------------------------------------------------------------------------
//

function ZoomRect(event)
{
	if (numclick == 0) {

		var objZoom2 = document.getElementById("zoom2");

		objZoom2.style.visibility = "hidden";
		objZoom2.style.width = 0;
		objZoom2.style.height = 0;

		numclick = 1;

		StartX = (event.clientX + document.body.scrollLeft) - 2;
		StartY = (event.clientY + document.body.scrollTop) - 2;

		objZoom2.style.left = StartX;
		objZoom2.style.top = StartY;

		moveon = true;
	}
	else
	{
		moveon = false;
		numclick = 0;
		if (MapMode == 'ZoomRect')
			{getNewMap();}
		else if (MapMode == 'DrawBoundingBox')
			{getBounds();}
	}
}
//---------------------------------------------------------------------------------------------

//---------------------------------------------------------------------------------------------
//

function getBounds() {
	CentreXWorld = parseFloat(document.forms[0].WorldX.value);
	CentreYWorld = parseFloat(document.forms[0].WorldY.value);
	UnitsPerPixel = parseFloat(document.forms[0].WPD.value);
	bitmap_width_pixel = parseInt(document.forms[0].PixelWidth.value.replace("px",""));
	bitmap_height_pixel = parseInt(document.forms[0].PixelHeight.value.replace("px",""));

	CurrentStartX = parseInt((document.forms[0].zoom2.style.left).replace("px","")) - objMapHolder.offsetLeft - parseInt(objMapHolder.style.borderWidth.replace("px",""));
	CurrentStartY = parseInt((document.forms[0].zoom2.style.top).replace("px","")) - objMapHolder.offsetTop - parseInt(objMapHolder.style.borderWidth.replace("px",""));
	CurrentEndX = parseInt(CurrentStartX) + parseInt((document.forms[0].zoom2.style.width).replace("px",""))
	CurrentEndY = parseInt(CurrentStartY) + parseInt((document.forms[0].zoom2.style.height).replace("px",""))
	CurrentCenterXPixel = (parseInt(CurrentStartX) + parseInt(((CurrentEndX - CurrentStartX)/2)))
	CurrentCenterYPixel = (parseInt(CurrentStartY) + parseInt(((CurrentEndY - CurrentStartY)/2)))

	CurrentWidth = parseInt((document.forms[0].zoom2.style.width).replace("px",""))
	CurrentHeight = parseInt((document.forms[0].zoom2.style.height).replace("px",""))

	ZoomCentreXWorld = CentreXWorld - UnitsPerPixel * ((bitmap_width_pixel/2) - CurrentCenterXPixel)
	ZoomCentreYWorld = CentreYWorld + UnitsPerPixel * ((bitmap_height_pixel/2) - CurrentCenterYPixel)

	XMin = ZoomCentreXWorld - ((CurrentWidth / 2) * UnitsPerPixel);
	YMin = ZoomCentreYWorld - ((CurrentHeight / 2) * UnitsPerPixel);
	XMax = ZoomCentreXWorld + ((CurrentWidth / 2) * UnitsPerPixel);
	YMax = ZoomCentreYWorld + ((CurrentHeight / 2) * UnitsPerPixel);

	document.forms[0].XMin.value = XMin;
	document.forms[0].YMin.value = YMin;
	document.forms[0].XMax.value = XMax;
	document.forms[0].YMax.value = YMax;

	document.forms[0].MapMode.value = 'DrawBoundingBox';

	document.forms[0].submit();

}
//---------------------------------------------------------------------------------------------

function MapMove2()
{
	MapMove();
}
//---------------------------------------------------------------------------------------------
//
var RectWidth = 100;
var RectHeight = 150;
var RectOpacity = 25;
var RectImage = 'Images/RectBlack.gif';

function activateRect()
{
	document.forms[0].zoom2.src = RectImage;

	document.forms[0].zoom2.style.opacity = RectOpacity / 100;
	document.forms[0].zoom2.style.filter = "alpha(opacity=" + RectOpacity + ")";

	document.forms[0].zoom2.style.width = RectWidth;
	document.forms[0].zoom2.style.height = RectHeight;
}

function RectMove()
{
	if (recton == true)
	{

		mouseX = (event.clientX + document.body.scrollLeft);
		mouseY = (event.clientY + document.body.scrollTop);

		MapWindowPositionLeft = objMapHolder.offsetLeft + parseInt(objMapHolder.style.borderWidth.replace("px",""));
		MapWindowPositionTop = objMapHolder.offsetTop + parseInt(objMapHolder.style.borderWidth.replace("px",""));

		MapWidth = parseInt(objMapHolder.style.width.replace("px","")) - (parseInt(objMapHolder.style.borderWidth.replace("px","")) * 2);
		MapHeight = parseInt(objMapHolder.style.height.replace("px","")) - (parseInt(objMapHolder.style.borderWidth.replace("px","")) * 2);

		newX = mouseX - RectWidth / 2;
		newY = mouseY - RectHeight / 2;
		
		if ((newX + RectWidth) > (MapWindowPositionLeft + MapWidth))
		{document.forms[0].zoom2.style.left = MapWindowPositionLeft + MapWidth - RectWidth - 2}
		else if (newX <= MapWindowPositionLeft)
		{document.forms[0].zoom2.style.left = MapWindowPositionLeft}
		else if (newX > MapWindowPositionLeft)
		{document.forms[0].zoom2.style.left = newX}

		if ((newY + RectHeight) > (MapWindowPositionTop + MapHeight))
		{document.forms[0].zoom2.style.top = MapWindowPositionTop + MapHeight - RectHeight - 2}
		else if (newY <= MapWindowPositionTop)
		{document.forms[0].zoom2.style.top = MapWindowPositionTop}
		else if (newY > MapWindowPositionTop)
		{document.forms[0].zoom2.style.top = newY}
		
		document.forms[0].zoom2.style.visibility = "visible";
}

}
//---------------------------------------------------------------------------------------------

//---------------------------------------------------------------------------------------------
//

function MapMove()
{

	if (moveon == true) {

		document.forms[0].zoom2.style.visibility = "visible";

//			mouseX = (event.clientX + document.body.scrollLeft);
//			mouseY = (event.clientY + document.body.scrollTop);

		mouseX = (g_mouseX + document.body.scrollLeft);
		mouseY = (g_mouseY + document.body.scrollTop);

		newX = (mouseX - 2 - StartX);
		newY = (mouseY - 2 - StartY);

		if ((newX > 0) & (newY > 0))  {
			document.forms[0].zoom2.style.left = (StartX)
			document.forms[0].zoom2.style.top = (StartY)
			document.forms[0].zoom2.style.width = (mouseX - 3 - StartX)
			document.forms[0].zoom2.style.height = (mouseY - 3 - StartY)
		}
		else if ((newX < 0) & (newY > 0))  {
			document.forms[0].zoom2.style.left = (mouseX - 2)
			document.forms[0].zoom2.style.top = StartY
			document.forms[0].zoom2.style.width = (-1 * (mouseX - 2 - StartX))
			document.forms[0].zoom2.style.height = (mouseY - 3 - StartY)
		}
		else if ((newX > 0) & (newY < 0))  {
			document.forms[0].zoom2.style.left = StartX
			document.forms[0].zoom2.style.top = (mouseY - 2)
			document.forms[0].zoom2.style.width = (mouseX - 3 - StartX)
			document.forms[0].zoom2.style.height = (-1 * (mouseY - 2 - StartY))
		}
		else if ((newX < 0) & (newY < 0))  {
			document.forms[0].zoom2.style.left = (mouseX - 2)
			document.forms[0].zoom2.style.top = (mouseY - 2)
			document.forms[0].zoom2.style.width = (-1 * (mouseX - 2 - StartX))
			document.forms[0].zoom2.style.height = (-1 * (mouseY - 2 - StartY))
		}
	}
}
//---------------------------------------------------------------------------------------------
function Map_onmousemove(event) {

	g_mouseX = (event.clientX);
	g_mouseY = (event.clientY);

	if (moveon == true)
	{MapMove();}
	else
	{
		try
		{
			mouseX = (event.clientX);
			mouseY = (event.clientY);

			mouseMapX = (event.clientX + document.body.scrollLeft) - 2 - objMapHolder.offsetLeft - parseInt(objMapHolder.style.borderWidth.replace("px",""));
			mouseMapY = (event.clientY + document.body.scrollTop) - 2 - objMapHolder.offsetTop - parseInt(objMapHolder.style.borderWidth.replace("px",""));

			doTip(mouseX,mouseY,mouseMapX,mouseMapY)
		}
		catch (e)
		{}
	}
}
//---------------------------------------------------------------------------------------------
//function Map_onmousedown() {
//	if (MapMode == 'ZoomRect')
//	{MapDown()}
//}
//---------------------------------------------------------------------------------------------
//function Map_onmouseup() {
//	if (MapMode == 'ZoomRect')
//	{MapUp()}
//}
//---------------------------------------------------------------------------------------------
function zoom2_onmousemove(event) {
	Map_onmousemove(event)
	//MapMove()
}
//---------------------------------------------------------------------------------------------
function zoom2_onclick(event) {
	MapClick(event)
}
//---------------------------------------------------------------------------------------------

function PointCenter(event)
{
	newX = (event.clientX + document.body.scrollLeft) - 2 - objMapHolder.offsetLeft - parseInt(objMapHolder.style.borderWidth.replace("px",""));
	newY = (event.clientY + document.body.scrollTop) - 2 - objMapHolder.offsetTop - parseInt(objMapHolder.style.borderWidth.replace("px",""));

	CentreXWorld = parseFloat(document.forms[0].WorldX.value);
	CentreYWorld = parseFloat(document.forms[0].WorldY.value);

	UnitsPerPixel = parseFloat(document.forms[0].WPD.value);
	bitmap_width_pixel = parseInt(document.forms[0].PixelWidth.value.replace("px",""));
	bitmap_height_pixel = parseInt(document.forms[0].PixelHeight.value.replace("px",""));

	NewWorldX = ((newX - (bitmap_width_pixel/2)) * UnitsPerPixel + CentreXWorld)
	NewWorldY = (((bitmap_height_pixel/2) - newY) * UnitsPerPixel + CentreYWorld)

	document.forms[0].MapMode.value = 'PointCenter';
	document.forms[0].X.value = NewWorldX;
	document.forms[0].Y.value = NewWorldY;

	document.forms[0].submit();

}
//---------------------------------------------------------------------------------------------
function FindPoint(event)
{
	newX = (event.clientX + document.body.scrollLeft) - 2 - objMapHolder.offsetLeft - parseInt(objMapHolder.style.borderWidth.replace("px",""));
	newY = (event.clientY + document.body.scrollTop) - 2 - objMapHolder.offsetTop - parseInt(objMapHolder.style.borderWidth.replace("px",""));

	CentreXWorld = parseFloat(document.forms[0].WorldX.value);
	CentreYWorld = parseFloat(document.forms[0].WorldY.value);

	UnitsPerPixel = parseFloat(document.forms[0].WPD.value);
	bitmap_width_pixel = parseInt(document.forms[0].PixelWidth.value.replace("px",""));
	bitmap_height_pixel = parseInt(document.forms[0].PixelHeight.value.replace("px",""));

	NewWorldX = ((newX - (bitmap_width_pixel/2)) * UnitsPerPixel + CentreXWorld)
	NewWorldY = (((bitmap_height_pixel/2) - newY) * UnitsPerPixel + CentreYWorld)

	document.forms[0].MapMode.value = 'GetPoint';
	document.forms[0].X.value = NewWorldX;
	document.forms[0].Y.value = NewWorldY;

	document.forms[0].submit();

}
//---------------------------------------------------------------------------------------------

function PointQuery(event)
{

	newX = (event.clientX + document.body.scrollLeft) - 2 - objMapHolder.offsetLeft - parseInt(objMapHolder.style.borderWidth.replace("px",""));
	newY = (event.clientY + document.body.scrollTop) - 2 - objMapHolder.offsetTop - parseInt(objMapHolder.style.borderWidth.replace("px",""));

	CentreXWorld = parseFloat(document.forms[0].WorldX.value);
	CentreYWorld = parseFloat(document.forms[0].WorldY.value);

	UnitsPerPixel = parseFloat(document.forms[0].WPD.value);
	bitmap_width_pixel = parseInt(document.forms[0].PixelWidth.value.replace("px",""));
	bitmap_height_pixel = parseInt(document.forms[0].PixelHeight.value.replace("px",""));

	NewWorldX = ((newX - (bitmap_width_pixel/2)) * UnitsPerPixel + CentreXWorld)
	NewWorldY = (((bitmap_height_pixel/2) - newY) * UnitsPerPixel + CentreYWorld)

	document.forms[0].MapMode.value = 'PointQuery';
	document.forms[0].ScreenX.value = NewWorldX;
	document.forms[0].ScreenY.value = NewWorldY;

	document.forms[0].submit();

}
//---------------------------------------------------------------------------------------------
function MeasureLine(event)
{
	
	if (event.button == 0)
	{
		var objLineCoords = document.getElementById("LineCoords");
		var objLineCoordsWorld = document.getElementById("LineCoordsWorld");

		PointX = (event.clientX + document.body.scrollLeft) - 2 - objMapHolder.offsetLeft - parseInt(objMapHolder.style.borderWidth.replace("px",""));
		PointY = (event.clientY + document.body.scrollTop) - 2 - objMapHolder.offsetTop - parseInt(objMapHolder.style.borderWidth.replace("px",""));

		CentreXWorld = parseFloat(document.forms[0].WorldX.value);
		CentreYWorld = parseFloat(document.forms[0].WorldY.value);

		UnitsPerPixel = parseFloat(document.forms[0].WPD.value);
		bitmap_width_pixel = parseInt(document.forms[0].PixelWidth.value.replace("px",""));
		bitmap_height_pixel = parseInt(document.forms[0].PixelHeight.value.replace("px",""));

		WorldX = ((PointX - (bitmap_width_pixel/2)) * UnitsPerPixel + CentreXWorld)
		WorldY = (((bitmap_height_pixel/2) - PointY) * UnitsPerPixel + CentreYWorld)

		if (objLineCoords.value == '')
		{
			objLineCoords.value = PointX + ',' + PointY;
			objLineCoordsWorld.value = WorldX + ',' + WorldY;
		}
		else
		{
			objLineCoords.value = objLineCoords.value + ';' + PointX + ',' + PointY;
			objLineCoordsWorld.value = objLineCoordsWorld.value + ';' + WorldX + ',' + WorldY;

			if (MapMode == 'MeasureLine')
			{document.forms[0].MapMode.value = 'MeasureLine';}
			else if (MapMode == 'MeasurePolygon')
			{document.forms[0].MapMode.value = 'MeasurePolygon';}
			document.forms[0].submit();
		}
	}
}

//---------------------------------------------------------------------------------------------
objMapHolder.oncontextmenu = function CloseMeasureLine(event)
{
	var objLineCoordsWorld = document.getElementById("LineCoordsWorld");

	if (objLineCoordsWorld.value != '')
	{
		if (MapMode == 'MeasureLine')
		{document.forms[0].MapMode.value = 'GetDistance';}
		else if (MapMode == 'MeasurePolygon')
		{document.forms[0].MapMode.value = 'GetArea';}
		document.forms[0].submit();
	}
	return false;
};
//---------------------------------------------------------------------------------------------


    function grabEl() {
		objMap.style.cursor = "url(Images/H_MOVE.CUR), move";
	    whichEl = event.srcElement;
	
	    while (whichEl.id.indexOf("MAP") == -1) {
	        whichEl = whichEl.parentElement;
	        if (whichEl == null) { return }
	    }
	
	    if (whichEl != activeEl) {
	        whichEl.style.zIndex = activeEl.style.zIndex + 1;
	        activeEl = whichEl;
	    }

	    whichEl.style.pixelLeft = whichEl.offsetLeft;
	    whichEl.style.pixelTop = whichEl.offsetTop;

	    currentX = ((event.clientX + document.body.scrollLeft) + document.body.scrollLeft);
	    currentY = ((event.clientY + document.body.scrollTop) + document.body.scrollTop);
	}
//---------------------------------------------------------------------------------------------
    function moveEl() {
	    if (whichEl == null) { return };

	    newX = ((event.clientX + document.body.scrollLeft) + document.body.scrollLeft);
	    newY = ((event.clientY + document.body.scrollTop) + document.body.scrollTop);
	
	    distanceX = (newX - currentX);
	    distanceY = (newY - currentY);
	    currentX = newX;
	    currentY = newY;
	
	    whichEl.style.pixelLeft += distanceX;
	    whichEl.style.pixelTop += distanceY;
	    event.returnValue = false;
	}
//---------------------------------------------------------------------------------------------
function dropEl() {
    objMap.style.cursor = "default";
	whichEl = null;
	doMapPan();
}
//---------------------------------------------------------------------------------------------
document.onselectstart = checkEl;
//---------------------------------------------------------------------------------------------
function checkEl() {
	if (whichEl!=null) { return false }
}
//---------------------------------------------------------------------------------------------
function cursEl() {
	if (event.srcElement.id.indexOf("MAP") != -1) { 
		objMap.style.cursor = "url(Images/H_POINT.CUR), move";
	}
}
//---------------------------------------------------------------------------------------------
function doFrameEnd()
{
	if (whichEl!=null)
	{dropEl();	}
}
//---------------------------------------------------------------------------------------------
	function doMapPan()
	{

		CentreXWorld = parseFloat(document.forms[0].WorldX.value);
		CentreYWorld = parseFloat(document.forms[0].WorldY.value);

		UnitsPerPixel = parseFloat(document.forms[0].WPD.value);

		newX = CentreXWorld - ((objMap.offsetLeft) * UnitsPerPixel);
		newY = CentreYWorld - ((-1 * (objMap.offsetTop)) * UnitsPerPixel);

		NewWorldX = newX;
		NewWorldY = newY;

		document.forms[0].MapMode.value = 'PanMap';
		document.forms[0].X.value = NewWorldX;
		document.forms[0].Y.value = NewWorldY;

		document.forms[0].submit();
	}
//---------------------------------------------------------------------------------------------
function QueryAll()
{
	document.forms[0].MapMode.value = 'QueryAllInView';
	document.forms[0].submit();
}
//---------------------------------------------------------------------------------------------
