﻿var map;
var gdir;
var geocoder = null;
var addressMarker;

function initialize(fromAddress, toAddress) {
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map_canvas"));
        gdir = new GDirections(map, document.getElementById("directions"));
        GEvent.addListener(gdir, "addoverlay", onGDirectionsLoad);
        GEvent.addListener(gdir, "error", customHandleErrors);

        setDirections(fromAddress, toAddress, "en_US");
    }
}

function setDirections(fromAddress, toAddress, locale) {
    gdir.load("from: " + fromAddress + " to: " + toAddress,
                { "locale": locale });
}

function customHandleErrors() {
    var canvas = document.getElementById("map_canvas");
    canvas.style.display = "none";

    var errorPanel = document.getElementById("error_panel");
    errorPanel.style.display = "block";
}

function onGDirectionsLoad() {
    var poly = gdir.getPolyline();
    if (poly.getVertexCount() > 100) {
        //alert("This route has too many vertices");
        return;
    }
    var baseUrl = "http://maps.google.com/staticmap?";

    var params = [];
    var markersArray = [];
    markersArray.push(poly.getVertex(0).toUrlValue(5) + ",greena");
    markersArray.push(poly.getVertex(poly.getVertexCount() - 1).toUrlValue(5) + ",greenb");
    params.push("markers=" + markersArray.join("|"));

    var polyParams = "rgba:0x0000FF80,weight:5|";
    var polyLatLngs = [];
    for (var j = 0; j < poly.getVertexCount(); j++) {
        polyLatLngs.push(poly.getVertex(j).lat().toFixed(5) + "," + poly.getVertex(j).lng().toFixed(5));
    }
}