var map;
var geocoder;
var sGoogleMapsAddress = null
var sMapCanvasID = null
var sStoreName = null;

// addAddressToMap() is called when the geocoder returns an
// answer.  It adds a marker to the map with an open info window
// showing the nicely formatted version of the address and the country code.
function addAddressToMap(response) {
  var place = null;
  if (!response || (response.Status && response.Status.code != 200)) {
    document.getElementById(sMapCanvasID).style.display = 'none';
  } else if (response.Placemark) {
    place = response.Placemark[0];
    point = new GLatLng(place.Point.coordinates[1],
    place.Point.coordinates[0]);
    marker = new GMarker(point);
    map.addOverlay(marker);
    marker.openInfoWindowHtml('<strong>'+sStoreName+'</strong><BR/>'+place.address.replace(',',',<BR/>','g'));
  }
}


// findLocation() is used to enter the sample addresses into the form.
function findLocation(sAddress,sCanvas) {
  sMapCanvasID = sCanvas;
  map = new GMap2(document.getElementById(sCanvas));
  map.addMapType(G_PHYSICAL_MAP);
  map.setCenter(new GLatLng(34, 0), 15);
  map.addControl(new GMapTypeControl(1));
  map.addControl(new GLargeMapControl());
  
  geocoder = new GClientGeocoder();
  geocoder.getLocations(sGoogleMapsAddress, addAddressToMap);
}

document.OnUnload = GUnload();

if (document.addEventListener) 
  document.addEventListener("DOMContentLoaded", initGoogleMaps, false);


function initGoogleMaps() {
  // quit if this function has already been called
  if (arguments.callee.done) return;
  // flag this function so we don't do the same thing twice
  arguments.callee.done = true;
  // IE needs a little delay here..
  findLocation('','map_canvas');
}

if (navigator.appVersion.indexOf("MSIE")!=-1)
  setTimeout("initGoogleMaps()",400);
