(function($) {
  $.fn.googleMap = function() {
    $(this).each(function () {
      var targetElement, geocoder, targetContent, rawAddress, address;
      targetElement = $(this);
      
      rawAddress = targetElement.text();
      if ($.browser.msie) {
        rawAddress = targetElement[0].innerText;
      }
      address = rawAddress.replace(/\s/g, ' ');
      
      geocoder = new google.maps.Geocoder();
      geocoder.geocode({'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          var container, map, location, marker, info;
          container = $("<div class='map'></div>");
          container.insertAfter(targetElement);

          location = results[0].geometry.location;
        
          map = new google.maps.Map(container[0], {
            zoom: 14,
            center: location,
            mapTypeId: google.maps.MapTypeId.ROADMAP
          });
          marker = new google.maps.Marker({
            map: map,
            position: location
          });
          info = new google.maps.InfoWindow({
            content: targetElement.html() + "<br/><a target='_blank' href='http://maps.google.com/maps?saddr=&daddr=" + escape(address) + "'>Get Directions</a>",
            position: location
          });
          info.open(map);
          targetElement.hide();
        }
      });
    });
  }
})(jQuery);

