var map = null;

var centered = false;

// have an array of all the locations we want to join up.
var places = new Array();
var index = 0;

function loadGMap() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.enableScrollWheelZoom();
				
		var geocoder = new GClientGeocoder();
		
		
		for(i=0;i<places.length;i++) {
			if (places[i].status == "Success") {
				places[i].latlng = new GLatLng(places[i].lat, places[i].lng)
				var marker = new GMarker(places[i].latlng);
				places[i].marker = marker;
				map.setCenter(places[i].latlng, 12);
				map.addOverlay(marker);
				places[i].description ="<h5>"+places[i].name+"</h5>\n"+places[i].address;
				marker.bindInfoWindowHtml(places[i].description);
			}
		}
		autoCenter();
	}
}

function autoCenter() {
	var minLongitude = 0;
	var maxLongitude = 0;
	var minLatitude = 0;
	var maxLatitude = 0;
	var zoom;
	
	for (var i = 0; i < places.length; i++) {
		if (places[i].latlng) {
			if (!minLongitude) minLongitude = places[i].latlng.lng();
			if (!maxLongitude) maxLongitude = places[i].latlng.lng();
			if (!minLatitude) minLatitude = places[i].latlng.lat();
			if (!maxLatitude) maxLatitude = places[i].latlng.lat();

			minLongitude = Math.min(minLongitude, places[i].latlng.lng());
			maxLongitude = Math.max(maxLongitude, places[i].latlng.lng());
			minLatitude = Math.min(minLatitude, places[i].latlng.lat());
			maxLatitude = Math.max(maxLatitude, places[i].latlng.lat());
		}
	}

	var centerLatitude = (minLatitude + (maxLatitude - minLatitude) / 2 );
	var centerLongitude = (minLongitude + (maxLongitude - minLongitude) / 2);

	var miles = (3958.75 * Math.acos(Math.sin(minLatitude / 57.2958) * Math.sin(maxLatitude / 57.2958) + Math.cos(minLatitude / 57.2958) * Math.cos(maxLatitude / 57.2958) * Math.cos(maxLongitude / 57.2958 - minLongitude / 57.2958)));
	var kms = miles * 1.609344;

	if (!kms || kms < 0.5) {
		zoom = 15;
	} else if (kms < 1.5) {
		zoom = 14;
	} else if (kms < 3) {
		zoom = 13;
	} else if (kms < 5) {
		zoom = 12;
	} else if (kms < 20) {
		zoom = 11;
	} else if (kms < 35) {
		zoom = 10;
	} else if (kms < 70) {
		zoom = 9;
	} else if (kms < 110) {
		zoom = 8;
	} else if (kms < 150) {
		zoom = 7;
	} else if (kms < 400) {
		zoom = 6;
	} else if (kms < 2000) {
		zoom = 5;
	} else if (kms < 3500) {
		zoom = 4;
	} else if (kms < 6000) {
		zoom = 3;
	} else {
		zoom = 2;
	}
	/*
	if (!centered) {
		document.getElementById("map_center").innerHTML = 
			document.getElementById("map_center").innerHTML +
			"<br /><br />Longitude: " + centerLongitude +
			"<br />Min Longitude: " + minLongitude +
			"<br />Max Longitude: " + maxLongitude +
			"<br /><br />Latitude: " + centerLatitude +
			"<br />Min Latitude: " + minLatitude +
			"<br />Max Latitude: " + maxLatitude +
			"<br /><br />Max distance in kms: "+ kms;
		centered = true;
	}
	*/
	
	map.setZoom(zoom);
	if (places.length > 0) {
		map.panTo(new GLatLng(centerLatitude, centerLongitude));
	}
	return;
}

// move to the next marker
function moveTo(index)
{
	if (map != null)
	{
		if (places[index].latlng) {
			map.panTo(places[index].latlng);
			map.setZoom(14);
			places[index].marker.openInfoWindowHtml(places[index].description);
		}
	}
}
		
//----- Stop page scrolling if wheel over map ----
function wheelevent(e)
{
	if (!e) e = window.event;
	if (e.preventDefault) e.preventDefault();
	e.returnValue = false;
}
