// Google Maps javascript routines
var geocoder = new GClientGeocoder();
var map = null;
var _console = null;

var markerArray = new Array();
var baseMarker = null;


function load() {
	map = new GMap2(document.getElementById("map"));
	if (GBrowserIsCompatible()) {
		map.setCenter(new GLatLng(40.0000, -95.0000), 4);
	}

	map.addMapType(G_PHYSICAL_MAP);
	map.addMapType(G_HYBRID_MAP);
	map.addMapType(G_NORMAL_MAP);
//	map.addMapType(G_SATELLITE_3D_MAP);
	map.addMapType(G_SATELLITE_MAP);
	var mapTypeControl = new GMapTypeControl();
	map.addControl(mapTypeControl);

	var smallZoomControl = new GSmallZoomControl();
	map.addControl(smallZoomControl);

	map.enableScrollWheelZoom();
	loadRetailers();
}

function fullSize() { /* map.setCenter(new GLatLng(40.0000, -95.0000), 4); */ }

function addAddress(x,y,name,address,contact) {
	// Load the map and put the points on it
	var point = new GLatLng(x,y);
	var marker = new GMarker(point,{clickable: true, title: name});
	markerArray.push(marker);
	GEvent.addListener(marker, "click", function() {
		var tip = "<b>" + name + "</b><br>" + address + "<br>" + contact;
		marker.openInfoWindowHtml(tip);
	});

	GEvent.addListener(marker, "dblclick", function() {
			map.setCenter(point,13);
	});

	map.addOverlay(marker);
}

function debug(msg) {
	if((_console == null) || (_console.closed)) {
		_console = window.open("","console","width=600,height=300,resizable");
		_console.document.open("text/plain");
	}
	
	msg = msg + "<br>\n";

	_console.focus();
	_console.document.writeln(msg);
}

function zoomAddress(address) {
	// Load the map and put the points on it
	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);
				}
			}
	);
}

function setGoogleEarthMap() { map.setMapType(G_SATELLITE_MAP); }
function setGoogleEarth3DMap() { map.setMapType(G_SATELLITE_3D_MAP); }
function setStandardMap() { map.setMapType(G_NORMAL_MAP); }
function setHybridMap() { map.setMapType(G_HYBRID_MAP); }
function setPhysicalMap() { map.setMapType(G_PHYSICAL_MAP); }

function updateGeoCode(index,address) {
	// Find the correct geocode for the address
	geocoder.getLatLng(
			address,
			function(point) {
				if(!point) {
					debug("Address not found: '" + address + "'");
				} else {
					debug("Coords for " + address + " are " + point.toString());

					// Find the form element based on the index
					var gcid = "geocode" + index;
					debug("Looking for " + gcid);

					var whereclause = document.getElementById(gcid);
					// Update the value for the "values" hidden variable: GeoCode="0.0000 0.0000"
					try {
						whereclause.setAttribute("value","GeoCode=" + point.toString());
					} catch (e) {
						debug(e);
					}
					debug(whereclause.getAttribute("name") + "=" + whereclause.getAttribute("value"));

					var fid = "f" + index;
					var f = document.getElementById(fid);

					// Submit the form
					f.submit();
				}
			}
	);
}

function findNearestPoint() {
	var address = document.getElementById('address').value;

	geocoder.getLatLng(
			address,
			function(point) {
				if(!point) {
					alert("Address not found: '" + address + "'");
				} else {
					// alert("Coords for basemarker is " + point.toString());
					baseMarker = new GMarker(point,{draggable:false});
				}
			}
	);

	markerArray.sort(comparePoints);
	map.setCenter(markerArray[0].getPoint(),13);
}

function comparePoints(LatLonA, LatLonB) {
	return baseMarker.getLatLng().distanceFrom(LatLonA.getPoint()) - baseMarker.getLatLng().distanceFrom(LatLonB.getPoint());
} 
