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;
var goLatLong=null;
var giZoom = null;




function setAddressDiv(address, mapDiv, hidePointer, poLatLong, piZoom) {

    gsAddress = address;
    goMapDiv = mapDiv;
    if (arguments.length > 2) {
        gbHidePointer = hidePointer;
    }
    goLatLong = poLatLong;
    giZoom = piZoom;
}

function initializeGoogleMaps() {
  window.clearTimeout(gTimer);
  try {
      if (GBrowserIsCompatible()) {
        map = new GMap2(goMapDiv);
        map.addControl(new GSmallMapControl());
        if (goLatLong){
            showAddress(goLatLong,giZoom);
        }
        else{
            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 showAddress(poLatLong,piZoom) {
    map.addControl(new GSmallMapControl());
    map.setCenter(poLatLong,piZoom);
    if (!gbHidePointer){
        var marker = new GMarker(poLatLong);
        map.addOverlay(marker);
    }
}


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);
      }
}
