var map = null;
var geocoder = null;
var gsAddress = null;
var goMapDiv = null;
var gTryCount = 0;
var TRY_LIMIT = 10;
var gTimer = null;
var gbHidePointer = false;

function setAddressDiv(address, mapDiv, hidePointer) {

    gsAddress = address;
    goMapDiv = mapDiv;
    if (arguments.length > 2) {
        gbHidePointer = hidePointer;
    }
}

function initializeGoogleMaps() {
  window.clearTimeout(gTimer);
  try {
      if (GBrowserIsCompatible()) {
        map = new GMap2(goMapDiv);
        map.addControl(new GSmallMapControl());
        geocoder = new GClientGeocoder();
        showAddress(gsAddress);      
      } 
  } catch (e) {
        if (gTryCount < TRY_LIMIT) {
            gTryCount++;
            gTimer = window.setTimeout("initializeGoogleMaps()", 100);
        }
  }
}
 
function showAddress(address) {
  if (geocoder) {
    geocoder.getLocations(address, fromGetLocation);    
  }
}

function fromGetLocation(response) {
    
    var hidepoint = false;
    if (!response || response.Status.code != 200) {
        if (document.getElementById("googleMapDivInnacurate")) {
            document.getElementById("googleMapDivInnacurate").style.display = "block";
        }    
        return;
    }    
    place = response.Placemark[0];
    if (place.AddressDetails.Accuracy != 8) {
        if (document.getElementById("googleMapDivInnacurate")) {
            document.getElementById("googleMapDivInnacurate").style.display = "block";
        }
        hidepoint = true;
    }
    addAddressToMap(place.Point.coordinates[1],place.Point.coordinates[0],hidepoint);
}

function addAddressToMap(latitude,longitude, hidepoint) {

      map.addControl(new GSmallMapControl());        
      point = new GLatLng(latitude,longitude);
      map.setCenter(point, 13);
      if (!(hidepoint || gbHidePointer)) {
        var marker = new GMarker(point);
        map.addOverlay(marker);
      }
}