var DefaultAddress = "888 W 8 Ave Vancouver BC";
var VPLAddress = "350 West Georgia Street Vancouver BC Canada";
var MinCtrAddress = "1126 Douglas Road Burnaby BC Canada";

var VPLLocation;
var MinCtrLocation;
var CurrentLocation;
var MainMapCanvas;
var DriveMap;
var Directions;

function load() {
    if (GBrowserIsCompatible()) {
        VPLLocation = new GLatLng(49.2797,-123.1155);
        MinCtrLocation = new GLatLng(49.2745,-123.0197);

        MainMapCanvas = new GMap2(document.getElementById("MainMapCanvas"));
        MainMapCanvas.setCenter(VPLLocation, 15);
        MainMapCanvas.addControl(new GLargeMapControl);
        MainMapCanvas.addControl(new GMapTypeControl());
        MainMapCanvas.addOverlay(new GMarker(VPLLocation));
        MainMapCanvas.addOverlay(new GMarker(MinCtrLocation));
        
        DriveMap = new GMap2(document.getElementById("DriveMap"));
        DriveMap.setCenter(VPLLocation, 15);
        DriveMap.addControl(new GLargeMapControl);
        DriveMap.addControl(new GMapTypeControl());

        Directions = new GDirections(DriveMap, document.getElementById("Directions"));
        DriveMap.setCenter(VPLLocation, 15);

        GEvent.addListener(Directions, "error", HandleErrors);
    }

    switchid("Address");
    hidediv("OfficeAddress");
    showdiv("SundayAddress");
    CurrentLocation = "VPL";
}

function HandleErrors() {
    alert("Please check your address!\n\n e.g " + DefaultAddress);
}

function SetDirection(Origin, Destination) {
    Directions.load("from: " + Origin + " to: " + Destination, { "locale": "en_US" });
}

function SetCurrentLocation(NewLocation){
    if (NewLocation == "VPL") {
        hidediv("OfficeAddress");
        showdiv("SundayAddress");
        MainMapCanvas.panTo(VPLLocation, 15);
        DriveMap.panTo(VPLLocation, 15);
        document.getElementById("Destination").selectedIndex = 0;
    }
    else {
        hidediv("SundayAddress");
        showdiv("OfficeAddress");
        MainMapCanvas.panTo(MinCtrLocation, 15);
        DriveMap.panTo(MinCtrLocation, 15);
        document.getElementById("Destination").selectedIndex = 1;
    }
    CurrentLocation = NewLocation;
}

function ChangeLocation() {
    var SelectList = document.getElementById("Location");

    if (SelectList.options[SelectList.selectedIndex].text == "Sunday Service") {
        SetCurrentLocation("VPL");
    }
    else if (SelectList.options[SelectList.selectedIndex].text == "Ministry Center") {
        SetCurrentLocation("MinCtr");
    }
}

function RetrieveDirection() {
    var Origin = document.getElementById("OriginAddress").value;
    var SelectList = document.getElementById("Destination");
    
    if (SelectList.options[SelectList.selectedIndex].text == "Sunday Service Location") {
        SetDirection(Origin, VPLAddress);
    }
    else if (SelectList.options[SelectList.selectedIndex].text == "Ministry Center Location") {
        SetDirection(Origin, MinCtrAddress);
    }
}

function StopEnter(Event) {
    if (window.event && window.event.keyCode == 13 || Event.which == 13) {
        RetrieveDirection();
        return false;			
    }
    return true;
}
