// Start up the gallery script.
// TODO: create a merge of options so caller doesn't need to pass all options.
function initGallery(options) {
	
	// Grab default options if none passed.
	if (!options) {
		var options	= getGalleryOptions();
	}
	
	hideFullSizeImages(options);			// Hide the images before we show them.
	showFirstFullSizeImage(options); 	// Show the first full size image.
	bindThumbImages(options);				// Adds click event to thumbnail.
}

// Hides all the full size images on a page load.
function hideFullSizeImages(options) {
	
	// Hide the full size images.
	if (options.full_size_image_item_element) {
		$(options.full_size_image_item_element).hide();
	}
}

// Wrapper to show the first full sized image found in the dom.
function showFirstFullSizeImage(options) {
	showFullSizeImage(options, 0);
}

// Adds the an onclick event to the thumbs.
// Shows the full size image.
function bindThumbImages(options) {
	if (options.thumb_image_item_element) {
		$(options.thumb_image_item_element + ' a').each(function(index, val) {
			$(this).bind('click', function(){
				showFullSizeImage(options, index);
				return false;
			});
		});
	}
}

// Shows one full size image from the index on the dom.
// Activates the active thumb function.
function showFullSizeImage(options, index) {
	if (options.full_size_image_item_element) {
		hideFullSizeImages(options);
		$(options.full_size_image_item_element + ':eq(' + index + ')').fadeIn('slow');
		highlightActiveThumb(options, index); 	// Add class to active thumb.
	}
}

// Function to add an active state to the current image thumbnail being viewed.
function highlightActiveThumb(options, index) {
	if (options.thumb_image_item_element) {
		$(options.thumb_image_item_element + ' img').removeClass('gallery-thumb-active');
		$(options.thumb_image_item_element + ':eq(' + index + ') img').addClass('gallery-thumb-active');
	}
}

// Default options are used when none are passed.
function getGalleryOptions() {
	return options = {
		gallery_wrapper_element				: '.gallery-wrapper',
		full_size_image_wrapper_element	: '.full-size',
		thumb_image_wrapper_element		: '.thumb_size',
		full_size_image_item_element		: '.large-image',
		thumb_image_item_element			: '.thumb-image'
	}
}