// Global Variables
var oMap;
var oGeocoder = null;
var gDir = null;
// sTo is the default value of directions option. Jubilee Park
var sTo = '-33.876124,151.176430';

var nMapLat = -33.888005;
var nMapLng = 151.179095;
//Creates GoogleMap on page.
// - nMapLat:					Is the lattitude the map is centered to.
// - nMapLng:					Is the longitude the map is centered to.

function createGMap(nMapLat, nMapLng)
{
	//If the GoogleMp objects are supported make a map.
	if (GBrowserIsCompatible())
	{
		// change the size of the map_canvas div
		document.getElementById('map_canvas').style.height = '500px';
		document.getElementById('map_canvas').style.width = '100%';
		// hide div that contains activation button
		//document.getElementById('makemapgonow').innerHTML = '';
		//Make a new map in the map element.
		oMap = new GMap2(document.getElementById("map_canvas"));

		//Assign center of map to a co-ordinates supplied.
		var oMapCenter = new GLatLng(nMapLat, nMapLng);
		oMap.setCenter(new GLatLng(nMapLat, nMapLng), 12);
		
		//Add control bar to the map.
		oMap.addControl(new GSmallMapControl());
		oMap.addControl(new GMapTypeControl());
		oMap.addControl(new GOverviewMapControl());

		//Add geocoding object.
		oGeocoder = new GClientGeocoder();

		//Get markers from database and add them. ok, no database here but.....for loops? arrays?
		addLocataionPinMarkers(oMap,-33.876124,151.176430,'Nelson St Glebe','Glebe Greyhounds','Jubilee Park');
		addLocataionPinMarkers(oMap,-33.865587,151.103624,'Broughton St Concord','The Concord Cats','Goddard Park');
		addLocataionPinMarkers(oMap,-33.880500,151.232460,'Glenmore Rd Wollahra','The Eastern Bulldogs','Trumper Oval');
		addLocataionPinMarkers(oMap,-33.907641,151.187101,'Sydney Park Rd, Alexandria','Newtown Swans','Alan Davidson Oval');
		addLocataionPinMarkers(oMap,-33.947224,151.232040,'Fitzgerald Ave, Maroubra','Maroubra Saints','Heffron Park');
		addLocataionPinMarkers(oMap,-33.900909,151.107836,'Brighton Ave, Croydon Park','Wests Magpies','Picken Oval');
		addLocataionPinMarkers(oMap,-33.849768,151.150239,'Bayswater Rd, Drummoyne','Drummoyne Power','Drummoyne Oval');
	}
}

//Creates a pin map marker on oGMap at the co-ordinates supplied.
// - oCustomPin:				Custom pin object
// - oGMap:						GoogleMap object pin marker is being attached
// - nLattitude:				Lattitude pin is located
// - nLongitude:				Longitude pin is located
// * Return:					Marker objected attached to oGMap
function createGMapPinMarker(oCustomPin, oGMap, nLattitude, nLongitude)
{
	//Setup pin object
	var oGMapPin = new GIcon();
	oGMapPin.image = oCustomPin.imagePath;
	oGMapPin.shadow = oCustomPin.shadowPath;
	oGMapPin.iconSize = new GSize(oCustomPin.iconHeight, oCustomPin.iconWidth);
	oGMapPin.shadowSize = new GSize(oCustomPin.shadowHeight, oCustomPin.shadowWidth);
	oGMapPin.iconAnchor = new GPoint(oCustomPin.anchorHeight, oCustomPin.anchorWidth);
	oGMapPin.infoWindowAnchor = new GPoint(oCustomPin.infoWidth, oCustomPin.infoHeight);

	//Turn pin object into a literal.
	oMarker = {icon:oGMapPin, draggable: true};
	
	//Create marker from pin literal.
	var oPoint = new GLatLng(nLattitude, nLongitude);
	var oGMapMarker = new GMarker(oPoint, oMarker);
	
	// add event listener to return co-ords of dragged pin marker
	GEvent.addListener(oGMapMarker, "dragend", function()
	{
		var oMarkerPoint = oGMapMarker.getLatLng();
		oGMapMarker.openInfoWindowHtml('The Co-ords are: ' + oMarkerPoint);
	});

	//Add marker to map.
	oGMap.addOverlay(oGMapMarker);
	
	return oGMapMarker;
}

//Takes all locations in database and adds map markers to oGMap
// - oGMap:						GoogleMap object pin marker is being attached
// * Return:					VOID
function addLocataionPinMarkers(oGMap, nPinlattitude, nPinLongitude, sAddress, sName, sTeam)
{

	//Loop through each database location entry and add a pin.
	//for (var i = 0; var y = aLocations.length; i < y; i++)
	//{
		//Create a new custom pin.
		var oCustomPin = new customGMapPin();

		//Get marker info from db.
		//var nPinlattitude = -33.888005;
		//var nPinLongitude = 151.179095;
		//var sAddress = 'Briggs Street Camperdown';
		//var sName = 'Dantopia';
		//var sTeam = 'Dan\'s Team';

		//Set tabs for marker.
		var oInfoTabs = [new GInfoWindowTab('NAME', '<div style="width: 300px;">' + sTeam + '</div>'),
						new GInfoWindowTab('ADDRESS', '<div style="width: 300px;">' + sAddress + '</div>'),
						new GInfoWindowTab('TEAM', '<div style="width: 300px;">' + sName + '</div>')];

		//Make the marker object.
		var oMapMarker = createGMapPinMarker(oCustomPin, oGMap, nPinlattitude, nPinLongitude);

		//Add mouse click event listener to marker.
		GEvent.addListener(oMapMarker, "click", function()
		{
			oMapMarker.openInfoWindowTabsHtml(oInfoTabs);
		});
	//}
}

//Custom pin object, no methods, data holder only.
function customGMapPin()
{
	this.imagePath = "http://www.dansdom.com/images/redpin.png";
	this.shadowPath = "http://www.dansdom.com/images/pinshadow.png";
	this.iconHeight = 60;
	this.iconWidth = 45;
	this.shadowHeight = 60;
	this.shadowWidth = 45;
	this.anchorHeight = 28;
	this.anchorWidth = 43;
	this.infoHeight = 8;
	this.infoWidth = 30;
}

// Show Geocoder address value
// - sAddress					value of the text inside the form element: user input
function showAddress(sAddress)
{
	if (oGeocoder)
	{
		oGeocoder.getLatLng(sAddress, function(point)
			{
            if (!point) 
				{
              	alert(sAddress + " Oh silly - you forgot to enter an address. Try again.");
            	} 
			else
				{
              	oMap.setCenter(point, 15);
              	var oCustomPin = new customGMapPin();
              	var oMapMarker = createGMapPinMarker(oCustomPin, oMap, point.lat(), point.lng());
              	oMapMarker.openInfoWindowHtml(sAddress);
              	GEvent.addListener(oMapMarker, "click", function()
				{
					oMapMarker.openInfoWindowHtml(sAddress);
				});
				// take co-ords of address and convert it into a string for directions request
              	dLat = point.lat();
              	dLng = point.lng();
              	sFrom = dLat + ', ' + dLng;
              	// returns address with latitude and longitude values to div id="results"
                document.getElementById('result').innerHTML = '<br /><em>The address you have entered is:</em> ' + sAddress + '.<br /><em>Which is at these co-ordinates:</em> latitude = ' + point.lat() + ', longitude = ' + point.lng() + '.';
                // empty "directions" container first
                document.getElementById("directions").innerHTML = '';
				loadDirections(sFrom, sTo);
            	}
          	});
    }
}

// Loads directions panel in div id="directions"
// nFrom						Geoquery reult served as a combination of point.lat() and point.lng()
// sTo							set by event handler on the select options
function loadDirections(nFrom,nTo)
{
		// if there is already directions data on the map then clear it
		if (gDir)
		{
			gDir.clear();
		}
		 // div that the directions panel is going into
         oPanel = document.getElementById("directions");
         gDir = new GDirections(oMap, oPanel);
         gDir.load("from: " + nFrom + " to: " + nTo);
}


