// Initialize variables.
var map			= null;
var geocoder	= null;
var zoomLevel	= null;

// Define customized "Sonny Hunt" marker icon.
var icon				= new GIcon();
icon.image				= "/styles/1.0.0/build/SilkIcons/cross.png";
//icon.image				= "/styles/images/mm_20_tcn.png";
//icon.shadow				= "/styles/images/mm_20_shadow.png";
icon.iconSize			= new GSize(16, 16);
//icon.shadowSize			= new GSize(22, 20);
icon.iconAnchor			= new GPoint(6, 20);
icon.infoWindowAnchor	= new GPoint(5, 1);

function getMeThere(formObject)
{
	var googlemapsurl = formObject.url1.value;
	googlemapsurl    += (formObject.zipcode.value).replace(' ','+');
	googlemapsurl    += (formObject.url2.value).replace(' ','+');
	top.location = googlemapsurl;
}

// Show the marker on the map when either the address/zipcode/city or the zipcode/city combination has been found.
function showMarker(point, dAddr)
{
	map.setCenter(point, zoomLevel);
	var marker = new GMarker(point, icon);
	map.addOverlay(marker);
	var info  = '<b>'+venue+'</b><br />';
	info += venueAddress+' '+venueHousenumber+'<br />';
	if (venueCountryID == 227)
	{
		info += venueCity+' '+venueZipcode+'<br /><br />';
	}
	else
	{
		info += venueZipcode+' '+venueCity+'<br /><br />';
	}
	if (venueTelephone != '')
	{
		info += '<img class="icon" src="'+stylesDir+'SilkIcons/telephone.png" width="16" height="16" alt="telephone" align="absmiddle" /> '+venueTelephone+'<br />';
	}
	if (venueFax != '')
	{
		info += '<img class="icon" src="'+stylesDir+'SilkIcons/printer.png" width="16" height="16" alt="fax" align="absmiddle" /> '+venueFax+'<br />';
	}
	if (venueEmail != '')
	{
		info += '<img class="icon" src="'+stylesDir+'SilkIcons/email.png" width="16" height="16" alt="e-mail" align="absmiddle" /> <a href="mailto:'+venueEmail+'">'+venueEmail+'</a><br /><br />';
	}
	info += '<form onsubmit="getMeThere(this);return false;" action="" method="post">';
	info += '<input type="hidden" name="url1" value="http://maps.google.com/maps?f=d&saddr=" />';
	info += ' <label for="zipcode">My zipcode:</label> <input type="text" id="zipcode" name="zipcode" size="5" maxlength="7" value="" />';
	info += ' <input type="hidden" name="url2" value=",'+venueCountry+'&daddr='+dAddr+'&ie=UTF8&om=1" />';
	info += ' <input type="submit" value="Get directions" />';
	info += '</form>';
	marker.openInfoWindowHtml(info);
	GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml(info);});
}

// Action to perform when the selected zipcode and city has not been found... search for zipcode alone.
function showZipcode()
{
	if (geocoder)
	{
		address = venueZipcode+venueCountry;
		geocoder.getLatLng(	address,	
							function(point)
							{
								if (!point)
								{
									alert(address + " not found");
								}
								else
								{
									showMarker(point, address);
								}
							}
						  );
	}
}

// Action to perform when the selected address has not been found... search for zipcode and city.
function showCity()
{
	if (geocoder)
	{
		address = venueZipcode+','+venueCity+','+venueCountry;
		geocoder.getLatLng(	address,	
							function(point)
							{
								if (!point)
								{
									showZipcode();
								}
								else
								{
									showMarker(point, address);
								}
							}
						  );
	}
}

// Action to perform when the user selects a venue.
function showAddress()
{
	map.clearOverlays();
	
	if (geocoder)
	{
		address = venueAddress+','+venueZipcode+','+venueCity+','+venueCountry;
		geocoder.getLatLng(	address,	
							function(point)
							{
								if (!point)
								{
									showCity();
								}
								else
								{
									showMarker(point, address);
								}
							}
						  );
	}
}

// Actions to perform when the web page is loaded.
function load(mapType, initialZoomLevel)
{
	if (GBrowserIsCompatible())
	{
		zoomLevel = initialZoomLevel;
		map = new GMap2(document.getElementById("map"));
		map.setCenter(new GLatLng(36.7,-86.41), zoomLevel);
		map.setMapType(mapType);
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		geocoder = new GClientGeocoder();
		showAddress();
	}
}
