﻿// The map.
var map = null;
var count = 0;

function initializeMap() 
{
	// TODO: Get the bounds and center from somewhere else.

	// Set up the base map.
	var element = document.getElementById("map_canvas");
	if (element == null)
	{
		return;
	}
	map = new GMap2(element);  
	map.setUIToDefault();
	map.setMapType(G_PHYSICAL_MAP);
	map.setCenter(new GLatLng(43.8, -111.117), 8);   

	// This causes all the info points to get created as a side effect.
	PageMethods.GetCount(GetCountSucceeded, GetCountFailed);
}

function GetCountSucceeded(count, context, method)
{
//	alert("GetCount() Succeeded: " + result);
	var i = 0;
	for (i=0; i<count; i++)
	{
		PageMethods.GetInfo(i, GetInfoSucceeded, GetInfoFailed);
	}
}

function GetCountFailed(error, context, method)
{
	alert("GetCount() Failed: " + error);
	count = 0;
}

function GetInfoSucceeded(info, context, method)
{
	var lat = info[0];
	var lon = info[1];
	var title = info[2];
	var details = info[3];
	
	var point = new GLatLng(lat, lon);
	var marker = new GMarker(point, {title:title});
	map.addOverlay(marker);
	GEvent.addListener(marker, "click", function() { marker.openInfoWindow(details); });  
}

function GetInfoFailed(error, context, method)
{
	alert("GetInfo() Failed: " + error);
}

