$(document).ready(
	function() {
		
		if(window.GBrowserIsCompatible || GBrowserIsCompatible()) {
			
			$('body').addClass('has-googlemaps'); // class to the body for styling
			
			$('<div>').attr('id','map').appendTo($('#venue-map'));
			var map = new GMap2($('#map').get(0));
			map.addControl(new GSmallMapControl());
			//map.addControl(new GMapTypeControl());
			//map.enableScrollWheelZoom();
			
			setup_point = new GLatLng(53.75, -1.75); // shows all the venues in the one window
			map.setCenter(setup_point, 6);
			
			// make the 'return to' a link, on the paragraph
			$('<p>').text('Click the location name or marker on the main map to reveal the local map. ').attr('id','map-return').insertAfter('#map');
			$('<a>').text('Return to main map view.').attr('id','map-return-view').attr('href','#zoom-out').click(function(){ map.setCenter(setup_point, 6); $(this).blur(); return false; }).appendTo('#map-return');
			
			function createMarker(map, lat, lon, text) {
				var point	= new GLatLng(lat, lon);
				var marker 	= new GMarker(point);
				GEvent.addListener(marker, 'click', function() {
					map.setZoom(10);
					map.panTo(point);
					
					/*
					$('#get-directions').remove();
			
					$('<form>').attr({id: 'get-directions', action: 'http://maps.google.co.uk/maps', method: 'get'}).appendTo('#content-primary').hide();
					$('<fieldset>').attr('id','get-directions-fieldset').appendTo('#get-directions');
					$('<label>').attr('for','saddr').text('Get directions to '+text+', simply put in your postcode').appendTo('#get-directions fieldset');
					$('<input>').attr({id: 'saddr', name: 'saddr', type: 'text', value: ''}).insertAfter('#get-directions fieldset label');
					
					$('<input>').attr({id: 'submit_directions', name: 'submit_directions', type: 'submit', value: 'Go'}).click(function(){
						//window.location = 'http://maps.google.co.uk/maps?daddr='+lat+','+lon+'&h1=en&saddr='+$('#saddr').val();
					}).insertAfter('#get-directions fieldset label + input');
					
					$('<input>').attr({id: 'venue-name', name: 'venue-name', type: 'hidden', value: text}).insertAfter('#get-directions fieldset');
					$('<input>').attr({id: 'longitude', name: 'longitude', type: 'hidden', value: lon}).insertAfter('#get-directions fieldset');
					$('<input>').attr({id: 'latitude', name: 'latitude', type: 'hidden', value: lat}).insertAfter('#get-directions fieldset');
					$('<input>').attr({id: 'hl', name: 'hl', type: 'hidden', value: 'en'}).insertAfter('#get-directions fieldset');
					$('<input>').attr({id: 'daddr', name: 'daddr', type: 'hidden', value: lat+','+lon}).insertAfter('#get-directions fieldset');				

					marker.openInfoWindowHtml($('#get-directions').html());
					*/
				});
				return marker;
			}
			
			$('body.venues #content-primary .venues').each(function(i) {
				$(this).find('h4 strong').remove();
				venue	 	= $.trim($(this).find('h4').text());
				lat 		= $(this).find('.latitude').text();
				lon 		= $(this).find('.longitude').text();
				postcode 	= $(this).find('.postal-code').text();
				
				// on the post code...
				// click for google maps directions
				$(this).find('.postal-code').wrap('<a>');
				$(this).find('.postal-code').parent('a').attr('href','http://maps.google.co.uk/maps?f=d&ie=UTF8&z=10&om=1&daddr='+postcode+'&saddr=').attr('title','Click for Google maps directions');
				
				point 		= new GLatLng(lat, lon);
				marker 		= new GMarker(point);
				this_zoom 	= map.getZoom();
				
				map.addOverlay(marker);
				map.addOverlay(createMarker(map, lat, lon, venue));
				
				$(this).find('h4').addClass('clickable').click(function() {
					clicked_lat 		= $(this).parent('.venues').find('.latitude').text();
					clicked_lon 		= $(this).parent('.venues').find('.longitude').text();
					clicked_point 		= new GLatLng(clicked_lat, clicked_lon);
					clicked_venue	 	= $.trim($(this).text());
					map.setZoom(10);
					map.panTo(clicked_point);
					//map.openInfoWindowHtml(clicked_point,clicked_venue);
				});
			});
		}
		
	}
);
$(document).unload(GUnload);