
// time (ms) between updates
var PERIOD = 1000;

// shuttle stops
var points = [];
points[points.length] = new GLatLng(42.366618, -71.11354); // peabody terrace
points[points.length] = new GLatLng(42.36532991791331, -71.12282752990722); // HBS
points[points.length] = new GLatLng(42.365028685777226, -71.12031698226929); // SFP
points[points.length] = new GLatLng(42.36712935348296, -71.12475872039795); // Stadium
points[points.length] = new GLatLng(42.37124328786433, -71.12114310264587); // Winthrop
points[points.length] = new GLatLng(42.37324070796323, -71.11926555633545); // H Square
points[points.length] = new GLatLng(42.36869886312712, -71.1151134967804); // Mather
points[points.length] = new GLatLng(42.372162743101676, -71.11533880233764); // H Inn?
points[points.length] = new GLatLng(42.37267794920396, -71.11508131027221); // Lamont
points[points.length] = new GLatLng(42.376347679634506, -71.11438393592834); // Mem Hall
points[points.length] = new GLatLng(42.37515087966602, -71.11936211585998); // Cemetary
points[points.length] = new GLatLng(42.37642693710535, -71.12224817276001); // Radcliffe Yard
points[points.length] = new GLatLng(42.37773, -71.123157); // 20 Garden St
points[points.length] = new GLatLng(42.378736, -71.124031); // 29 Garden St
points[points.length] = new GLatLng(42.38175280992323, -71.12531661987304); // Quad
points[points.length] = new GLatLng(42.37813094853328, -71.1199951171875); // HLS
points[points.length] = new GLatLng(42.37883631647602, -71.11669063568115); // MD?
points[points.length] = new GLatLng(42.37311388952802, -71.11739873886108); // Widener

// reference to sb's map
var map = null;

// quick-and-dirty cache for routes
var markers = [];
var timestamps = [];

// function that gets called upon initialization of Google Maps API
function initialize() 
{
    // ensure browser supports Maps
    if (!GBrowserIsCompatible())
        return;

    // ensure map belongs on this page
    if (!document.getElementById("map"))
        return;

    // instantiate map
    map = new GMap2(document.getElementById("map"));

    // center map on campus
    map.setCenter(new GLatLng(42.375222, -71.1183), 15);

    // add controls
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());

    // enable fancy features
    map.enableContinuousZoom();
    map.enableScrollWheelZoom();

    // display stops
    for (i in points)
    {
        // prepare icon for stop
        var icon = new GIcon(G_DEFAULT_ICON);
        icon.image = "http://www.people.fas.harvard.edu/~malan/sb/bluecirclemarker.png";
        icon.iconSize = new GSize(10,10);
        icon.iconAnchor = new GPoint(0,0);
        icon.shadow = null;

        // prepare options for marker
        var options = { icon: icon };

        // place marker
        var marker = new GMarker(points[i], options);
        map.addOverlay(marker);
    }

    // update map
    update();
}

function update()
{
      YAHOO.util.Connect.asyncRequest("GET", "http://www.people.fas.harvard.edu/~malan/sb/ajax.php", { success: handler });
}

// handler for updating map
function handler(o)
{
    // evaluate JSON
    var points = eval("(" + o.responseText + ")");

    // get current time
    var date = new Date();
    var time = date.getTime();

    // iterate over routes
    for (i in points)
    {
        // add bus to map if not already there
        if (!markers[points[i].id])
        {
            // prepare icon for marker
            var icon = new GIcon(G_DEFAULT_ICON);
            icon.image = "http://www.people.fas.harvard.edu/~malan/sb/blank.png";

            // prepare options for marker
            var options = { icon: icon , zIndexProcess: orderOfCreation };

            // place marker
            var marker = new GMarker(new GLatLng(points[i].lat, points[i].lng), options);
            map.addOverlay(marker);

            // remember marker
            markers[points[i].id] = marker;

            // remember time of marker's last update
            timestamps[points[i].id] = time;
        }

        // else move bus
        else
        {
            // find marker
            var marker = markers[points[i].id];

            // move marker
            marker.setLatLng(new GLatLng(points[i].lat, points[i].lng));

            // show marker
            if (marker.isHidden())
                marker.show();

            // remember time of marker's last update
            timestamps[points[i].id] = time;
        }
    }

    // remove stale buses
    for (i in timestamps)
    {
        if (timestamps[i] < time)
            markers[i].hide();
    }

    // do it all again in 2 seconds
    window.setTimeout(update, PERIOD);
}

function orderOfCreation(marker, b) 
{
    return 1;
}
