var map;
var base_icon = new GIcon(G_DEFAULT_ICON);
base_icon.image = "themes/pegasus/images/locations/blue_marker.png";
var current_marker;

// Set up our GMarkerOptions object
markerOptions = { icon:base_icon };

function infoWindowHtml(label, marker_url) {
	return "<div style='width: 220px; height: 120px'><h4 style='font-weight: bold; margin-bottom: 0;'>" + label + "</h4>" +
			"<p>Visit the " + label + " site by clicking the button below. <br /> <a class=\"visit_btn\" href='http://"+marker_url+"'>Visit</a></p></div>";
}

function addMarker(id, latitude, longitude, label, marker_url) {
	
	// add marker to map
	var marker = new GMarker(new GLatLng(latitude, longitude), markerOptions);
	map.addOverlay(marker);
	var menu_item = $("li#menu_"+id);
	
	var focusPoint = function() {
		
		// set other current's to flase
		if(current_marker) current_marker.setImage("themes/pegasus/images/locations/blue_marker.png");
		current_marker = marker;
		marker.setImage("themes/pegasus/images/locations/red_marker.png");
		marker.openInfoWindowHtml(infoWindowHtml(label, marker_url));
		map.panTo(marker.getLatLng());
		
		//TODO: perhaps pass in the widget name?
		$("#locations_widget li.current").removeClass("current");
		$("#map_nav li.current").removeClass("current");
		menu_item.addClass("current");
		return false;
	}
	
	var overPoint = function() {
		marker.setImage("themes/pegasus/images/locations/red_marker.png");
	}
	
	var outPoint = function() {
		// TODO: don't reset to blue if we're the current marker (i.e. info window is open) 
		if(current_marker!=marker) marker.setImage("themes/pegasus/images/locations/blue_marker.png");
		//console.log("current: "+current_marker+" over: "+marker);
	}

	// add map & menu listeners	
	GEvent.addListener(marker, 'click', focusPoint);
	menu_item.click(focusPoint);
	
	GEvent.addListener(marker, 'mouseover', overPoint);
	GEvent.addListener(marker, 'mouseout', outPoint);
	menu_item.hover(overPoint, outPoint);
	
	// inner links don't work because click event is caught by parent element
	// so re-add default link behaviour
	var inner_item = $("li#menu_"+id+" h4 a");
	inner_item.click(function(){
		window.location.href="http://"+marker_url;
		return false;
	});
}


//TODO: Lazy load the map AFTER static content is visible
function initialize() {
	if (GBrowserIsCompatible()) {
	
		var nz = new google.maps.LatLng(-40.713956,172.902832);
		
		map = new GMap2(document.getElementById("map_canvas"));
		
		map.setCenter(nz, 6);
		map.addControl(new GLargeMapControl3D());
		map.addControl(new GScaleControl());
		
		// markers[] is defined in LocationPage customScript
		for (i in markers) {
			//console.log(markers[i].city + " " + markers[i].latitude + " " + markers[i].longitude);
			addMarker(markers[i].id, markers[i].latitude, markers[i].longitude, markers[i].label, markers[i].url);
		}
		
	}
}

// do stuff when DOM is ready
$(document).ready(function() {
	initialize();
});

// do stuff when page is unloaded
$(window).unload(function() {
	GUnload();
});