// -----------------------------------------------------------------
//						Redland Shire Council 
// -----------------------------------------------------------------
// Class    : LegendMove 
// Purpose  : Handles moving the dynamic legend div layer around.
// -----------------------------------------------------------------
// Calls    :
// Called by: transLegend's mouse events within default.aspx
// -----------------------------------------------------------------
// Arguments:
// Globals  :
// Returns  :
// -----------------------------------------------------------------
// Notes    : This JavaScript file contains client side code to
//			  allow the Legend div tag to be dragged anywhere
//			  within the main map window and released.
// -----------------------------------------------------------------
// History  :
// =================================================================

//Variable to store the start x/y click when moving the legend
var beginLegendXClick = 0;
var beginLegendYClick = 0;

//MouseDown event handler for dragging the legend
function LegendMouseDown(event)
{
	//Set the legend as moving only if the left button was clicked
	if (event.button == 1)
	{
		legendMoving = 1;
	}
		
	//Set variables to be used in LegendMouseMove
	beginLegendXClick = event.clientX;
	beginLegendYClick = event.clientY;
	
	//Start a capture over the image so that IE does not try and drag'n'drop it
	if (document.getElementById('imgLegend').setCapture)
	{
		document.getElementById('imgLegend').setCapture();
	}
}

//MouseDown event handler for dragging the legend window
function LegendMouseMove(event)
{

	//If the window is currently moving... (being dragged)
	//The legendMoving variable is set locally within default.aspx
	if (legendMoving == 1)
	{	
	
		//Only adjust the position of the legend if it's within the main map window
		//ie, don't allow it to go off the sides or top/bottom of the screen
		if (event.clientX < (parseInt(document.body.clientWidth)) && event.clientY < (parseInt(document.body.clientHeight)) - 20 && event.clientX > 0 && event.clientY > 0)
		{			
			//Update the left and top properties of the div tag as the mouse moves and drags		
			document.getElementById('transLegend').style.left = parseInt(document.getElementById('txtLegendLeft').value) + event.clientX - beginLegendXClick;
			document.getElementById('transLegend').style.top = parseInt(document.getElementById('txtLegendTop').value) + event.clientY - beginLegendYClick;
		}
	}
}

//LegendMouseUp event handler
function LegendMouseUp(event)
{
	//Stop the legend from moving
	legendMoving = 0;
	
	//If the left mouse button has been clicked...
	if (event.button == 1)
	{
		//Update the current left and top values for the legend in the hidden form controls
		document.getElementById('txtLegendLeft').value = parseInt(document.getElementById('txtLegendLeft').value) + event.clientX - beginLegendXClick;
		document.getElementById('txtLegendTop').value = parseInt(document.getElementById('txtLegendTop').value) + event.clientY - beginLegendYClick;
	}
	
	//Release mouse capture as no more dragging necessary
	if (document.getElementById('imgLegend').releaseCapture)
	{
		document.getElementById('imgLegend').releaseCapture();
	}
}

function toggleLegend()
{
	//Toggle hiding and showing of the legend div layer.
	//NB: Legend is always requested and drawn, this function only
	//toggles the state of the CSS visibility.

	//If the legend is currently visible...
	if (document.getElementById('transLegend').style.visibility == 'visible')
	{
		//...Then hide it and set the invisible form control to represent this state
		document.getElementById('transLegend').style.visibility = 'hidden';
		document.getElementById('txtLegendOnOff').value = "Off";
	}
	else
	{
		//...Otherwise show it and set the invisible form control to represent this state
		document.getElementById('transLegend').style.visibility = 'visible';
		document.getElementById('txtLegendOnOff').value = "On";
	}
}

function refreshLegendVisibility()
{
	//If user has hidden legend, set it to hidden
	//This function is called after a postback
	if (document.getElementById('txtLegendOnOff').value == "On")
	{
		//Show the legend div tag
		document.getElementById('transLegend').style.visibility = 'visible';
	}
}

//This function ensures the legend always remains in a sensible position,
//and doesn't extent outside any of the map window boundaries.
function adjustLegendPosition()
{
	
	//If the window is resized, snap the legend to the bottom right corner
	if (document.getElementById('txtCurrentTool').value == 'ResizeMapWindow')
	{
		//Set the top of the legend to be the height of the window - the height of the legend
		document.getElementById('txtLegendTop').value = document.getElementById('divImgMap').clientHeight - document.getElementById('transLegend').clientHeight - 5;
	
		//If the legend is greater than 300px wide, ie it needs a horizonal scrollbar...
		if (document.getElementById('transLegend').clientWidth > 300)
		{
			//Set the left position of the legend to be flush with the right-hand side of the window
			document.getElementById('txtLegendLeft').value = document.getElementById('divImgMap').clientWidth - 300 - 5;
		}
		else
		//Legend is 300px wide or less
		{
			//Set the left position of the legend to be flush with the right-hand side of the window, based on the actual width of the legend image
			document.getElementById('txtLegendLeft').value = document.getElementById('divImgMap').clientWidth - document.getElementById('transLegend').clientWidth - 5;
		}
	}
	
	
	//If the legend is below bottom of the screen, move it up so it's completely visible
	if ((parseInt(document.getElementById('txtLegendTop').value) + parseInt(document.getElementById('transLegend').clientHeight)) > document.getElementById('divImgMap').clientHeight)
	{
		//Calculate the top of the legend position to be at the top of the map window
		document.getElementById('txtLegendTop').value = document.getElementById('divImgMap').clientHeight - document.getElementById('transLegend').clientHeight - 5;
	}
	
	
	//If legend is outside the right of the screen, move it left so it's completely visible
	if ((parseInt(document.getElementById('txtLegendLeft').value) + parseInt(document.getElementById('transLegend').clientWidth)) > document.getElementById('divImgMap').clientWidth)
	{
		if (document.getElementById('transLegend').clientWidth > 300)
		//Legend has horizontal scrollbar
		{
			document.getElementById('txtLegendLeft').value = document.getElementById('divImgMap').clientWidth - 300 - 5;
		}
		else
		//Legend is 300px wide or less
		{
			document.getElementById('txtLegendLeft').value = document.getElementById('divImgMap').clientWidth - document.getElementById('transLegend').clientWidth - 5;
		}
	}
				
	//Update the position of the legend with the values which have been placed in the hidden form fields
	document.getElementById('transLegend').style.left = document.getElementById('txtLegendLeft').value;
	document.getElementById('transLegend').style.top = document.getElementById('txtLegendTop').value;
	
	try
	{
	
		if (document.getElementById('transLegend')&&document.getElementById('divImgMap')&&document.getElementById('imgLegend'))
		{
			//If the legend is taller than the map window's height, shrink it so that a vertical
			//scroll bar is displayed and snap it to the top of the screen
			if (document.getElementById('transLegend').clientHeight > document.getElementById('divImgMap').clientHeight && document.getElementById('divImgMap').clientHeight!=0)
			{
				document.getElementById('divImgLegend').style.height = document.getElementById('divImgMap').clientHeight - 25;
				document.getElementById('transLegend').style.width = document.getElementById('imgLegend').width;
				
				document.getElementById('txtLegendTop').value = 2;
				document.getElementById('transLegend').style.top = document.getElementById('txtLegendTop').value;
			}
		}
		
		if (document.getElementById('transLegend')&&document.getElementById('divImgLegend'))
		{
			//Don't allow the Legend to be wider than 300 pixels
			if (document.getElementById('transLegend').clientWidth > 300)
			{
				document.getElementById('divImgLegend').style.width = '300px';
				document.getElementById('transLegend').style.width = '300px';
			}
		}
	}
	catch (exc)
	{
		// sometimes resetting the legend height generates an error.
	}
		
}