var mtZoomIn = "mtZoomIn";
var mtZoomOut = "mtZoomOut";
var mtPan = "mtPan";
var mtDrawPolygon = "mtDrawPolygon";
var mtDrawFilledPolygon = "mtDrawFilledPolygon";
var mtDrawCircle = "mtDrawCircle";
var mtDrawFilledCircle = "mtDrawCircle";
var mtDrawText = "mtDrawText";
var mtDrawLine = "mtDrawLine";
var mtDrawPolyline = "mtDrawPolyline";
var mtSelect = "mtSelect";
var mtSelectRectangle = "mtSelectRectangle";
var mtSelectPolygon = "mtSelectPolygon";
var mtMeasure = "mtMeasure";
var mtIdentify = "mtIdentify";
var mtDrawRectangle = "mtDrawRectangle";
var mtCustom = "mtCustom";
var mtDrawFilledRectangle = "mtDrawFilledRectangle";

var mapImageWidth = 0;
var mapImageHeight = 0;

var downX;
var downY;
var lastX;
var lastY;

var docDownX;
var docDownY;

var mapTool = mtZoomIn;

var mouseDown=false;
var lineTrace=false;
var absScreenX;
var absScreenY;
var relScreenX;
var relScreenY;

var relDownX;
var relDownY;
var relUpY;
var relUpY;

var absDownX;
var absDownY;
var absUpX;
var absUpY;

var drawArrayX = new Array();
var drawArrayY = new Array();

var drawCount = 0;

var mapLoaded = false;

var totalMeasureDistance = 0;


function fireEvent(eventName, parameters)
{
//if (typeof(FunctionImade) == 'function')

	if (eval('window.'+eventName))
		result = eval(eventName+parameters);
}

function mapControlEvents(events)
{
	this.InitializeMenu=events[0];
	this.ItemCheck=events[1];
	this.ItemClick=events[2];
	this.SubMenuDisplay=events[3];
	this.ItemHover=events[4];
}

function docMouseDown(e)
{

	shiftCanvas();
	if (isIE) e = event;
	getAbsoluteScreenPoint(e);
	
	docDownX = absScreenX;
	docDownY = absScreenY;
	
	if (getCurrentMapTool() == mtPan)
	{			
		var obj = document.getElementById(mapControlId);
		var dx = findPosX(obj);
		var dy = findPosY(obj);

		moveLayer("panCanvas",dx+1,dy+1);
		moveLayer("panImage",dx+1,dy+1);
		mapUrl = document.getElementById(mapControlId).src;
		
		document.getElementById("panImage").src = mapUrl;
	}
}

function docMouseMove(e)
{
		if (isIE) e = event;
		getAbsoluteScreenPoint(e);
			
		moveX = docDownX - absScreenX;
		moveY = docDownY - absScreenY;

	if ( mouseDown)
	{		
		if (getCurrentMapTool() == mtPan && mouseDown)
		{
			var obj = document.getElementById(mapControlId);
			var dx = findPosX(obj);
			var dy = findPosY(obj);
			
			showLayer("panImage");
			hideLayer(mapControlId);
			showLayer("panCanvas");

			moveX2 = moveX - dx;
			moveY2 = moveY - dy;
			moveLayer("panShift",-moveX,-moveY);
			moveLayer("mapCanvas",-moveX2,-moveY2);
		}
	}
}
				
function getAbsoluteScreenPoint(e)
{
	
	if (isNav) {
		absScreenX=e.pageX;
		absScreenY=e.pageY;		
	} else {
		absScreenX=e.clientX + document.body.scrollLeft-2;
		absScreenY=e.clientY + document.body.scrollTop-2;
	}
}

function getRelativeScreenPoint(obj,e)
{
	var dx = findPosX(obj);
	var dy = findPosY(obj);

	if (e.pageX || e.pageY){
		mouseX=e.pageX;
		mouseY=e.pageY;
	} else if (e.clientX || e.clientY) {
		mouseX=e.clientX + document.body.scrollLeft-2;
		mouseY=e.clientY + document.body.scrollTop-2;
	}
	
	relScreenX = mouseX-dx;
	relScreenY = mouseY-dy;
}

function mapMouseDown(obj,e)
{
	getAbsoluteScreenPoint(e);
	getRelativeScreenPoint(obj,e);
	
	absDownX = absScreenX;
	absDownY = absScreenY;
	relDownX = relScreenX;
	relDownY = relScreenY;

	zoomCanvas.clear();
	mouseDown=true;
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;	
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function mapMouseMove(obj,e)
{

		getRelativeScreenPoint(obj,e);
		
		moveX = relScreenX;
		moveY = relScreenY;
		
	
	//if (mapLoaded)
	//window.status=Math.round(screenToWorldX(moveX))+','+Math.round(screenToWorldY(moveY));
	if ( mouseDown)
	{

	
		if (getCurrentMapTool() == mtZoomIn || getCurrentMapTool() == mtZoomOut)
		{		
			var width = Math.abs(moveX - relDownX);
			var height = Math.abs(moveY - relDownY);

			//find the top left corner
			var left = relDownX;
			var top = relDownY;
			
			if (moveX<relDownX) var left = moveX;
			if (moveY<relDownY) var top = moveY;
			
			zoomCanvas.clear();
			var colour = getDrawColour();
			zoomCanvas.setColor(colour);	
			zoomCanvas.drawRect(left,top,width,height);
			zoomCanvas.paint();	
		}
		else if (getCurrentMapTool() == mtDrawRectangle || getCurrentMapTool() == mtSelectRectangle)
		{
			var width = Math.abs(moveX - relDownX);
			var height = Math.abs(moveY - relDownY);

			//find the top left corner
			var left = relDownX;
			var top = relDownY;
			
			if (moveX<relDownX) var left = moveX;
			if (moveY<relDownY) var top = moveY;
			
			mapCanvas.clear();
			if (getCurrentMapTool() == mtDrawRectangle)
			{
				var colour = getDrawColour();
				var lineWidth = getDrawLineWidth();	
			}
			else
			{
				var colour = '#0000ff';
				var lineWidth = 2;				
			}
			
			mapCanvas.setColor(colour);
			mapCanvas.setStroke(lineWidth);	
			mapCanvas.drawRect(left,top,width,height);
			mapCanvas.paint();	
		}
		else if (getCurrentMapTool() == mtDrawFilledRectangle)
		{
			var width = Math.abs(moveX - relDownX);
			var height = Math.abs(moveY - relDownY);

			//find the top left corner
			var left = relDownX;
			var top = relDownY;
			
			if (moveX<relDownX) var left = moveX;
			if (moveY<relDownY) var top = moveY;
			
			mapCanvas.clear();
			
			var colour = getDrawColour();
			var lineWidth = getDrawLineWidth();
			mapCanvas.setColor(colour);
			mapCanvas.setStroke(lineWidth);	
			mapCanvas.fillRect(left,top,width,height);
			mapCanvas.paint();	
		}

	}
	
		/*if (getCurrentMapTool() == mtMeasure)
		{
			getAbsoluteScreenPoint(e);
			moveLayer('MeasureDiv',absScreenX,absScreenY);
		}*/
		
	if (lineTrace)
	{		
		lineTraceCanvas.clear();
		if (getCurrentMapTool() == mtSelectPolygon)
		{
			var colour = '#0000ff';
			var lineWidth = 2;
		}
		else
		{
			var colour = getDrawColour();
			var lineWidth = getDrawLineWidth();
		}
		if (getCurrentMapTool() == mtMeasure)
		{
			var colour = '#ff0000';
			var lineWidth = 2;
			
			var worldX = screenToWorldX(moveX);
			var worldY = screenToWorldY(moveY);
			if (drawCount>0)
			{
				var lastWorldX = screenToWorldX(drawArrayX[(drawCount-1)]);
				var lastWorldY = screenToWorldY(drawArrayY[(drawCount-1)]);
				var dX = (worldX - lastWorldX);
				var dY = (worldY - lastWorldY);
			}
			else
			{
				var dX=0;
				var dY=0;
			}
			
			var dist = Math.sqrt(Math.pow(dX,2)+Math.pow(dY,2));
			fireEvent('MeasureClick','('+totalMeasureDistance+','+dist+')');
		}
		lineTraceCanvas.setColor(colour);
		lineTraceCanvas.setStroke(lineWidth);	
		lineTraceCanvas.drawLine(relDownX,relDownY,moveX,moveY);
		lineTraceCanvas.paint();	
	}

	


}

function isDoubleClick(e)
{
	dblClickInterval=3000;
	if((e.clientX==lastX) && (e.clientY==lastY) && (elapsedTime<=dblClickInterval)){
		return true;	
	} else {
		return false;
	}
}

function getLayer(name) 
{
	var theObj = document.getElementById(name);
	if (theObj!=null) return theObj.style;
	  else return(null);
}

// toggle layer to invisible
function hideLayer(name) 
{
  	var layer = getLayer(name);
   	if (layer!=null) layer.visibility = "hidden";
	return false;
}

// toggle layer to visible
function showLayer(name) 
{
  	var layer = getLayer(name);
   	if (layer!=null) layer.visibility = "visible";
	return false;
}

// move layer to x,y
function moveLayer(name, x, y) 
{		
  	var layer = getLayer(name);		
    layer.left = x + "px";
   	layer.top  = y + "px";
}

function getMapControlX()
{
	var obj = document.getElementById(mapControlId);
	var dx = findPosX(obj);
	return dx;
}

function getMapControlY()
{
	var obj = document.getElementById(mapControlId);
	var dy = findPosY(obj);
	return dy;
}

function stopMeasure()
{
	lineTrace=false;
	drawArrayX.length=0;
	drawArrayY.length=0;
	drawCount=0;
	totalMeasureDistance = 0;
	dist=0;
}

function resetMeasure()
{
	lineTrace=false;
	drawArrayX.length=0;
	drawArrayY.length=0;
	drawCount=0;
	totalMeasureDistance = 0;
	dist=0;
	mapCanvas.clear();
	lineTraceCanvas.clear();

}



