//------------------------------
// Mouseover scripts for buttons
//------------------------------
// This module reads through the toolbar arrays in globarVars.js
// and creates icons and mouseovers for them and writes them to the toolbar
// To add new buttons, just insert the items to the end of the arrays
// Then add the click handlers to the function "SelectTool" in mapFunctions.js

function loadToolBar()
{
	// This writes the actual icons to the page
	// including the mouseover/out routines and the onclick functions
	output = "";
	for (i=0;i<numTools-1;i++)
	{
		if (typeof toolArray[i] != "undefined" && toolArray[i] != "")
		{
			output += '<a';
			output += '  onclick="selectTool(' + i + ')"';
			output += '  onmouseover="mouseOver(' + i + ')"';
			output += '  onmouseout="mouseOut(' + i + ')">';
			output += '<img';
			output += '  id="' + toolArray[i] + '"';
			output += '  alt="' + toolArray_Alt[i] + '"';
			if (toolArray_Disabled[i])
			{
				output += '  src="' + toolArray_Disabled[i].src + '"';
			}
			else
			{
				output += '  src="' + toolArray_Off[i].src + '"';
			}
			output += '  border="0"></a>';
		}
	}
	toolBarIcons.innerHTML = output;
	// Select the default tool
	selectTool(0);
}


function mouseOver(i) { 
	if (i != null && document.images && typeof toolArray[i] != "undefined" && toolArray[i] != "")
	{
		if (typeof toolArray_Disabled[i] != "undefined") 
		{
			// This tool has a 'disabled' image, so it must be constrained to the Zoom Rules
			if (amIZoomedInFarEnough)
			{
				document.getElementById(toolArray[i]).alt = toolArray_Alt[i];
				if (typeof toolArray_On[i] != "undefined") 
				{
					// Yes, we have a valid image for the On position
					document.getElementById(toolArray[i]).src = toolArray_On[i].src; 
				}
				else
				{
					// No, we do not have an image for On.  Use the Off image
					document.getElementById(toolArray[i]).src = toolArray_Off[i].src; 
				}
			}
			else
			{
				document.getElementById(toolArray[i]).alt = "Zoom In closer to use this tool";
				if (typeof toolArray_Disabled[i] != "undefined") 
				{
					// Yes, we have a valid image for the Disabled position
					document.getElementById(toolArray[i]).src = toolArray_Disabled[i].src;
				}
				else
				{
					// No, we do not have an image for Disabled.  Use the Off image
					document.getElementById(toolArray[i]).src = toolArray_Off[i].src; 
				}
			}
		}
		else
		{
			// This tool is not bound by the Zoom Rules, so show the highlight
			document.getElementById(toolArray[i]).alt = toolArray_Alt[i];
			if (typeof toolArray_On[i] != "undefined") 
			{
				// Yes, we have a valid image for the On position
				document.getElementById(toolArray[i]).src = toolArray_On[i].src; 
			}
			else
			{
				// No, we do not have an image for On.  Use the Off image
				document.getElementById(toolArray[i]).src = toolArray_Off[i].src; 
			}
		}
		// Update the status bar with the help text
		statusBar.innerHTML = document.getElementById(toolArray[i]).alt;
	}
}


function mouseOut(i) { 
	if (i != null && document.images && typeof toolArray[i] != "undefined" && toolArray[i] != "")
	{
		// if this is the current tool, leave the hilight turned on by calling the mouseover function
		if (selectedTool != null && selectedTool == i) 
		{
			mouseOver(i);
		}
		else
		{
			if (amIZoomedInFarEnough)
			{
				document.getElementById(toolArray[i]).alt = toolArray_Alt[i];
				if (typeof toolArray_Off[i] != "undefined") 
				{
					// Yes, we have a valid image for the Off position
					document.getElementById(toolArray[i]).src = toolArray_Off[i].src; 
				}
				else
				{
					// No, we do not have an image for Off.  THIS IS AN ERROR, Let the developer know there is a problem with the code
					alert("Let the developer know there is a problem with the OFF_IMAGE for (" + toolArray[i] + ")");
				}
			}
			else
			{
				if (typeof toolArray_Disabled[i] != "undefined") 
				{
					// Yes, we have a valid image for the Disabled position
					document.getElementById(toolArray[i]).src = toolArray_Disabled[i].src; 
				}
				else
				{
					// No, we do not have an image for Disabled.  Use the Off image
					document.getElementById(toolArray[i]).src = toolArray_Off[i].src; 
				}
			}
//			if (selectedTool != null)
//			{
//				statusBar.innerHTML = document.getElementById(toolArray[selectedTool]).alt;
//			}
//			else
//			{
//				statusBar.innerHTML = "Status";
//			}
		}
	}
}


function ToolbarToggleDisable() { 
	// runs through all the toolbar buttons and sets them to Off or Disabled based on zoom level
	for (i=0;i<numTools-1;i++)
	{
		mouseOut(i);
	}
}


function hiliteActiveToolIcon(toolNumber)
{
	// First turn off all the tool icons
	for (i=0;i<numTools-1;i++) 
	{
		mouseOut(i);
	}
	// Now turn on the Active tool
	mouseOver(toolNumber);
	// And set focus on the glass sheet layer
//	glassSheetLayer.focus;
}


function setWaitCursor()
{
	waitCount++;
	window.status = waitCount;
	$('waitCount').innerHTML = (waitCount > 1) ? waitCount + ' responses' : waitCount + ' response';

	glassSheetLayer.style.cursor = "wait";
	
	document.getElementById("WaitDiv").style.visibility = "visible";
}


function setNormalCursor()
{
	waitCount--;
	window.status = waitCount;
	$('waitCount').innerHTML = (waitCount > 1) ? waitCount + ' responses' : waitCount + ' response';
	if (waitCount > 0) return;
	// Reset the map cursor
//	glassSheetLayer.style.cursor = mapCursor;
	setMapCursor();

	document.getElementById("WaitDiv").style.visibility = "hidden";
	waitCount = 0;	// In case we somehow managed to call this function more times then we set the wait function

	// And set focus on the glass sheet layer
	glassSheetLayer.style.zIndex = 1000;
//	glassSheetLayer.focus;
}


function setMapCursor()
{
	try 
	{ 
		glassSheetLayer.style.cursor = mapCursor;
	}
	catch(err) 
	{
		glassSheetLayer.style.cursor = 'default';
	}
	mapCursor = glassSheetLayer.style.cursor;
}


