﻿// The map.
var map = null;
var count = 0;

// Where the skier icon lives.
var skierIconURL = "http://www.westernsnowinfo.com/Images/skiing.png";

function initializeMap() 
{ 
	// Set up the base map.
	map = new GMap2(document.getElementById("map_canvas"));  
	map.setUIToDefault();
	map.setMapType(G_PHYSICAL_MAP);
	map.setCenter(new GLatLng(45, -115), 5);   

	// This causes all the info points to get created as a side effect.
	PageMethods.GetSnowReportCount(GetCountSucceeded, GetCountFailed);
}

function GetCountSucceeded(count, context, method)
{
//	alert("GetCount() Succeeded: " + count);
	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 name = info[0];
	var lat = info[1];
	var lon = info[2];
	var url = info[3];
	var snow24 = info[4];
	var snow7Days = info[5];
	var temp = info[6];
	var wind = info[7];
	
	DisplayResort(name, lat, lon, snow24, snow7Days, temp, wind, url)
}

function GetInfoFailed(error, context, method)
{
	alert("GetInfo() Failed: " + error);
}

function DisplayResort(title, lat, lon, snow24, snow7Days, temp, wind, url)
{
	var tooltip = title + "\n" + snow24 + "'' in 24 hrs";

	var details = "<table id='googleInfo'><tr><th colspan='2'>" + title + "</th></tr>";

	details += "<tr><td width='60%'>" + snow24 + "'' last 24 hrs</td>";
	details += "    <td>" + temp + "&deg; F</td></tr>";
	details += "<tr><td>" + snow7Days + "'' last 7 days</td>";
	details += "    <td>" + wind + "</td></tr>";

	details += "<tr><td colspan='2' style='text-align: center;padding-top: 4px;'>";
	details += "<a href='" + url + "' target='_self'>View the details</a>";
	details += "</td></tr></table>";

	var skierIcon = new GIcon(G_DEFAULT_ICON);
	skierIcon.image = skierIconURL;
	var markerOptions = { title:tooltip, icon:skierIcon};
	
	var point = new GLatLng(lat, lon);
	var marker = new GMarker(point, markerOptions);
	map.addOverlay(marker);
	GEvent.addListener(marker, "click", function() { marker.openInfoWindow(details); });  
}


