$().ready(function(){
	
	if ($('#restaurant-info-map').length && $('.info_wrapper').length) {
		restaurantMap.getCoordinates();
	}  
	
	if ($('div.tabs > a[href$="info"]').length) {
		$('div.tabs > a[href$="info"]').click(function(){
			if (!$('#restaurant-info-map').css('background')) 
				restaurantMap.getCoordinates();
		})
	}
})

var restaurantMap = {	
	getCoordinates: function() {
		var geocoder = new google.maps.Geocoder();
		  geocoder.geocode( { 'address': $('.info_wrapper').find('.address').text()}, function(results, status) {
		      if (status == google.maps.GeocoderStatus.OK) {
			  	restaurantMap.init(results[0].geometry.location.lat().toFixed(5), results[0].geometry.location.lng().toFixed(5));
	 		  } else {
		        alert("Geocode was not successful for the following reason: " + status);
		      }
		    });
	},
	
	init: function(lat, lng) {
		$('#restaurant-info-map').css('background', 'url(http://maps.google.com/maps/api/staticmap?markers=color:red|' + lat + ',' + lng + '&zoom=15&size=370x300&sensor=false) no-repeat');
	}
}
