// JavaScript Document

//map globals
var map = null;
var geocoder = null;

//load map with center at harvard
window.onload = function initialize() {
	if (!GBrowserIsCompatible())
		return;

	map = new GMap2(document.getElementById("map_canvas"));
	map.setCenter(new GLatLng(38, -97), 4);
	map.setUIToDefault();
	map.addControl(new GMapTypeControl());
	
	start();
}

function addMarker(point, xhtml)
{
    var marker = new GMarker(point);
    GEvent.addListener(marker, "click", function() {
        map.openInfoWindowHtml(point, xhtml);
    });
    map.addOverlay(marker);
}

function start() {
	//communicate with map.php
	var request = null;
	try {
		request = new XMLHttpRequest();
	}
	catch (e) {
		request = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (request == null) {
		alert("Your browser does not support AJAX");
	}
	
	request.open("GET", "map.php", true);
	request.send(null);
	
	request.onreadystatechange = function() {
		if (request.readyState == 4) {
			if (request.status == 200) {
				var network = eval("(" + request.responseText + ")");
				for (var i = 0; i < network.length; i++) {
					var xhtml = "<b>" + network[i].name + "</b><br /> Class of " + network[i].class + "<br /> Currently " + network[i].status;
					var point = new GLatLng(network[i].lat,network[i].long);
					addMarker(point, xhtml);
				}
			}
		}
	}
}
