﻿var map;
var center = new GLatLng(53.3, -0.65);
var gdir;
var localSearch;
var mapZoomLevel = 7;
var gmap_jgIcon = new GIcon();
var gmap_hqIcon = new GIcon();

function gmap_load() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.setCenter(center, mapZoomLevel);
		var opts2 = {
			zoomInBtnTitle: "Zoom In",
			zoomOutBtnTitle: "Zoom Out",
			moveNorthBtnTitle: "Move North",
			moveSouthBtnTitle: "Move South",
			moveEastBtnTitle: "Move East",
			moveWestBtnTitle: "Move West",
			homeBtnTitle: "Back to Home"
		};
		var extLargeMapControl = new ExtLargeMapControl(opts2);
		map.addControl(extLargeMapControl);

		localSearch = new GlocalSearch();
		var directionsPanel = document.getElementById("route");
		gdir = new GDirections(map, directionsPanel);
		GEvent.addListener(gdir, "error", gmap_handleErrors);
	}
}

function gmap_loadIcons(path) {
	gmap_jgIcon.image = path + "gmap-jg-marker.png";
	gmap_jgIcon.iconSize = new GSize(38, 61);
	gmap_jgIcon.shadow = path + "gmap-jg-marker-shadow.png";
	gmap_jgIcon.shadowSize = new GSize(55, 61);
	gmap_jgIcon.iconAnchor = new GPoint(1, 60);
	gmap_jgIcon.infoWindowAnchor = new GPoint(1, 60);
	gmap_jgIcon.transparent = path + "gmap-jg-marker-transparent.png";
	gmap_jgIcon.printImage = path + "gmap-jg-marker-print.gif";
	gmap_jgIcon.mozPrintImage = path + "gmap-jg-marker-print-ff.gif";
	gmap_jgIcon.printShadow = path + "gmap-jg-marker-print-shadow.gif";
	gmap_jgIcon.imageMap = [17, 0, 10, 2, 7, 4, 4, 7, 2, 11, 1, 17, 1, 22, 3, 26, 5, 30, 7, 33, 1, 60, 20, 37, 25, 36, 31, 33, 35, 27, 37, 23, 37, 20, 37, 15, 37, 10, 35, 7, 32, 4, 28, 2, 22, 0];

	gmap_hqIcon.image = path + "gmap-hq-marker.png";
	gmap_hqIcon.iconSize = new GSize(73, 42);
	gmap_hqIcon.shadow = path + "gmap-hq-marker-shadow.png";
	gmap_hqIcon.shadowSize = new GSize(73, 42);
	gmap_hqIcon.iconAnchor = new GPoint(0, 29);
	gmap_hqIcon.infoWindowAnchor = new GPoint(0, 29);
	gmap_hqIcon.transparent = path + "gmap-hq-marker-transparent.png";
	gmap_hqIcon.printImage = path + "gmap-hq-marker-print.gif";
	gmap_hqIcon.mozPrintImage = path + "gmap-hq-marker-print-ff.gif";
	gmap_hqIcon.printShadow = path + "gmap-hq-marker-print-shadow.gif";
	gmap_hqIcon.imageMap = [42, 0, 37, 1, 34, 3, 30, 6, 28, 9, 26, 15, 26, 16, 19, 20, 0, 30, 29, 30, 33, 34, 37, 36, 41, 37, 48, 37, 52, 36, 55, 34, 59, 31, 61, 28, 62, 24, 63, 20, 63, 16, 62, 12, 61, 9, 58, 5, 55, 3, 52, 1, 47, 0];
}

function gmap_addMarker(lat, lng, name, address, id, url, headoffice) {
	var point = new GLatLng(lat, lng);
	var marker = new GMarker(point, headoffice ? gmap_hqIcon : gmap_jgIcon);
	var popupHtml = "<div style=\"text-align: left;\"><div><strong>" + name + "</strong></div><div>" + address + "</div>";
	
	if (!headoffice)
	{
		popupHtml += "<p><a href=\"" + url + "\">View dogs at this rescue centre</a></p>";
	}
	
	popupHtml += "<div><strong>Directions from:</strong></div><div><input type=\"text\" id=\"DirectionsTo" + id + "\" /><button onclick=\"return gmap_directMe('" + id + "', '" + name + "', " + lat + ", " + lng + ");\">Go</button></div>";

	map.addOverlay(marker);
	GEvent.addListener(marker, "click", function() {
		map.openInfoWindowHtml(point, popupHtml);
	});
}

function gmap_directMe(id, destName, lat, lng) {
	var from = $("#DirectionsTo" + id).val();
	var point = new GLatLng(lat, lng);

	if (from == "") {
		alert("Please enter an address.");
		return false;
	}

	localSearch.setSearchCompleteCallback(null, function() {
		if (localSearch.results[0]) {
			var resultLat = localSearch.results[0].lat;
			var resultLng = localSearch.results[0].lng;
			var addressString = "from: " + resultLat + ", " + resultLng + " to: " + destName + "@" + point.toUrlValue(11);
			gdir.load(addressString, { locale: "en_GB" });
		} else {
			alert("Postcode not found!");
		}
	});

	localSearch.execute(from + ", UK");
	return false;
}

function gmap_handleErrors() {
	alert("Sorry, we couldn't find the address you entered.");
}
