
var GoogleMaps = Class.create(
{	
	initialize: function()
	{
		if ($('directionsTab')) {
			$('directionsTab').observe('click', this.initMap.bind(this));
		}
		
		if ($('directionsLink')) {
			$('directionsLink').observe('click', this.initMap.bind(this));
		}
		
		this.initMap();
	},
	
	initMap: function()
	{
		if (!this.initialized && $('directions').visible())
		{
			this.initialized = true;
			
			if ($('postcodeValue'))
			{
				var geocoder;
				var map;
				var latlng = new google.maps.LatLng(0, 0);
				var postcode = $('postcodeValue').innerHTML;
				
				geocoder = new google.maps.Geocoder();
		    	
		    	var options = {
		    		zoom: 14,
		      		center: latlng,
		      		mapTypeId: google.maps.MapTypeId.ROADMAP
		    	}
		    	
		    	map = new google.maps.Map($('googleMapCanvas'), options);
		    	
			    if (geocoder) 
			    {
			    	geocoder.geocode({'address': postcode}, function(results, status)
			    	{
				        if (status == google.maps.GeocoderStatus.OK)
				        {
				        	map.setCenter(results[0].geometry.location);
				        	var marker = new google.maps.Marker({
				        		map: map, 
				        		position: results[0].geometry.location
				        	});
				        }
			    	});
			    }
			}
		}
	}
});

document.observe('dom:loaded', function() {
	new GoogleMaps();
});

