
// -----------------------------------------------------------------
//						Redland Shire Council 
// -----------------------------------------------------------------
// Class    : EventHandler 
// Purpose  : Handles mouse move/click or key press etc events.
// -----------------------------------------------------------------
// Calls    :
// Called by: 
// -----------------------------------------------------------------
// Arguments:
// Globals  :
// Returns  :
// -----------------------------------------------------------------
// Notes    :
// -----------------------------------------------------------------
// History  : Client side code used for handling all events.
// =================================================================

//Variable to store the x/y click of the mouse when zooming in
var beginXClick = 0;
var beginYClick = 0;

//Variable used to hold the current x/y position of the mouse as it moves
var eventY = 0;
var eventX = 0;

//The map offset in pixels. This value represents how far down the client window the actual
//map window starts. Eg, if there is a toolbar, this value may be approximately 50 or 60 px.
//This value is automatically calculated and does not need to be explicitly set.
var imgMapYOffset;
var imgMapXOffset;

//The dimensions of the map image, set dynamically depending on the client's browser size
//Initially initialised to 0
var imgMapWidth = 0;
var imgMapHeight = 0;

//Arrays to hold the x and y clicks when using the measure tool
var xClicks = new Array();
var yClicks = new Array();

//Arrays to hold the Map corrdinate equivalents of the clicked points in the measure tool
// these values are posted back so that the measure tool can be recreated using the previous
// clicked points
var xClicksMap = new Array();
var yClicksMap = new Array();

var shiftKeyHeld = false;
var bBrowserError=false;		// is there an error displaying in the users browser
var bDisplayedError = false;	// has the error been displayed already (prevent from displaying twice)

//Check the client browser version and return this as a string
function checkBrowser()
{
	// there is a javascript issue with the speed of the page loading, the onmousemove events and onmousedown events
	// are sometimes attached and fired before the form has generated producing an error.  to ensure the event isn't fired
	// until the form has finished loading check that the current tool textbox exists before continuing.
	if (document.getElementById('txtCurrentTool'))
	{
		// used to create a list of the supported browsers
		var sSupportedBrowsers="";

		// first check if the browser is supported. if it isn't supported, redirect to a holding page.
		for (var i=0;i<arSupportedBrowser.length;i++)
		{
			if (navigator.userAgent.indexOf(arSupportedBrowser[i]) != -1) 
			{
				// browser is supported, no need to continue
				bBrowserError=false;
				break;
			}
			else
			{
				// unsupported browser.
				// to prevent the alert from displaying multiple times.
				bBrowserError=true;
			}
			sSupportedBrowsers+=displayBrowser(arSupportedBrowser[i]);
			if (i<arSupportedBrowser.length-1) sSupportedBrowsers+=", ";
		}
		
		if (bBrowserError && !bDisplayedError)
		{
			bDisplayedError=true;
			alert("Your browser does not support DHTML. We recommend that you upgrade your web browser.\n\nThe following browsers are supported: "+sSupportedBrowsers);
			// redirect to the error page
			document.location.href='error.aspx?error=browser';
			return null;
		}
		
		if (navigator.userAgent.indexOf('Opera') != -1)
		{
			//Opera Browser
			return "opera";
		}
		else if (navigator.userAgent.indexOf("Netscape") != -1 || navigator.userAgent.indexOf("Firefox") != -1)
		{
			//Netscape or firefox browser
			return "firefox";
		}
		else if (navigator.userAgent.indexOf('MSIE') != -1)
		{
			//Internet Explorer
			return "ie";
		}
		else
		{
			//Unsupported browser
			alert("Your browser does not support DHTML. We recommend that you upgrade your web browser.");
			return null;
		}
	}
}


//Call the appropriate MouseDown event handler depending on which browser is being used.
function MouseDown(event)
{
	switch (checkBrowser())
	{	
		case "opera":
			operaMouseDown();
			break;
			
		case "firefox":
			//alert('in mouse down firefox')
			if (event.button==0)
			{
				if (document.getElementById('txtCurrentTool').value == 'ZoomIn')
				{
					firefoxMouseDownZoomIn(event,'in');
				}
				else if (document.getElementById('txtCurrentTool').value == 'ZoomOut')
				{
					firefoxMouseDownZoomIn(event,'out');
				}
				else if (document.getElementById('txtCurrentTool').value == 'Pan')
				{
					firefoxMouseDownPan(event);
				}
				else if (document.getElementById('txtCurrentTool').value == 'Select')
				{
					firefoxMouseDownZoomIn(event,'select');
				}
				else if (document.getElementById('txtCurrentTool').value == 'Coord')
				{
					ieMouseDownCoord(event);
				}
			}
			break;
			
		case "ie":
			if (event.button==1)
			{
				//Check which map tool is being used, and call the appropriate function
				if (document.getElementById('txtCurrentTool').value == 'ZoomIn')
				{
					ieMouseDownZoomIn(event);
				}
				else if (document.getElementById('txtCurrentTool').value == 'ZoomOut')
				{
					ieMouseDownZoomIn(event);
				}
				else if (document.getElementById('txtCurrentTool').value == 'Pan')
				{
					ieMouseDownPan(event);
				}
				else if (document.getElementById('txtCurrentTool').value == 'Select')
				{
					ieMouseDownZoomIn(event);
				}
				else if (document.getElementById('txtCurrentTool').value == 'Coord')
				{
					ieMouseDownCoord(event);
				}
			}
			break;
			
		default:
			//Nothing
	}
}


//Call the appropriate MouseMove event handler depending on which browser is being used.
function MouseMove(event)
{	
	switch (checkBrowser())
	{
		case "opera":
		break;
		
		case "firefox":
			if (document.getElementById('txtCurrentTool').value == 'Coord')
			{	
				ieMouseMoveCoord(event);
			}
		break;

		case "ie":
			if (event.button==1)
			{
				//Check which map tool is being used, and call the appropriate function
				if (document.getElementById('txtCurrentTool').value == 'ZoomIn')
				{
					ieMouseMoveZoomIn(event);
				}
				else if (document.getElementById('txtCurrentTool').value == 'ZoomOut')
				{
					ieMouseMoveZoomIn(event);
				}
				else if (document.getElementById('txtCurrentTool').value == 'Pan')
				{
					ieMouseMovePan(event);
				}
				else if (document.getElementById('txtCurrentTool').value == 'Select')
				{
					ieMouseMoveZoomIn(event);
				}
				else if (document.getElementById('txtCurrentTool').value == 'Coord')
				{	
					ieMouseMoveCoord(event);
				}
			}
			else
			{
				//coord mouse move works all the time on mouse move, not just if mouse button is clicked
				if (document.getElementById('txtCurrentTool').value == 'Coord')
				{	
					ieMouseMoveCoord(event);
				}
			}
		break;
	}
}

//Call the appropriate MouseMove event handler depending on which browser is being used.
function MouseMoveBody(event)
{	
	switch (checkBrowser())
	{	
		case "opera":
			//operaMouseMove();
			break;
			
		case "firefox":
			//firefoxMouseMove(event);
			break;
			
		case "ie":
			//Check which map tool is being used, and call the appropriate function
			//Remember that only certain tools are accessed from the body mouse move.
			if (document.getElementById('txtCurrentTool').value == 'Measure')
			{
				ieMouseMoveMeasure(event);
			}
			break;
			
		default:
			//Nothing
	}
}

//Call the appropriate Key Press function.
function KeyPressBody()
{	
	switch (checkBrowser())
	{	
		case "opera":
			//operaKeyPress();
			break;
			
		case "firefox":
			//firefoxKeyPress();
			break;
			
		case "ie":
			if (window.event.keyCode==27)
			{
				// escape key was pressed cancel any current rubber band - zoom/select
				if (mouseMoving==1)
				{
					CancelRubberBand();
				}
				else if (document.getElementById('txtCurrentTool').value=='Measure')
				{
					// stop the measyre tool
					ieDoubleClickMeasure();
				}
			}
			break;
			
		default:
			//Nothing
	}
	
}


//Call the appropriate MouseUp event handler depending on which browser is being used.
function MouseUp(event)
{
	switch (checkBrowser())
	{
		case "opera":
			operaMouseUp();
			break;
			
		case "firefox":
			if (event.button==0)
			{
				if (document.getElementById('txtCurrentTool').value == 'AddText')
				{
					ieMouseUpAddText(event);
				}
				else if (document.getElementById('txtCurrentTool').value == 'Identify')
				{
					ieMouseUpIdentify(event);
				}
			}
			break;
			
		case "ie":
			if (event.button==1)
			{
				//Check which map tool is being used, and call the appropriate function
				if (document.getElementById('txtCurrentTool').value == 'ZoomIn')
				{
					ieMouseUpZoomIn(event);
				}
				else if (document.getElementById('txtCurrentTool').value == 'ZoomOut')
				{
					ieMouseUpZoomIn(event);
				}
				else if (document.getElementById('txtCurrentTool').value == 'Pan')
				{
					ieMouseUpPan(event);
				}
				else if (document.getElementById('txtCurrentTool').value == 'Identify')
				{
					ieMouseUpIdentify(event);
				}
				else if (document.getElementById('txtCurrentTool').value == 'Select')
				{
					ieMouseUpSelect(event);
				}
				else if (document.getElementById('txtCurrentTool').value == 'AddText')
				{
					ieMouseUpAddText(event);
				}
				else if (document.getElementById('txtCurrentTool').value == 'AsCon')
				{
					ieMouseUpAsCon(event);
				}
			}
			break;
			
		default:
			//Nothing
	}
}

//Call the appropriate MouseUp event handler depending on which browser is being used.
function MouseUpBody()
{
	switch (checkBrowser())
	{
		case "opera":
			//operaMouseUp();
			break;
			
		case "firefox":
			//firefoxMouseUp();
			break;
			
		case "ie":
			//Check which map tool is being used, and call the appropriate function
			if (document.getElementById('txtCurrentTool').value == 'Measure')
			{
				ieMouseUpMeasure(event);
			} 
			else if (document.getElementById('txtCurrentTool').value == 'MeasureOff')
			{
				resetMeasure();
				document.getElementById('txtCurrentTool').value = 'Measure';
				ieMouseUpMeasure(event);
			}
			break;
			
		default:
			//Nothing
	}
}

function DoubleClickBody(event)
{
	switch (checkBrowser())
	{
		case "opera":
			//operaMouseUp();
			break;
			
		case "firefox":
			//firefoxMouseUp();
			break;
			
		case "ie":
			//Check which map tool is being used, and call the appropriate function
			if (document.getElementById('txtCurrentTool').value == 'Measure')
			{
				ieDoubleClickMeasure(event);
			}
			break;
			
		default:
			//Nothing
	}
}

function MouseDownBody(event)
{
	switch (checkBrowser())
	{
		case "opera":
			//operaMouseUp();
			break;
			
		case "firefox":
			//firefoxMouseUp();
			break;
			
		case "ie":
			//Check which map tool is being used, and call the appropriate function
			if (document.getElementById('txtCurrentTool').value == 'help')
			{
				document.getElementById('bodytag').style.cursor = 'default';
				//alert('display help');
				document.getElementById('txtCurrentTool').value = '';
			}
			break;
			
		default:
			//Nothing
	}
}

function CancelRubberBand()
{
	//reset the state of the mouse moving
	mouseMoving = 0;
	
	//Hide the zoom rubber band
	document.getElementById('divZoomBox').style.visibility = 'hidden';
	
	//Release mouse capture as no more dragging necessary
	if (document.getElementById('imgMap').releaseCapture)
	{
		document.getElementById('imgMap').releaseCapture();
	}

}

// if the common iframe exists return a reference to the document in the Iframe.
function getIframeDoc()
{
	var iFrameObj = document.getElementById('iframeCommon');
	var iFrameDoc;
	
	// only continue if the iFrame object exists.
	if (iFrameObj) {
		if (iFrameObj.contentDocument) {
			// For NS6
			iFrameDoc = iFrameObj.contentDocument; 
		} else if (iFrameObj.contentWindow) {
			// For IE5.5 and IE6
			iFrameDoc = iFrameObj.contentWindow.document;
		} else if (iFrameObj.document) {
			// For IE5
			iFrameDoc = iFrameObj.document;
		}
		
		if (iFrameDoc.forms.length>0) {
			return iFrameDoc;
		}
	}
	// if the Iframe Reference isn't returned return false
	return false;
}



//MouseUp event handler for FireFox
function firefoxMouseUp()
{
	//YET TO BE IMPLEMENTED FULLY
	mouseMoving = 0;
	document.getElementById('divZoomBox').style.visibility='hidden';
}

//MouseDown event handler for Opera
function operaMouseDown()
{
	//YET TO BE IMPLEMENTED FULLY
	mouseMoving = 1;
	eval('document.all.' + 'divZoomBox').style.visibility='visible';
}

//MouseMove event handler for Opera
function operaMouseMove()
{
	//YET TO BE IMPLEMENTED FULLY
	if (mouseMoving==1)
	{
		eval('document.all.' + 'divZoomBox').style.width = parseInt(eval('document.all.' + 'divZoomBox').style.width) + 1;
	}
}
	
//MouseUp event handler for Opera
function operaMouseUp()
{
	//YET TO BE IMPLEMENTED FULLY
	mouseMoving = 0;
	eval('document.all.' + 'divZoomBox').style.visibility='hidden';
}

// simple function that can be used for loading a specific help topic and closing the error div if it was open
function help(page)
{
	document.getElementById('divError').style.visibility='hidden';
	// load the requested help page into the help page
	showCommonDiv('Help',750,400,0,10,'help.aspx?help='+page,true,false,true);
}

function initForm()
{
	// only display the VML tags if this is Internet Explorer (as the tags will prevent 
	//Javascript from executing in other browsers)
	if (checkBrowser()=="ie")
	{
		var vml='<v:shape style="Z-INDEX:999;LEFT:0px;WIDTH:1000px;POSITION:absolute;TOP:0px;HEIGHT:1000px" id="areaShape" strokecolor="red" filled="f" strokeweight=".75pt" coordsize="1000,1000"></v:shape>';
		vml+='<v:shape style="Z-INDEX:999;LEFT:0px;WIDTH:1000px;POSITION:absolute;TOP:0px;HEIGHT:1000px" id="areaClosingLine" strokecolor="blue" filled="f" strokeweight=".75pt" coordsize="1000,1000"></v:shape>';
		document.getElementById('divImgMap').innerHTML=vml+document.getElementById('divImgMap').innerHTML;
	}

	// also check if there is a current measure tool in progress... if there is recreate the
	// click arrays from the posted form field in map coords.
	if (xClicksMap.length>0)
	{
		xClicksMap=xClicksMap.split(",");
		yClicksMap=yClicksMap.split(",");
		if (browserToolCheck(arMeasure)) 
		{
			showCommonDiv('Measure',180,120,300,63,'Measure.htm',true,false,true);
			highlightMe(document.getElementById('imgMeasure'));
			document.getElementById('imgMap').style.cursor='crosshair';
			document.getElementById('txtCurrentTool').value='Measure';
			loadMeasurePoints();
		}
	}
}

// function to check if a tool is available in the users browser.
function browserToolCheck(ar)
{
	// used to create a list of the supported browsers
	var sSupportedBrowsers="";
	var bError=true;

	// first check if the browser is supported. if it isn't supported, redirect to a holding page.
	for (var i=0;i<ar.length;i++)
	{
		if (navigator.userAgent.indexOf(ar[i]) != -1) 
		{
			// browser is supported, no need to continue
			bError=false;
			break;
		}
		sSupportedBrowsers+=displayBrowser(ar[i]);
		if (i<ar.length-1) sSupportedBrowsers+=", ";
	}
	
	if (bError)
	{
		alert("This function is not currently supported in your browser. \n\nThe following browsers support this function: "+sSupportedBrowsers);
		return false;
	}
	return true;
}

function displayBrowser(str)
{
	if (str=="MSIE")
	{
		return "Internet Explorer";
	}
	else
	{
		return str;
	}
}