var nextLocationToAddToMap = 0;

google.load("maps", "2", {"other_params":"sensor=false"});

function initialise()
{
	if (GBrowserIsCompatible())
	{
		var map = new google.maps.Map2(document.getElementById("map"));	

		var geocoder = new GClientGeocoder();

		switch ( country )
		{
			case "au":
			{
				geocoder.setBaseCountryCode("au");
				map.setCenter( new GLatLng( -25.274398, 133.775136), 4);
			break;
			}
			case "nz": 
			{
				geocoder.setBaseCountryCode("au");
				map.setCenter( new GLatLng( -41.28648, 174.776217), 5);
				break;
			}
			default: 
				map.setCenter( new GLatLng( 30, 130), 1);
		}
/*
		if (country == "au")
		{
			geocoder.setBaseCountryCode("au");
			map.setCenter( new GLatLng( -25.274398, 133.775136), 4);
		}
		else
		{
			geocoder.setBaseCountryCode("nz");
			map.setCenter( new GLatLng( -41.28648, 174.776217), 5);
		}
*/
		
		map.addControl( new GLargeMapControl3D() );
		
		addLocationsToMap(map,geocoder);
	}
}

google.setOnLoadCallback(initialise);
		  
function centerOnAddress(map, geocoder, address, zoom)
{
	geocoder.getLatLng( address,
		function(point)
		{
			map.setCenter(point, zoom);
		}
	);
}

function addLocationMarker(map, geocoder, location)
{
	if (location)
	{
		location.point = new GLatLng(location.latlng.lat, location.latlng.lng);
		addLocationMarkerByPoint(map, location);
	}
	else
	{
		geocoder.getLatLng( location.address,
			function(point)
			{
				if (point)
				{
					location.point = point;
					addLocationMarkerByPoint(map, location);
				}
			}
		);
	}
}

function addLocationMarkerByPoint(map, location)
{
	if (location)
	{
		var icon = new GIcon(G_DEFAULT_ICON);
		
		icon.image = "/assets/markers/b_pin.png";
		icon.shadow = "/assets/markers/b_shadow.png";
		icon.transparent = "/assets/markers/b_pin_ie.png";

		icon.iconSize = new GSize(34,29);
		icon.shadowSize = new GSize(34,29);
		
		icon.iconAnchor = new GPoint(11,29);
		icon.infoWindowAnchor = new GPoint(11,0);
		
		markerOptions = { icon:icon, title: location.title, clickable:true };
		
		var marker = new GMarker(location.point, markerOptions);
		
		location.marker = marker;
		
		map.addOverlay(marker);
		
		if (location.url)
		{
		
			GEvent.addListener(marker, "click", function()
			{
				window.location = location.url;
			});	
		}
		
		else
		{
			var locationNode = document.getElementById(location.id);	
	
			if (locationNode)
			{
				
				locationHTML = "<div class=\"map-location-info\">" + locationNode.innerHTML.replace( '<p>&nbsp;</p>', '' ) + "</div>";
				
				location.marker.bindInfoWindowHtml(locationHTML, {maxWidth:300});
			}
		}		
	}
}

function addNextLocationToMap(map, geocoder)
{
	if (map && geocoder)
	{
		var nextLocation = locations[nextLocationToAddToMap];
		
		addLocationMarker(map, geocoder, nextLocation);
		
		nextLocationToAddToMap++;
		
		if (nextLocationToAddToMap < locations.length )
		{
			setTimeout ( function() { addNextLocationToMap(map,geocoder) }, 200 );	
		}
	}
}

function addLocationsToMap(map,geocoder)
{
	if (map && geocoder)
	{
		nextLocationToAddToMap = 0;
		
		addNextLocationToMap(map, geocoder);
			
	}
}