﻿if (GBrowserIsCompatible()) {
    var map = new GMap2(document.getElementById("doc_map_canvas"));
    var geocoder = new GClientGeocoder();
    var directionsPanel = document.getElementById("directionsPanel");
    var directions = new GDirections(map,directionsPanel);        
    map.addControl(new GLargeMapControl());
    var geocoder = new GClientGeocoder();        

    // Create our "tiny" marker icon
    var blueIcon = new GIcon(G_DEFAULT_ICON);
    var blueIconSize = new GSize(46, 36);
    blueIcon.image = "/images/mapGraphics/markerA.png";
    blueIcon.iconSize = blueIconSize;

    GEvent.addListener(directions, "addoverlay", hideDirMarkers);                

    // Set up our GMarkerOptions object
    markerOptions = { icon:blueIcon };
}

// Not using the directions markers so hide them.
function hideDirMarkers(){
    var numMarkers = directions.getNumGeocodes()
    for (var i = 0; i < numMarkers; i++) {
        if (i == 1) blueIcon.image = "/images/mapGraphics/markerB.png";
        var marker = directions.getMarker(i);
        if (marker != null)
        {
            marker.hide();
            var marker2 = new GMarker(marker.getPoint(),markerOptions);
            map.addOverlay(marker2);                                              
        }
        else
        {
            alert("Marker is null");
        }
    }                
    mapMarkers();
} 

function showAddress(lat,lon) 
{
    var point = new GLatLng(lat,lon);
    var marker = new GMarker(point,markerOptions);
    map.addOverlay(marker);              
    map.setCenter(point, 14);
}

function mapMarkers()
{
    $('#directionsPanel img:first').attr("src","/images/mapGraphics/markerA.png");
    $('#directionsPanel img:last').attr("src","/images/mapGraphics/markerB.png");
}

function loadDirections(lat,lon,from,to)
{    
    geocoder.getLatLng(from,function(point) 
    {
        if (!point) 
        {
            alert(address + " not found");
        } 
        else 
        {
            var lat1 = point.lat();
            var lon1 = point.lng();
            directions.load('from: Your Location@' + lat1 + ',' + lon1 + ' to: Doctor Location@' + lat + ',' + lon);
        }
    });
    mapMarkers();
}
