/************************************************************************
** mapping.js
**
** Description:
** mapping.js stores all of the common routines and vars necessary
** to perform Google mapping.  Include this script in any page
** that requires Google mapping.
**
** Note:  It is assumed that you have registered your URL for a key
**		  to use Google mapping.  See http://www.google.com/apis/maps/
**		  for more information
**
** Prior to including this file, you should issue a line similar to the
** following in your page:
** 	<script src="http://maps.google.com/maps?file=api&v=1&key=ABQIAAAAbzEDVQfFk5sIOvGK-Y7OuBTqUM-tBxgpJWnk6YvqKRS_ByyapxQSgZnieK65DiDvpMfZlgo3GFBVOQ" type="text/javascript"></script>
** needless to say, the key=<value> should be your registered key
**
** NOTE:
**	Several functions take either an arLocs or arMarkers array.  These
**	are NOT defined in this file and should be defined and initialized
**  externally.  They are of the form:
**
**  arLocs = [
**				[lattitude (double), longitude (double), name of location (string), address of location (string), website of location],
**				[lat2, long2, loc2, add2, web]
**				...
**			 ]
**
**  arMarkers = [marker object (GMarker)],
**				 marker2,
**				 ...
**				]
**
**  There should be one marker for each location.  It is recommended that
**  you use CreateMarker to initialize the marker array, as follows:
**		var arMarkers = [CreateMarker(arLocs, 0),
**						 CreateMarker(arLocs, n)];
**
*************************************************************************/

var _map;									// _map object
var _bMapCreated = false;						// true after _map is created
var _elementMapLayer=null;					// the layer that contains the _map object
var _elementLoadingLayer=null;				// the layer that displays the loading message
var _defaultGPoint;							// the initial point on the _map to show
var _iDefaultZoomLevel;						// the default zoom level to show
var _arLocs;								// the array of locations (see above)
var _arMarkers;								// the array of markers (see above)
var _timer;									// timer for doing the load

/* Initialize the _map variables
** This needs to be called before any other _map function.  It sets
** up the variables that the _map will use, but does not instantiate
** the _map itself.  Subsequent calls to InitializeMap (via something
** like PanMap) will use the variables set up here
** sContainerId = Id of the container -- usually a div -- for the _map
** sLoadingId = Id of the loading layer -- usually a div -- for the _map (can be "" if no loading div)
** arLocs = the array of locations (see above)
** dInitialLat, dInitialLng = initial latitude and longitude used when showing _map
** iInitialZoomLevel = default zoom level when showing _map
*/
function InitMapVars(sContainerId, sLoadingId, arLocs, dInitialLat, dInitialLng, iInitialZoomLevel)
{
	_map = null;			// clear this out if it was previously set
	_bMapCreated = false;	// reset the map
	_elementMapLayer = document.getElementById(sContainerId);
	if (sLoadingId != "") _elementLoadingLayer = document.getElementById(sLoadingId);
	_arLocs = arLocs;
	_defaultGPoint = new GPoint(dInitialLat, dInitialLng);
	_iDefaultZoomLevel = iInitialZoomLevel;
}

// Initialize the _map.  
function InitializeMap(iIndex)
{
	// If a loading layer is present, show it using a timer
	if (_elementLoadingLayer != null)
		_timer = setTimeout("showLoadingLayer(" + iIndex + ")", 1);
	else
		loadMap(iIndex);		// No loading layer.  Just show the map.
	
	return false;
}

function showLoadingLayer(iIndex)
{
	_elementLoadingLayer.style.display = 'block';
	_elementLoadingLayer.innerHTML = 'loading map...';
	setTimeout("loadMap(" + iIndex + ")", 1);			// Use a timer to load the map to ensure that the loading div is shown
	return false;
}

// Show the map
function loadMap(iIndex)
{
	// Check for browser compatibility
 	if (GBrowserIsCompatible())
	{
		// Create the _map itself
		_elementMapLayer.style.display = 'block';		
		_map = new GMap(_elementMapLayer);				
		//_elementMapLayer.style.display = 'none';		
	
		// Add the controls to the _map
		_map.addControl(new GSmallMapControl());
		_map.addControl(new GMapTypeControl());
		// Do the initial zoom
		try
		{
			_map.centerAndZoom(_defaultGPoint,_iDefaultZoomLevel);
			
			createMarkers();
			
			if (_elementLoadingLayer != null)
			{
				_elementLoadingLayer.style.display = 'none';
			}
		
			_elementMapLayer.style.display = 'block';		
		}
		catch(x)
		{
			if (_elementMapLayer != null)
				_elementMapLayer.innerHTML = "Error creating map:  " + x.message;
		}
	
		_bMapCreated = true;
		
		setTimeout("CenterAndShowMarker(" + iIndex + ")", 1);
	}
	else
	{
		// This browser is not compaible with google maps.  Tell the user and link 
		// out to google maps compatibility
		sHtml = "<div style='margin: 10px'>" + 
		        "We're sorry.  The browser version that you are using is not supported " +
			    "by Google Maps.<br><br>For a list of supported browsers, please see " +
			    "<a class='external' href='http://local.google.com/support/bin/answer.py?answer=16532&topic=1499' target='_blank'> " +
			    "Googal Local Help Center</a>." + 
			    "</div>";
		if (_elementLoadingLayer!=null)
			_elementLoadingLayer.style.display = 'none';
		_elementMapLayer.style.display = 'block';		
		_elementMapLayer.innerHTML = sHtml;
		_bMapCreated = false;
	}
		
	return false;

	/*******************************************
	** FUNCTIONS LOCAL TO InitializeMap       **
	*******************************************/
	// Using _arLocs, create the _arMarkers array and add them to the _map layer
	function createMarkers()
	{
		var iLocLen = _arLocs.length;
		_arMarkers = new Array(iLocLen);
		
		var i;
		for (i=0; i < iLocLen; i++)
			_arMarkers[i] = createMarker(i);
	}

	// Create a marker at the location pointed to by iIndex
	function createMarker(iIndex)
	{
		try
		{
			var dLat = _arLocs[iIndex][0];
			var dLong = _arLocs[iIndex][1];
			var sLocationName = _arLocs[iIndex][2];
			var sLocationAddress = _arLocs[iIndex][3];
			var sWebAddress = _arLocs[iIndex][4];
			var sHtml = formatInfoHtml(sLocationName, sLocationAddress, sWebAddress);
			var point = new GPoint(dLat, dLong);
			var marker = new GMarker(point);
			GEvent.addListener(marker, 'click', function() {marker.openInfoWindowHtml(sHtml);});
			_map.addOverlay(marker);
			return marker;
		}
		catch(x)
		{
			return null;
		}
	}
}

// Format the text for an info box given the location name and address
function formatInfoHtml(sLocationName, sLocationAddress, sWebAddress)
{
	var sHtml = "<div style=\"width: 300px; font-size: small;\">"
	var sLoc;
	if (sWebAddress == "")
		sLoc = "<b><div style=\"font-size: larger;\">" + sLocationName + "</div></b>";
	else
		sLoc = "<a href='http://" + sWebAddress + "' class='external' target='_blank'>" + sLocationName + "</a>";
	sHtml += sLoc + "<br/>"
	sHtml += sLocationAddress + "</div>"
	return sHtml;
}

// Pan the _map to the location pointed to by iIndex
function PanMap(iIndex)
{
	if (!_bMapCreated)
		InitializeMap(iIndex);
	else
		CenterAndShowMarker(iIndex);
	return false;
}

function CenterAndShowMarker(iIndex)
{	
	_map.recenterOrPanToLatLng(new GPoint(_arLocs[iIndex][0], _arLocs[iIndex][1]));
	ShowMarkerInfo(iIndex);
	return false;
}

// Open up an info window above a given marker
// Uses the information in the arLocs to format the information string
function ShowMarkerInfo(iIndex)
{
	_arMarkers[iIndex].openInfoWindowHtml(formatInfoHtml(_arLocs[iIndex][2], _arLocs[iIndex][3], _arLocs[iIndex][4]));
}

