(function($){
	$.preloadImages = function() {
		for (var i=0; i < arguments.length; i++) {
			// window.console.log('Preloading: ' + arguments[i]);
			$('<img>').attr('src', arguments[i]);
		}
	}
})(jQuery);

$(document).ready(function(){
   	// Preload the submenu 
	$('#submenu img').each(function(){ $.preloadImages($(this).attr('src')); });
	
	// Activate CSS for the dynamic features
	$('body').addClass('active');
	
	$('#scroller').jScrollPane({scrollbarWidth: 11, scrollbarMargin: 12, showArrows: true});
	
	// Activate the media slideshow, if there is one
	var media = $('#media');
	if (media.children('li').length > 1) {
		media.innerfade({speed: 1000, timeout: 4000}).css({height: '343px', width: media.css('width')});
	}


	
});

// 
var initGMap = function(){};
var direct = function(f,t,l){};

(function() {
	var map; 
	var dir;
	
	
	var reportError = function(msg) {
		var err = $('#errors');
		var c = $('<p class="error">'+ msg +'</p>')
		err.empty();
		err.append(c);
	};
	
	/** Error handler for GMaps. */
	var handleGError = function() {
		if (dir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
			reportError("We couldn't translate your address into a geographic location, but we can probably do it from just your street and suburb. "+
						"Why don't you try that? (" + dir.getStatus().code +")");
		else if (dir.getStatus().code == G_GEO_SERVER_ERROR)
			reportError("We couldn't translate your address into a geographic location, and we can't tell why. "+
						"It might work if you try again. Why don't you try that? (" + dir.getStatus().code + ")");
		else if (dir.getStatus().code == G_GEO_MISSING_QUERY)
			reportError("We couldn't give you directions because you didn't say where you are. "+
						"Why not enter your address and try again? (" + dir.getStatus().code +")");
		else 
			reportError("We can't give you directions, and we aren't sure why. Sorry.");

	}
	
	/** Handle the new directions. */
	var loadedGDirections = function() {
		// Add link to these directions on Google...
		var f = escape(document.dirs.origin.value);
		var t = escape(document.dirs.destination.value);
		$('<p class="link"><a target="_blank" href="http://maps.google.com/maps?saddr=' +f+ '&daddr='+t+'">Printable directions &raquo;</a></p>').insertBefore('#directions');
		$('<p class="link"><a target="_blank" href="http://maps.google.com/maps?saddr=' +f+ '&daddr='+t+'">Printable directions &raquo;</a></p>').insertAfter('#directions');
		$('#scroller').jScrollPane({scrollbarWidth: 8, scrollbarMargin: 12, showArrows: true});
	}
	
	/** Initialise the directions. */
	var initDir = function() {	
		// Initialise the directions API
		dir = new GDirections(map, document.getElementById("directions"));
		
		// Add the event handlers
		GEvent.addListener(dir, "error", handleGError);
		GEvent.addListener(dir, "load", function(){$('#scroller p.link').remove(); $('#errors').empty(); });
		GEvent.addListener(dir, "addoverlay", loadedGDirections);
		
		// `direct` can now give directions (rather than doing nothing).
		direct = function(f,t,l) {
			dir.load("from: " + f + " to: " + t, { "locale": l });
		};
	}
	
	/** Initialise the map and directions (if present). */
	initGMap = function(){
		if (GBrowserIsCompatible()) {
			map = new GMap2(document.getElementById("map"));
			if (document.getElementById('directions')) initDir();
			
			var park = new GLatLng(-33.659, 115.250032);
			map.setCenter(park, 15);
			map.addOverlay(new GMarker(new GLatLng(-33.66026382171825, 115.24869561195374)));
			
		}
	};
	

})();

function initialise(){
	if (document.getElementById('map')) { initGMap(); }
};

function unload() {
	if (document.getElementById('map')) { GUnload(); }
};