function init() {
	/* show correct menu */
	if($('red-ul')) {
		$('red-ul').hide();
		$('green-ul').hide();
		$('blue-ul').hide();
		
		if(section==1) $('red-ul').show();
		else if(section==2) $('green-ul').show();
		else if(section==3) $('blue-ul').show();
		
		//Event.observe('red-nav', "click", function(event) { $('red-ul').toggle(); Event.stop(event); });
		//Event.observe('blue-nav', "click", function(event) { $('blue-ul').toggle();  Event.stop(event);});
		//Event.observe('green-nav', "click", function(event) { $('green-ul').toggle();  Event.stop(event); });
	}
	
	if($('back-link')) $('back-link').href = 'javascript:history.back()';
	
	showMonth('');

	if($('map-container')) {
		googleMap();
		Event.observe(window, 'unload', GUnload);
	}
}

function pageLoad() {
	dw_fontSizerDX.init();
	dw_fontSizerDX.setDefaults('px', 16, 10, 24);
	dw_fontSizerDX.set(16, 10, 24, ['div#content', 'div.content-main']);
}

Event.observe(window, 'load', pageLoad);


function showMonth(month) {
	if($('upcoming-events')) {
		 new Ajax.Updater('upcoming-events', '/js/ajax_calendar.php?section='+section+'&month='+month, {evalScripts: true});
	}
	if($('landing_upcoming_calendar')) {
		 new Ajax.Updater('landing_upcoming_calendar', '/js/ajax_calendar.php?section='+section+'&month='+month+'&short=true', {evalScripts: true});
	}
	return false;
}

function showDayInfo(day) {
	var pos = Element.cumulativeOffset($('calendarday'+day));
	if($('listingday'+day)) {
		$('listingday'+day).style.left = (pos[0])+"px";
		$('listingday'+day).style.top = pos[1]+"px";
		$('listingday'+day).show();	
	}
}

function hideDayInfo(day) {
	if($('listingday'+day)) {
		$('listingday'+day).hide();
	}
}

var map;
var gdir;
var geocoder = null;
var addressMarker;
var localSearch;
var magnaMarker;
function googleMap() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map-container"));
		gdir = new GDirections(map, document.getElementById("map-directions"));
		localSearch = new GlocalSearch();
        geocoder = new GClientGeocoder();
		GEvent.addListener(gdir, "load", onGDirectionsLoad);
        GEvent.addListener(gdir, "error", handleErrors);

		var point = new GLatLng(53.418250, -1.384945);
		map.addControl(new GSmallMapControl());
		map.setCenter(point, 13);
		
		var baseIcon = new GIcon();
//		baseIcon.shadow = "/images/we-are-here-shadow.png";
		baseIcon.iconSize = new GSize(42, 40);
//		baseIcon.shadowSize = new GSize(151, 42);
		baseIcon.iconAnchor = new GPoint(21, 20);

		var letteredIcon = new GIcon(baseIcon);
		letteredIcon.image = "/images/map-logo.png";
		markerOptions = { icon:letteredIcon };
		magnaMarker = new GMarker(point, markerOptions);
		/*GEvent.addListener(magnaMarker, "click", function() {
			alert("yes");
		});*/
		map.addOverlay(magnaMarker);	
		
	}
}

function setDirections(from) {
	localSearch.setSearchCompleteCallback(null, 
		function() {
			if (localSearch.results[0]) {    
				var resultLat = localSearch.results[0].lat;
				var resultLng = localSearch.results[0].lng;
				var points = Array();
				points[0] = new GLatLng(resultLat,resultLng);
				points[1] = new GLatLng(53.418250, -1.384945); //magna
				gdir.loadFromWaypoints(points);
				document.getElementById('map-directions').style.display = 'block';
			}
			else{
				handleErrors();
			}
		});  
	localSearch.execute(from + ", UK");
}

function onGDirectionsLoad(){ 
	magnaMarker.hide();
}

function handleErrors(){
	alert("Address could not be found");
}
