/**
 * Generic Google Maps handling for Gutenberg Framework
 *
 * @author   Andy West
 *
 **/


/**
 * Global variable for storing a reference to the MapHandler Object
 *
 **/
var MyMap = 'Going to become a maphandler object';


/**
 * Global variable for holding the XML once it's been parsed.  This is
 * necessary as the Google XML parser requires a callback function and
 * therefore we need to wait for the parsed XML to be returned.
 *
 **/
var ParsedXml = 'not parsed yet';


/**
 * Global variable to hold a reference to the current active overlay so
 * that it is easy to remove when another marker is clicked or the
 * overlay is closed.
 *
 **/
var ActiveOverlay = '';


/**
 * Global variable to hold a reference to the Point object for a geocoded address
 *
 **/
var GeoCodedPoint = '';



/**
 * Global marker manager object
 */ 
var Manager = 'Going to become a MarkerManager';



function ReportNearest(NearestMarker)
{
	MyMap.Map.panTo(NearestMarker.Point);
	MyMap.Map.setZoom(12);
	GetMarkerHtml(NearestMarker.Marker, NearestMarker);
}


/**
 * Places a GutenMarker on the map
 *
 **/
function PlaceMarker(GutenMarker, MapHandler)
{
	if ('string' == typeof(Manager))
	{
		Manager = new GMarkerManager(MapHandler.Map);
	}
	var Marker = new GMarker(GutenMarker.Point, GutenMarker.GetIcon(MapHandler.MarkerTypes));

//	MapHandler.Map.addOverlay(Marker);
	GEvent.addListener(Marker, 'click', function() {eval(GutenMarker.GetOverlayFunction()+'(Marker, GutenMarker)');});
	Manager.addMarker(Marker,GutenMarker.GetMinZoom(), GutenMarker.GetMaxZoom());
} // End of PlaceMarker()


/**
 * Creates a custom overlay and places it on the map.  The overlay will be of the
 * class GutenOverlay.
 *
 **/
function GetMarkerHtml(Marker, GutenMarker)
{

	// Create the 3 base divs for the overlay
	
	// First the outer containing div
	AjaxWrapper = document.createElement('div');
	AjaxWrapper.setAttribute('id', 'GutenOverlay');

	// Now a div to show the ajax request status
	AjaxTarget = document.createElement('div');
	AjaxTarget.setAttribute('id', 'googlemarkerdiv');
	
	// And finally a div to hold the final content
	AjaxContent = document.createElement('div');
	AjaxContent.setAttribute('id', 'googlecontentdiv');
	
	// Add the two inner divs to the wrapper
	AjaxWrapper.appendChild(AjaxTarget);
	AjaxWrapper.appendChild(AjaxContent);
	

	// Move the map so that it is centered horizontally on the clicked marker
	// and vertically so that the marker is one third the map height from the
	// bottom of the map.
	var Bounds = MyMap.Map.getBounds();
	var southWest = Bounds.getSouthWest();
	var northEast = Bounds.getNorthEast();
	var FocalLat = GutenMarker.Point.lat() + (((northEast.lat() - southWest.lat()) / 4));
	MyMap.Map.panTo(new GLatLng(FocalLat, GutenMarker.Point.lng()));

	// Close the current open info window, if there is one.
	CloseActiveOverlay();
	
	// Add the new info window to the map
	ActiveOverlay = new GutenOverlay(0, 0, -15, 200, GutenMarker.Point, AjaxWrapper);
	
	MyMap.Map.addOverlay(ActiveOverlay);


	// Make a new Ajax request to get the right content for the info window.
	MarkerAjax = new AjaxObject();
	MarkerAjax.SetAjaxDest('googlemarkerdiv', 'googlecontentdiv');
	MarkerAjax.SetResetFlag(false);
	MarkerAjax.ChangeAjaxBase(GutenMarker.GetAjaxUrl());
	MarkerAjax.sndReq();

	return true;

} // End of GetMarkerHtml()


/**
 * The callback function used to parse the downloaded XML file.  Sets the global
 * ParsedXml to the Xml Dom object
 *
 **/
XmlCallback = function(Data, Response)
{
	ParsedXml = GXml.parse(Data);
}


/**
 * This is the inital function that should be called in the onload event of the
 * page.  It attempts to load the XML file specified and pass it to the XmlCallBack
 * function, which parses it and sets the parsed object to the ParsedXml global.
 * It then calls the CheckForParsationOfXml function which waits for the XML to be
 * parsed.
 *
 **/
function LoadGoogleMap(XmlFile)
{
	GDownloadUrl(XmlFile, XmlCallback);
	CheckForParsationOfXml();
} // End of LoadGoogleMap()


/**
 * Implements a timeout that calls the function repeatedly until the XML file has
 * been parsed and the global variable is set.  Once the XML is parsed the MyMap
 * global will be set to a new instance of the MapHandler object and the MakeMap
 * method will be called to construct the map from the parsed XML.
 *
 **/
function CheckForParsationOfXml()
{
	if (typeof(ParsedXml) == 'string')
	{
		setTimeout('CheckForParsationOfXml()', 100);
	}
	else
	{
	   MyMap = new MapHandler();
		MyMap.MakeMap();
		if(typeof SearchAddress != 'undefined')
		{
		   MyMap.FindNearest(SearchAddress);
		}
	}
} // End of CheckForParsationOfXml()


/**
 * Closes the active info window, if one exists and resets the pointer to this
 * window.
 *
 **/
function CloseActiveOverlay()
{
	if(typeof(ActiveOverlay) != 'string')
	{
		ActiveOverlay.remove();
		ActiveOverlay = 'once more, not an object';
	}
}


