var map;
var maptype;
var points;
var bounds;

// Load Google Maps API
function load(type)
{
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GSmallMapControl());
		icon = new GIcon();
		icon.image = "http://labs.google.com/ridefinder/images/mm_20_white.png";
		icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
		icon.iconSize = new GSize(12, 20);
		icon.shadowSize = new GSize(22, 20);
		icon.iconAnchor = new GPoint(6, 20);
		icon.infoWindowAnchor = new GPoint(5, 1);
		maptype = type;
		points = new Object();
		bounds = new GLatLngBounds();
		map.getContainer().style.overflow="hidden"; 
	}
}

// THESE FUNCTIONS AREA USED ONLY IN LIST VIEW
function showEntriesOnMap(year, month, day, zoom)
{
	GDownloadUrl("xml.php?type=googlemaps&year="+year+"&month="+month+"&day="+day, function(data, responseCode) {
		var xml = GXml.parse(data);
		var markers = xml.documentElement.getElementsByTagName("marker");
		// Add markers to array but DON'T display them yet
		for (var i = 0; i < markers.length; i++) {
			var latitude = parseFloat(markers[i].getAttribute("latitude"));
			var longitude = parseFloat(markers[i].getAttribute("longitude"));
			var id = parseFloat(markers[i].getAttribute("id"));
			var location = new GLatLng(latitude, longitude);
			points[id] = addHilightPointer(location, id);
			points[id]['id'] = id;
			bounds.extend(points[id].getPoint());
		}
		
		// Define Zoom Level
		if (map.getBoundsZoomLevel(bounds) < zoom) {
			var zoomLevel = map.getBoundsZoomLevel(bounds)-1;
		} else {
			var zoomLevel = zoom;
		}
		
		// Define Map
		map.setCenter(bounds.getCenter(), zoomLevel, maptype);
		
		// Add maerkers to map
		for(curId in points) {
			map.addOverlay(points[curId]);
		}
	});
}

// Insert pointer into map if lat and lng is set
function addPointerToMap(latitude, longitude, zoom)
{
	map.clearOverlays();
	point = new GLatLng(latitude, longitude);
	map.setCenter(point, zoom, maptype);
	map.addOverlay(new GMarker(point));
}



function addHilightPointer(point, id) {
	var marker = new GMarker(point, icon);
	GEvent.addListener(marker, "mouseover", function() {
		document.getElementById("entry_"+id).style.background = "#f6f6f6";
	});
	GEvent.addListener(marker, "mouseout", function() {
		document.getElementById("entry_"+id).style.background = "transparent";
	});
	return marker;
}

function hilightPoint(id)
{
	points[id].setImage("http://labs.google.com/ridefinder/images/mm_20_red.png");
}
function unhilightPoint(id)
{
	points[id].setImage("http://labs.google.com/ridefinder/images/mm_20_white.png");
}

function freeMemory(){
	map = null;
	maptype = null;
	points = null;
	bounds = null
}
