var geocoder = new GClientGeocoder();
var map;
var directionsPanel;
var directions;

// Create a base icon for all of our markers that specifies the
// shadow, icon dimensions, etc.
var baseIcon = new GIcon(G_DEFAULT_ICON);
//baseIcon.image = "images/MVS-Map-Icon.png";
//baseIcon.iconSize = new GSize(37, 31);
//baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
baseIcon.shadowSize = new GSize(37, 34);
baseIcon.iconAnchor = new GPoint(18, 31);
baseIcon.infoWindowAnchor = new GPoint(18, 31);

function initialize() {
    map = new GMap2(document.getElementById("map_canvas"));
    //directionsPanel = document.getElementById("route");
    //directions = new GDirections(map, directionsPanel);
    //GEvent.addListener(directions, "error", handleErrors);

    var mapTypeControl = new GMapTypeControl();
    var topRight = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(3, 3));
    var bottomRight = new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10, 10));
    map.addControl(mapTypeControl, topRight);
    GEvent.addListener(map, "dblclick", function() {
        map.removeControl(mapTypeControl);
        map.addControl(new GMapTypeControl(), topRight);
    });
    map.addControl(new GSmallMapControl());
    mapLocations();
}

var addressError;
function showAddress(address, index, title, myDelay) {
    setTimeout(function() {
        geocoder.getLatLng(
    address,
    function(point) {
        if (!point) {
            addressError = 'address not found';
            //alert(address + " not found");
        } else {
            //map.setCenter(point, 10);

            var letter = String.fromCharCode("A".charCodeAt(0) + index);
            var letteredIcon = new GIcon(baseIcon);
            letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";

            // Set up our GMarkerOptions object
            markerOptions = { icon: letteredIcon };
            var marker = new GMarker(point, markerOptions);
            map.addOverlay(marker, index);
            GEvent.addListener(marker, "click", function() {
                marker.openInfoWindowHtml("<b>" + title + "</b>");
            })

        }
    }
  );
    }, myDelay);
}

function showStart(address, index, title) {
    geocoder.getLatLng(
    address,
    function(point) {
        if (!point) {
            //alert(address + " not found");
        } else {
            map.setCenter(point, 11);

            var letter = String.fromCharCode("A".charCodeAt(0) + index);
            var letteredIcon = new GIcon(baseIcon);
            letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";

            // Set up our GMarkerOptions object
            markerOptions = { icon: letteredIcon };
            var marker = new GMarker(point, markerOptions);
            map.addOverlay(marker, index);
            GEvent.addListener(marker, "click", function() {
                marker.openInfoWindowHtml("<b>" + title + "</b>");
            })

        }
    }
  );
}


function createMarker(point, index) {
    var marker = new GMarker(point);
    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml("<span style='font-size: 14px'><strong>JB's Guide Service</strong><br />11800 Maumelle Harbour Rd<br /><br />Phone: 501-519-5196</span>");
    });
    return marker;
}

/*
function getDirections() {
var dirFrom = document.getElementById("DirFrom").value;
//document.getElementById("route").innerHTML = "";
directions.load("from: " + dirFrom + " to: Maumelle Harbour Rd @34.864101, -92.565594");
}
      
*/
function handleErrors() {
    if (directions.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
        alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + directions.getStatus().code);
    else if (directions.getStatus().code == G_GEO_SERVER_ERROR)
        alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + directions.getStatus().code);
    else if (directions.getStatus().code == G_GEO_MISSING_QUERY)
        alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + directions.getStatus().code);
    else if (directions.getStatus().code == G_GEO_BAD_KEY)
        alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + directions.getStatus().code);
    else if (directions.getStatus().code == G_GEO_BAD_REQUEST)
        alert("A directions request could not be successfully parsed.\n Error code: " + directions.getStatus().code);
    else alert("An unknown error occurred.");

    document.getElementById("DirFrom").innerHTML = 'Enter address above to view directions';
}



//*****************************
//**** Handle 'ENTER' Key *****
//*****************************

/*
document.onkeypress = handleEvent;
if (document.layers) document.captureEvents(Event.KEYPRESS);
function handleEvent(e) {
var code;
if (!e) var e = window.event;
if (e.keyCode) code = e.keyCode;
else if (e.which) code = e.which;

if(code == 13)
{
var ele = e.target || e.srcElement;
if (ele == document.getElementById("DirFrom")) {
getDirections();
return (false);
}
//if (document.getElementById("enterKey").value != 'false') return(false);
}
}
*/
//*****************************
//*****************************

