/**
 * Author: PerthWeb
 */
$(document).ready( function() {

	//
	// Home Page
	//
	if( $('#banner').length != 0 ) {
		$('#banner').cycle('fade');
	}

	//
	// Gallery Page
	//
	if( $('#gallery-right').length != 0 && $('#gallery-left').length != 0 ) {
		// Initialise the left gallery
		initGallery($('#gallery-left img'), $('#preview-left img'));

		// Initialise the right gallery
		initGallery($('#gallery-right img'), $('#preview-right img'));
	}

	//
	// Map Page
	//
	if( ($('a.map').length != 0) && ($('#mapView').length != 0) ) {
		$('a.map').click( function(event) {
			event.preventDefault();
			var mapHref = $(this).attr('href');
			$('#mapView').attr('src', mapHref);
			$('#mapViewLarger').attr('href', mapHref);
		});
	}
});


/**
 * Initialises a gallery defined by a set of thumbnail images (galleryThumbnailImages) and a full size image (galleryFullSizeImage).
 *
 * @param jQuery galleryThumbnailImages Collection of image object for images that make up the thumbnails for the gallery
 * @param jQuery galleryFullSizeImage Fullsize image object that displays the full size verison of a thumbnail
 * @param string|null dirThumbnail The directory for the thumbnail images relative to the base directory defined by the first thumbnail src
 * @param string|null dirFullSize The directory for the full size images relative to the base directory defined by the first thumbnail src
 * @param boolean|null hoverOutToDefault Whether hovering out of a thumbnail image returns the full size image to the default full size image
 */
function initGallery( galleryThumbnailImages, galleryFullSizeImage, dirThumbnail, dirFullSize, hoverOutToDefault ) {
	dirThumbnail = ( jQuery.type(dirThumbnail) != 'string' || dirThumbnail.length == 0 ) ? 'small' : dirThumbnail;
	dirFullSize = ( jQuery.type(dirFullSize) != 'string' || dirFullSize.length == 0 ) ? 'large' : dirFullSize;

	// Determine the base path from the first image src
	var regExpBasePath = new RegExp("(.+\/)" + dirThumbnail, 'i');
	var regExpBasePathMatches = regExpBasePath.exec(galleryThumbnailImages.first().attr('src'));

	var basePath = '';
	if( regExpBasePathMatches[1] ) {
		basePath = regExpBasePathMatches[1];
	}

	var regExpImgFilename = new RegExp(".+\/([A-Za-z0-9_]+\.[A-Za-z0-9_]{3})$", 'i');

	galleryThumbnailImages.each( function(index, element) {
		// Extract the image filename from the thumbnail image
		var regExpImgFilenameMatches = regExpImgFilename.exec($(this).attr('src'));

		if( !regExpImgFilenameMatches[1] ) {
			// Can't get the filename, so skip this item.
			return true;
		}
		
		imgFileName = regExpImgFilenameMatches[1];
		srcFullSize = basePath + dirFullSize + '/' + imgFileName;

		// Pre-load the large version of the image
		$('<img>').attr('src', srcFullSize);

		// Ensure properties are stored for this image that can be utilised later.
		$(this).data('imgFullSize', galleryFullSizeImage);
		$(this).data('srcFullSize', srcFullSize);
		$(this).data('srcDefault', galleryFullSizeImage.attr('src'));
		$(this).data('hoverOutToDefault', false);

		if( jQuery.type(hoverOutToDefault) == 'boolean' ) {
			$(this).data('hoverOutToDefault', hoverOutToDefault);
		}

		// Bind a hover handler to show a full size version of the thumbnail image when hovered over.
		$(this).hover(
			function() {
				// Hover in
				var srcFullSize = $(this).data('srcFullSize');
				var speedOut = 'fast';
				var speedIn = 'fast';

				$(this).data('imgFullSize').stop().fadeTo(speedOut, 0.5, function() {
					$(this).attr('src', srcFullSize).fadeTo(speedIn, 1);
				});
			},
			function() {
				// Hover out
				if( $(this).data('hoverOutToDefault') === true ) {
					var srcDefault = $(this).data('srcDefault');
					var speedOut = 'fast';
					var speedIn = 'fast';

					$(this).data('imgFullSize');
					$(this).data('imgFullSize').stop().fadeTo(speedOut, 0.5, function() {
						$(this).attr('src', srcDefault).fadeTo(speedIn, 1);
					});
				}
			}
		);
	});
}

























