// JavaScript Document for Google Maps API Stuff

google.load("maps", "2.x");

var geocoder;
var map;
var point;


function showAddressOrig(address) {
	geocoder.getLatLng(
	address,
	function(point) {
		if (!point) {
			alert(address + " not found");
		} else {
			map.setCenter(point, 13);
			var marker = new GMarker(point);
			map.addOverlay(marker);
			marker.openInfoWindowHtml(address);
		}
	}
	);
}


// addAddressToMap() is called when the geocoder returns an
// answer.  It adds a marker to the map with an open info window
// showing the nicely formatted version of the address and the country code.
function addAddressToMap(response) {
	clearMarkers();

	if (!response || response.Status.code != 200) {
		//alert("Sorry, we were unable to geocode that address");
		showHideObject('googleMap', 'none');
	} else {
		place = response.Placemark[0];
		point = new GLatLng(place.Point.coordinates[1],	place.Point.coordinates[0]);
		map.setCenter(point, 13);
		marker = new GMarker(point);
		map.addOverlay(marker);
		setObjValue('location_geotag', place.Point.coordinates[1] + "," + place.Point.coordinates[0]);
	}
}


function addMarker(lat, lng) {
		point = new GLatLng(lat, lng);
		map.setCenter(point, 15);
		marker = new GMarker(point);
		map.addOverlay(marker);
}


// showLocation() is called when you click on the Search button
// in the form.  It geocodes the address entered into the form
// and adds a marker to the map at that location.
function showLocation(address) {
	if (address == undefined) { address = document.forms[0].q.value; }
	geocoder.getLocations(address, addAddressToMap);
}


// findLocation() is used to enter the sample addresses into the form.
function findLocation(address) {
	showLocation(address);
}


function clearMarkers() {
  marker=null;
  map.clearOverlays();
}


function reloadMarkers() {
  clearMarkers();
}


function resetMap() {
	clearMarkers();
	centreOnUK();
}


function showAddress(response) {
	if (!response || response.Status.code != 200) {
		alert("Status Code:" + response.Status.code);
	} else {
		place = response.Placemark[0];
		point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
	
		setObjValue('address1', place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.Thoroughfare.ThoroughfareName);
		setObjValue('address3', place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName);
		setObjValue('address4', place.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName);
		setObjValue('postcode', place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber);
	}
}

function centreOnUK() {
	map.setCenter(new GLatLng(54.220284882124005, -4.50439453125), 5);
}

function getBeerMiles() {
	var gDirections = new GDirections();
	var lstrDistance;
	
	gDirections.load("from: SN1 5BP to: BA1 2DP", "en_UK");
	lstrDistance =  gDirections.getDistance();
	alert(lstrDistance);	
}


function zoomToLocation()
{
	var geoTag = document.getElementById('location_geotag').value;
	
	if (geoTag != '' && geoTag != 'undefined') {
		addMarker(geoTag.split(',')[0], geoTag.split(',')[1]);
	}
}


// Call this function when the page has been loaded
function initialize() {
	//markers = [];

	map = new google.maps.Map2(document.getElementById("googleMap"));
	map.addControl(new GSmallMapControl ());
	//map.addControl(new GMapTypeControl());
	//map.addControl(new GScaleControl());
	map.enableScrollWheelZoom();
	
	geocoder = new GClientGeocoder();

	centreOnUK();
}


google.setOnLoadCallback(initialize);

