var map;
var allmarkers;
var geocoder;

function initialize() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map_canvas"));
		allmarkers = [];
		geocoder = new GClientGeocoder();
		geocoder.setBaseCountryCode("GB");
		// loop thru an array that was built in the onehit.jsp
		for (var i = 0; i < myMappingArray.length; i++) 
		{
			if (null != myMappingArray[i]){
				addAddressAsOverlay(map, myMappingArray[i], myMappingArray2[i]);
			}
		}		
	}else{
	alert("incompatible");
	}
}

function addAddressAsOverlay(map, address, message) {
	if (geocoder) {
		geocoder.getLatLng(
		address,
		function(point) {
			if (!point) {
				//alert(address + " not found");
			} else {

				// create marker, add to map.
				map.setCenter(point, 13);
				var gMarkerObj = new GMarker(point);

				map.addOverlay(gMarkerObj);
				// Display info when this point is clicked
				GEvent.addListener(gMarkerObj,"click", function() {
					map.openInfoWindowHtml(point, message);
				});
				map.addControl(new GSmallMapControl());
				map.addControl(new GMapTypeControl());

				// add marker to array, rescale map
				addPoint(point);
			}
		}
		);
	}
}

// adds a point to the array and rescales
function addPoint(point){
	allmarkers.push(point);
	rescaleMap(map, allmarkers);
}

// rescales the map to the points provided
function rescaleMap( map, points ) {
   var bounds = new GLatLngBounds();
   for (var i=0; i< points.length; i++) {
	  bounds.extend(points[i]);
   }
   map.setZoom(map.getBoundsZoomLevel(bounds));
   map.setCenter(bounds.getCenter());
}