/**
 * Re-writing Google Maps script for GMap API v3
 */

function initialiseGMap()
{
	// Set starting point 
	var start = new google.maps.LatLng(50.383029, -4.133327);
	
	// Initial map options
	var gmapOptions = {
		zoom: 16,
		center: start,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	
	// Create the map
	var gMap = new google.maps.Map(document.getElementById("map"), gmapOptions);
	
	addMarkers(gMap);
}


function addMarkers(gMap)
{
	/*
	 * MARKERS
	 */
	// Defne custom markers
	var icoGreen = "http://www.google.com/intl/en_us/mapfiles/ms/micons/green-dot.png";
	var icoBlue = "http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png";
	
	// WA venue first
	var mrkVenue = putMarker(gMap, 50.384187, -4.134586, "The Hyde Park, Mutley Plain");
	addInfoWindow(gMap, mrkVenue, "<p><b>The Hyde Park</b><br />Mutley Plain, Plymouth</p>");
	
	// Mutley Plain bus stop
	var mrkBusMutley = putMarker(gMap, 50.382261, -4.134280, "Mutley Plain bus stop", icoGreen);
	addInfoWindow(gMap, mrkBusMutley, "<p><b>Mutley Plain</b><br /><b class=\"red\">Citybus:</b> 28/A/B, 31, 35, 40, 41, 42, 44/A, 46, 47, 59, 61, 62<br /><b class=\"blue\">First:</b> 15, 81C,  84, 86<br /><b class=\"green\">Target:</b> 39/A</p>");
	
	// Alexandra Road bus stop
	var mrkBusAlex = putMarker(gMap, 50.380927, -4.132018, "Alexandra Road bus stop", icoGreen);
	addInfoWindow(gMap, mrkBusAlex, "<p><b>Alexandra Road Top</b><br /><b class=\"red\">Citybus:</b> 8, 9, 22<br /><b class=\"green\">Target:</b>10, 19</p>");

	// Greenbank Road bus stop
	var mrkBusGreen = putMarker(gMap, 50.378169, -4.130838, "Greenbank Road bus stop", icoGreen);
	
	// Ermington Terrace car park
	var mrkParkErm = putMarker(gMap, 50.381091, -4.134306, "Ermington Terrace car park", icoBlue);
	
	// Ford Park Lane car park
	var mrkParkFord = putMarker(gMap, 50.384201, -4.135394, "Ford Park Lane car park", icoBlue);
}

/**
 * Put a marker on the map
 * @param gMap - Reference to the Map on which the marker will be placed
 * @param lat - The latitude of the marker
 * @param lng - The longitude of the marker
 * @param gTitle - The Tooltip title of the marker
 * @returns {google.maps.Marker} - The compiled Marker object
 */
function putMarker(gMap, lat, lng, gTitle, iconUrl)
{
	var gLatLng = new google.maps.LatLng(lat, lng);
	
	if(arguments.length == 5)
	{
		// Custom icon
		var icon = new google.maps.MarkerImage({
			url: iconUrl
		});
		
		// Default icon
		var mrk = new google.maps.Marker({
			map: gMap,
			position: gLatLng,
			title: gTitle,
			icon: iconUrl
		});
	}
	else
	{
		// Default icon
		var mrk = new google.maps.Marker({
			map: gMap,
			position: gLatLng,
			title: gTitle
		});
	}
	
	return mrk;
}

/**
 * Add a po-up bubble when a marker is clicked
 * @param map
 * @param marker
 * @param message
 */
function addInfoWindow(map, marker, message)
{
	var info = new google.maps.InfoWindow({
		content: message,
		size: new google.maps.Size(50,50)
	});
	
	google.maps.event.addListener(marker, 'click', function(){
		info.open(map, marker);
	});
}
