// make new map for #map_id
// get directions for each waypoint
// plot on map
function makeMap(id, way_points) {
	var map = new GMap2(document.getElementById("map_" + id));
	
	//map.setCenter(nz, 6);
	map.addControl(new GSmallZoomControl());
	
	//may need to make multiple objects because of asynchronous behaviour
	var directions = new GDirections(map);
	directions.loadFromWaypoints(way_points);
	
	// wait for load, then add distance and drive times
	// GEvent.addListener(directions, "load", function() {
	// 		$("#trip_"+id+" h5.drive_time").html(directions.getDuration().html);
	// 		$("#trip_"+id+" h5.distance").html(directions.getDistance().html);
	// 	} );

}

//TODO: Lazy load the map AFTER static content is visible
function initialize() {
	if (GBrowserIsCompatible()) {
		
		// make a map for each trip
		// trips[] is defined in ItineraryHolder customScript
		if(window.trips) {
			for (i in trips) {
				//console.log(markers[i].city + " " + markers[i].latitude + " " + markers[i].longitude);
				makeMap(trips[i].id, trips[i].way_points);
			}
		}
		
	}
}

// do stuff when DOM is ready
$(document).ready(function() {
	initialize();
	
	//make trip links open in new window
	$('.itineraries .trip a').click(function(){this.target = "_blank"});
});

// do stuff when page is unloaded
$(window).unload(function() {
	GUnload();
});