var map,
    geocoder,
    strTrim = function ( str ) { return String(str).replace(/^\s+|\s+$/g, ''); },
    condenseWhitespace = function ( str, replacement ) {
        var replacement = replacement || ' ';
        return String(str).replace(/\s+/g, replacement);
    };
    
jQuery(function() {
    var storeAddress,
        
        googleMapInfo = {
            apiKeys: 'ABQIAAAAIVUwn7etHbWMQG-ZE3lMoRR2mcuotbjd43V3WmTAdEK3-Q9tZhS3NM885NAOzCFO_-5a7QSIh7_lYQ'
        },
        
        showAddress = function(address) {
            geocoder.getLatLng(address,	function(point) {
                if (!point) {
                    // We need to replace this error action with something better
                    alert(address + " not found");
                } else {
                    map.setCenter(point, 13);
                    var marker = new GMarker(point);
                    map.addOverlay(marker);
                    // marker.openInfoWindowHtml(address);
              }
            });
        };
    
    // Initialize the Google Map API
    map      = new GMap2(document.getElementById('map_canvas'));
    geocoder = new GClientGeocoder();
    
    // get the store address
    storeAddress = condenseWhitespace(strTrim(jQuery('#google_map .mapstoreaddress').text())),
    
    // Show the address of the store
    showAddress(storeAddress);
});

jQuery(document).unload(GUnload);


// Below is the sample code that came with google maps
// Whatever...


function initialize() {
 map = new GMap2(document.getElementById("map_canvas"));
 map.setCenter(new GLatLng(34, 0), 1);
 geocoder = new GClientGeocoder();
}

// 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) {
 map.clearOverlays();
 if (!response || response.Status.code != 200) {
   alert("Sorry, we were unable to geocode that address");
 } else {
   place = response.Placemark[0];
   point = new GLatLng(place.Point.coordinates[1],
                       place.Point.coordinates[0]);
   marker = new GMarker(point);
   map.addOverlay(marker);
   marker.openInfoWindowHtml(place.address + '<br>' +
     '<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode);
 }
}

// showLocation() is called when you click on the Search button
// in the form.  It geocodes the address entered into the form
// and adds a marker to the map at that location.
function showLocation() {
 var address = document.forms[0].q.value;
 geocoder.getLocations(address, addAddressToMap);
}

// findLocation() is used to enter the sample addresses into the form.
function findLocation(address) {
 document.forms[0].q.value = address;
 showLocation();
}
