﻿
addLoadEvent(loadMap);
window.onunload = GUnload;

/////////////////////////////////////////////////////////////////////////////////////////
//// Global Variables
//Used for map controler
var map = null;

//Used for directions controler
var gdir = null;

//Used for map marker links
var mapMarkers = [];

//Array of html text to be applied to individual pointers
var HTML = [
            '<h3>Montlake E1 Lot</h3> $5 daily with no overnight parking<br />Enter at Walla Walla Rd.',
            '<h3>Central Plaza Garage</h3> $11.00 weekdays from 6 a.m.-4 p.m.<br />$5.00 weekdays from 4-9 p.m.<br />Refunds available for stays shorter than 4 hours<br />Enter at NE 41st St. and 15th Avenue',
            '<h3>Padelford Parking Garage</h3> $11.00 weekdays from 6 a.m.-4 p.m.<br />$5.00 weekdays from 4-9 p.m.<br />Refunds available for stays shorter than 4 hours<br />Enter at Pend Oreille Road off Montlake Blvd. NE.',
            '<h3>Mary Gates Hall</h3> <img src="../images/mghexterior.jpg" />' +
            '<p>The Information School<br /><br />Mary Gates Hall, Ste 370<br />Seattle, WA 98195-2840<br />Box 352840<br /><br /><strong>Phone:</strong> (206) 685-9937</p>',
            '<h3>Roosevelt Commons Building</h3> <img src="../images/rendering-sm.jpg" />' +
            '<p>The Information School Research Commons<br /><br />Roosevelt Commons Building<br />4311 11th Ave NE<br />Seattle, WA 98105-4608<br />Box 354985<br /><br /><strong>Phone:</strong> (206) 616-1715</p>'];

//Indexical number   
var x = 0;

// Array for decoding the failure codes
var error=[];
error[G_GEO_SUCCESS]            = "Request succesfully completed";
error[G_GEO_MISSING_ADDRESS]    = "The address is either missing or has no value.";
error[G_GEO_UNKNOWN_ADDRESS]    = "A geographic location could be found for your address.";
error[G_GEO_UNAVAILABLE_ADDRESS]= "The address was not returned because of legal or contractual reasons.";
error[G_GEO_BAD_KEY]            = "The API key is either invalid or does not match a valid domain.";
error[G_GEO_TOO_MANY_QUERIES]   = "The daily search limit for this site has been exceeded.";
error[G_GEO_SERVER_ERROR]       = "The server produced an erros while processing your request.";
error[G_GEO_BAD_REQUEST]        = "The directions request could not be parsed.";
error[G_GEO_MISSING_QUERY]      = "Please specify an address.";
error[G_GEO_UNKNOWN_DIRECTIONS] = "The engine could not generate directions from your address.";


/////////////////////////////////////////////////////////////////////////////////////////
////Function to load the map initially and add the parking garage markers
function loadMap()
{
    if( GBrowserIsCompatible() )
    {
        //load map and its controls
        map = new GMap2(document.getElementById("map_canvas"));
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        map.setCenter(new GLatLng(47.655849, -122.305083), 15);
        
        //load individual lots
        var E = createMarker(new GLatLng(47.657721, -122.300341), HTML[0]);
        map.addOverlay(E);
        var C = createMarker(new GLatLng(47.655885, -122.309697), HTML[1]);
        map.addOverlay(C);
        var P = createMarker(new GLatLng(47.657045, -122.303308), HTML[2]);
        map.addOverlay(P);
        
        //load MGH
        var MGH = createMarker(new GLatLng(47.654844,-122.307905), HTML[3]);
        map.addOverlay(MGH);
        
        //load RCB
        var RCB = createMarker(new GLatLng(47.659896, -122.316437), HTML[4]);
        map.addOverlay(RCB);
        
        //create directions object
        gdir = new GDirections(map, document.getElementById("map_text"));
        
        // catch errors on search
        GEvent.addListener(gdir, "error",
                                function()
                                {
                                    var code = gdir.getStatus().code;
                                    var reason = "Code " + code;
                                    if( error[code] ){
                                        reason = error[code];
                                    }
                                    alert("Failed to obtain directions, " + reason);
                                });
        
        //open the appropriate info window
        parseQueryString();
    }
    else
    {
        //alert user their browser is not compatible with the API
        alert("Sorry, " + navigator.appName + " is not compatible with the Google Maps API");
    }
}

/////////////////////////////////////////////////////////////////////////////////////////
////Function to search and load directions
function getDirections()
{
    var toADDR = document.getElementById("toADDR").value
    var fromADDR = document.getElementById("fromADDR").value
    gdir.load( "from: " + fromADDR + " to: " + toADDR );
}

/////////////////////////////////////////////////////////////////////////////////////////
////Function to create and return an individual marker
//Param: point includes lat and lng coordinates
//Param: html is the html text from a global array
function createMarker(point, html)
{
    var marker = new GMarker(point);
    

    html += '<br /><br /><strong>Start address:</strong><form action="javascript:getDirections()">' +
    '<input type="text" SIZE=40 MAXLENGTH=40 name="fromADDR" id="fromADDR" value="" /><br>' +
    '<INPUT value="Get Directions" TYPE="SUBMIT">' +
    '<input type="hidden" id="toADDR" value="'+name+"@"+ point.lat() + ',' + point.lng() + 
    '"/>';
    
    //save new html and marker info
    HTML[x] = html;
    mapMarkers[x] = marker;
    
    //create event listener to open info windows
    GEvent.addListener(marker, "click", function(){ marker.openInfoWindow( html ); });
    
    //increase index and return the marker
    x++;
    return marker;    
}

/////////////////////////////////////////////////////////////////////////////////////////
////Function to open info window from links above map
//Param: index number to open specific window
function myclick(i)
{
    mapMarkers[i].openInfoWindowHtml(HTML[i]);
}


/////////////////////////////////////////////////////////////////////////////////////////
////Function to parse the querystring and see what locaiton marker should be expanded on load
//Param: index number to open specific window
function parseQueryString()
{
    var qrStr = window.location.search;
    var spQrStr = qrStr.substring(1);
    var arrQrStr = new Array();
    
    // splits each of pair
    var arr = spQrStr.split('&');

    for (var i=0;i<arr.length;i++){
        // splits each of field-value pair
        var index = arr[i].indexOf('=');
        var key = arr[i].substring(0,index);
        var val = arr[i].substring(index+1);

        // saves each of field-value pair in an array variable
        arrQrStr[key] = val;
    }

    //open the appropriate popup window
    switch (arrQrStr["location"]){
        case "mgh":
        myclick(3);
        break;
        case "rcb":
        myclick(4)
        break;
        default : myclick(3);
    }
}
