var map = null;
var geocoder = null;
var markerUrl = "images/googleMapPointer_red.png";

function ShowOnMap(tradingName, address, retryAddress){
  var mapNode = document.getElementById('map');

  if(mapNode){
    var map = new GMap2(mapNode);    
    var geocoder = new GClientGeocoder();
    

    // Create a base icon for all of our markers that specifies the
    // shadow, icon dimensions, etc.
    var baseIcon = new GIcon(G_DEFAULT_ICON);
    baseIcon.shadow = null;
    baseIcon.iconSize = new GSize(20, 34);
    baseIcon.shadowSize = new GSize(0, 0);
    baseIcon.iconAnchor = new GPoint(8, 35);
    baseIcon.infoWindowAnchor = new GPoint(9, 2); //(9,2)

    // Creates a marker whose info window displays the letter corresponding
    // to the given index.
    function CreateMarker(popupDisplayText, point) {
      var letteredIcon = new GIcon(baseIcon);
      letteredIcon.image = markerUrl;

      // Set up our GMarkerOptions object
      markerOptions = { icon:letteredIcon };
      var marker = new GMarker(point, markerOptions);

      GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(popupDisplayText);
      });
      
      return marker;
    }

    if(map && geocoder){
      map.addControl(new GLargeMapControl());

      geocoder.getLatLng(
        address,
        function(point) {
          if (!point) {
            if(retryAddress){
              ShowOnMap(tradingName, retryAddress, null);
            } else {
              $('map-description').html('The location specified could not be found');
            }
          } else {
            var popupDisplayText = "<b>" + tradingName + "</b><br />" + address;
            
            map.setCenter(point, 16);
            var marker = CreateMarker(popupDisplayText, point);
            map.addOverlay(marker);
            marker.openInfoWindowHtml(popupDisplayText);
          }
        }
      );
    }
  }
}
