var googleMap = null;
var mapMarkers = new Array();
var directionsWindow = null;
var centreMap = new Array();
var zoomLevel = 7;

centreMap['lat'] = 53.30462107510271;
centreMap['lng'] = -0.37353515625;

mapMarkers[0] = new Array();
mapMarkers[0]['lng'] = 53.06540893533914;
mapMarkers[0]['lat'] = -0.5724048614501953;
mapMarkers[0]['text'] = '<strong>Househam Sprayers Ltd</strong><br />The New Forge, Leadenham<br />Lincoln, LN5 0PE';

mapMarkers[1] = new Array();
mapMarkers[1]['lng'] = 53.167678353706485;
mapMarkers[1]['lat'] = -0.17082452774047852;
mapMarkers[1]['text'] = '<strong>Househam Sprayers Ltd</strong><br />Roughton Moor, Woodhall Spa<br />Lincolnshire, LN10 6YQ';

function loadGoogleMap() {	
	if (GBrowserIsCompatible()) {
		
		baseIcon = new GIcon();
        baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
        baseIcon.iconSize = new GSize(20, 34);
        baseIcon.shadowSize = new GSize(37, 34);
        baseIcon.iconAnchor = new GPoint(9, 34);
        baseIcon.infoWindowAnchor = new GPoint(9, 2);
        baseIcon.infoShadowAnchor = new GPoint(18, 25);
        
        househamIcon = new GIcon(baseIcon);
		househamIcon.image = '/images/green_map_marker.png';
		
		markerOptions = { icon:househamIcon };
		
		var point = new GLatLng(centreMap['lat'], centreMap['lng']);
		
		googleMap = new GMap2(document.getElementById("google_map"));
		googleMap.setCenter(point, zoomLevel);
		googleMap.addControl(new GSmallMapControl());
		googleMap.addControl(new GMapTypeControl());
		
		var leadenhamMarker = new GMarker(new GLatLng(mapMarkers[0]['lng'], mapMarkers[0]['lat']), markerOptions);
		GEvent.addListener(
			leadenhamMarker,
			"click",
			function(){
				leadenhamMarker.openInfoWindowTabsHtml(
					[
						new GInfoWindowTab('Info', mapMarkers[0]['text']),
						new GInfoWindowTab('Directions', 'Enter Your Postcode:<br />'+
															'<form onsubmit="return getDirections(0)" style="padding: 0px; margin:0px;" action="#">'+
																'<input type="text" size="30" name="address_0" id="address_0" />'+
															'</form>'+
															'<a href="#" onclick="return getDirections(0)">Get Directions</a>'
						)
					],
					{
						maxWidth:150,
						selectedTab:0,
						pixelOffset: new GSize(200,200)
					}
				);
			}
		);
		googleMap.addOverlay(leadenhamMarker);
		
		var woodhallMarker = new GMarker(new GLatLng(mapMarkers[1]['lng'], mapMarkers[1]['lat']), markerOptions);
		GEvent.addListener(
			woodhallMarker,
			"click",
			function(){
				woodhallMarker.openInfoWindowTabsHtml(
					[
						new GInfoWindowTab('Info', mapMarkers[1]['text']),
						new GInfoWindowTab('Directions', 'Enter Your Postcode:<br />'+
															'<form onsubmit="return getDirections(1)" style="padding: 0px; margin:0px;" action="#">'+
																'<input type="text" size="30" name="address_0" id="address_1" />'+
															'</form>'+
															'<a href="#" onclick="return getDirections(1)">Get Directions</a>'
						)
					],
					{
						maxWidth:150,
						selectedTab:0,
						pixelOffset: new GSize(200,200)
					}
				);
			}
		);
		googleMap.addOverlay(woodhallMarker);
	}
}


function getDirections(markerId) {
	addressEle = document.getElementById('address_'+markerId);
	
	if(addressEle.value == '') {
		alert('Please enter your Postcode');
		return false;
	}
	
	url = 'http://maps.google.co.uk/maps?saddr='+escape(addressEle.value)+'&daddr='+mapMarkers[markerId]['lng']+','+mapMarkers[markerId]['lat'];
	
	if(directionsWindow!=null && !directionsWindow.closed && directionsWindow.location) {
		directionsWindow.location.href = url;	
	} else {
		directionsWindow = window.open(url, 'directions');
		if(!directionsWindow.opener) {
			directionsWindow.opener = self;
		}
	}
	
	if(window.focus) {
		directionsWindow.focus();	
	}
	
	return false;
}