/*
 * (c) 2006-2009 David Pritchard
 * All rights reserved.
 */

function setup2010()
{
    routes.getRoute("bline97").hide(map);
    routes.getRoute("evergreen").show(map);
    document.getElementById('legend-evergreen').className = 'rowshown';
    document.getElementById('to2010').className = 'current';
    document.getElementById('toToday').className = '';
}
function setupToday()
{
    routes.getRoute("evergreen").hide(map);
    routes.getRoute("bline97").show(map);
    document.getElementById('legend-evergreen').className = 'rowhidden';
    document.getElementById('toToday').className = 'current';
    document.getElementById('to2010').className = '';
}

var today = true;
var defaultZoom = 12;
var defaultLong = -123.0798;
var defaultLat = 49.2485;
var defaultType = G_NORMAL_MAP;

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

if( location.search.length > 0 )
{
    var i;
    var args = location.search.slice(1).split('&');
    for( i = 0; i < args.length; ++i )
    {
        var arg, val;
        arg = args[i].split('=');
        if( arg.length != 2 )
            continue;
        val = arg[1];
        arg = arg[0];
        if( arg == 'long' )
        {
            defaultLong = parseFloat(val);
        }
        else if( arg == 'lat' )
        {
            defaultLat = parseFloat(val);
        }
        else if( arg == 'zoom' )
        {
            defaultZoom = parseFloat(val);
        }
        else if( arg == 'type' )
        {
            var mapTypes = map.getMapTypes();
            for( j = 0; j < mapTypes.length; ++j)
            {
                if( mapTypes[j].getName(true) == val )
                {
                    defaultType = mapTypes[j];
                }
            }
        }
        else if( arg == 'routes' )
        {
            today = (parseInt(val) == 0);
        }
    }
}

map.setUIToDefault();
map.addControl(new google.maps.LocalSearch(),
               new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10,20)));
map.setCenter(new GLatLng(defaultLat, defaultLong), defaultZoom, defaultType);
map.markermanager = new GMarkerManager(map);

var baseurl = "http://davidpritchard.org/maps/vantransit-2.0/";
var markerIcons = [
    new DTransitMarkerIcon(baseurl, "stop", new GSize(14,14),
             new GPoint(7,7), new GPoint(7,7))
];

// Setup routes.
var routes = new DTransitRouteSet(map, baseurl,
    ["expo", "millenium", "canada", "evergreen",
     "bline97", "bline99",
     "seabus", "wce"],
    markerIcons,
    [["bikelockers", "Bicycle Lockers"],
     ["parkandride", "Park and Ride"]]);
GEvent.addListener(map, "zoomend", function(oldZoomLevel, newZoomLevel)
    { routes.onZoom(oldZoomLevel, newZoomLevel); }
);


// Setup Link to this page.
document.getElementById('linktopage').onclick = function()
{
    var newurl = location.href;
    // Cut off existing query string
    if( newurl.indexOf('?') >= 0 )
        newurl = newurl.slice(0, newurl.indexOf('?'));
    var center = map.getCenter();
    newurl += "?lat=" + center.lat().toFixed(5)
            + "&long=" + center.lng().toFixed(5)
            + "&zoom=" + map.getZoom()
            + "&type=" + map.getCurrentMapType().getName(true)
            + "&routes=" + (today ? "0" : "1");
    location.href = newurl;
    return false;
}

document.getElementById('to2010').onclick = function()
{
    if( today )
    {
        setup2010();
        today = false;
    }
    return false;
}

document.getElementById('toToday').onclick = function()
{
    if( !today )
    {
        today = true;
        setupToday();
    }
    return false;
}

if( today )
{
    setupToday();
}
else
{
    setup2010();
}


