var googleMap = null;
var coordLat = '53.139337488582484';
var coordLng = '-0.0011050701141357422';
var zoomLevel = 15;
var directionsWindow = null;
var infoWindowTabs = [];
var infoWindowOptions = {
	maxWidth:150,
	selectedTab:0,
	pixelOffset: new GSize(200,200)
};


function loadGoogleMap() {
 
    if (GBrowserIsCompatible()) {        
        googleMap = new GMap2(document.getElementById("google_map"));
        googleMap.setMapType(G_HYBRID_MAP);
        
        var point = new GLatLng(coordLat, coordLng);
        
        googleMap.setCenter(point, zoomLevel);
        googleMap.addControl(new GSmallMapControl());
        googleMap.addControl(new GMapTypeControl());

	    var icon = new GIcon(G_DEFAULT_ICON,"/images/google_markers/aviation.png",'','','','','','');
	    icon.shadow = '';
		icon.iconSize = new GSize(32, 32);
		var markerOptions = { icon:icon };	
		var marker = new GMarker(point, markerOptions);
		
		infoWindowTabs = [
			new GInfoWindowTab('Info', '<strong>Lincolnshire Aviation Heritage Centre,</strong><br />East Kirkby Airfield,<br/>East Kirkby,<br />Nr Splisby,<br />Lincolnshire,<br />PE23 4DE'),
			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>'+
				'<br/><a href="#" onclick="return getDirections(0)">Get Directions</a>'
			)
		];
		
		GEvent.addListener( marker, "click", function(){ marker.openInfoWindowTabsHtml(infoWindowTabs, infoWindowOptions); } );
		
		googleMap.addOverlay(marker);
		marker.openInfoWindowTabsHtml(infoWindowTabs, infoWindowOptions);
    }
}


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='+coordLat +','+coordLng;
	
	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;
}

addLoadEvent(loadGoogleMap);
addUnLoadEvent(GUnload);