$(function() {
	var onMouseOutOpacity = 0.67;
	$('#thumbs ul.thumbs li').opacityrollover({
		exemptionSelector: '.selected',
		fadeSpeed: 'fast',
		mouseOutOpacity: onMouseOutOpacity,
		mouseOverOpacity: 1.0
	});
	
	var gallery = $('#thumbs').galleriffic({
		autoStart: true,
		captionContainerSel: '#caption',
		delay: 4000,
		enableBottomPager: false,
		enableTopPager: false,
		imageContainerSel: '#slideshow',
		numThumbs: 7,
		preloadAhead: 5,
		renderNavControls: true,
		renderSSControls: true,
		syncTransitions: true,
		onSlideChangeOut: function(prevIndex) {
			var prevThumb = this.find('ul.thumbs').children().eq(prevIndex);
			grayscale(prevThumb.find('a.thumb span.thumb-image'));
			
			// Fade out previous thumbnail
			prevThumb.fadeTo('fast', onMouseOutOpacity);
			prevThumb.find('.thumb-border').stop(true, true).fadeOut('fast');
		},
		onSlideChangeIn: function(nextIndex) {
			var nextThumb = this.find('ul.thumbs').children().eq(nextIndex);
			grayscale.reset(nextThumb.find('a.thumb span.thumb-image'));
			
			// Fade in new thumbnail
			nextThumb.fadeTo('fast', 1.0);
			nextThumb.find('.thumb-border').stop(true, true).fadeIn('fast');
		},
		onPageTransitionOut: function(callback) {
			this.fadeTo('fast', 0.0, callback);
		},
		onPageTransitionIn: function() {
			var prevPageLink = $('a.prev').fadeTo('fast', 0.4);
			var nextPageLink = $('a.next').fadeTo('fast', 0.4);
			
			// Show appropriate next / prev page links
			if (this.displayedPage > 0)
				prevPageLink.fadeTo('fast', 1.0);
			var lastPage = this.getNumPages() - 1;
			if (this.displayedPage < lastPage)
				nextPageLink.fadeTo('fast', 1.0);
				
			this.fadeTo('fast', 1.0);
			
			var currrentThumb = $('li.selected');
			currrentThumb.find('.thumb-border').stop(true, true).fadeIn('fast');
			grayscale.reset(currrentThumb.find('a.thumb span.thumb-image'));
		}
	});
	
	$('a.prev').click(function(e) {
		gallery.previousPage();
		e.preventDefault();
	});
	$('a.next').click(function(e) {
		gallery.nextPage();
		e.preventDefault();
	});
	
	$('#thumbs a.thumb span.thumb-image').hover(function() {
		if (!$(this).parents('li').hasClass('selected')) {
			grayscale.reset($(this));
		}
	},
	function() {
		if (!$(this).parents('li').hasClass('selected')) {
			grayscale($(this));
		}
	});
});

$(window).load(function() {
	grayscale($('li:not(.selected) a.thumb span.thumb-image'));
	$('div.gallery').fadeIn('slow');
});
