/* Author: Barnaby Walters
	
	With great thanks to Jacob Gube for the jQuery slideshow code. Thanks!
	
*/

$(document).ready(function (){
	
	// category div mouseover
	
	$('.categoryDiv').click(function () {
		
		window.location.href = $(this).children('a').attr('href');
			
	});
	
	// Slideshow
	
	var currentPosition = 0;
	var slideWidth = 650;
	var slides = $('.slide'); // Reference object for all of the <figure.slide>s
	var numberOfSlides = slides.length;
	
	// Remove scrollbar
	$('#slidesContainer').css('overflow', 'hidden');
	
	// Insert slide wrapper
	slides.css({'float' : 'left', 'width' : slideWidth}).wrapAll('<div id="slideInner"></div>');
	
	// Set slide wrapper width
	$('#slideInner').css('width', slideWidth * numberOfSlides);
	
	// Add control spans
	$('#slideshow').prepend('<span title="See Previous Image" class="control" id="leftControl"><span>Prev</span></span>').append('<span class="control" title="See Next Image" id="rightControl"><span>Next</span></span>');
	
	// On load, set visible controls
	manageControls(currentPosition);
	
	// Event listners
	$('.control').bind('click', function () {
		
		// Prevent embarrasing errors
		if (currentPosition > -1 && currentPosition < numberOfSlides)
		{	
			// On click, determine new position
			currentPosition = ($(this).attr('id') == 'rightControl') ? currentPosition + 1 : currentPosition - 1;
		
			// If new currentPosition is a valid slide number…
			// Hide/show controls
			manageControls(currentPosition);
			
			// Adjust margin, moving slide
			$('#slideInner').animate({'marginLeft' : slideWidth * (-currentPosition)});
		}
	});
	
	// manageControls function: Hides/Shows controls based on arg (currentPosition)
	function manageControls(position)
	{
		if (position == 0)
		{
			// First slide — Hide left control
			$('#leftControl').hide();
		}
		else
		{
			// Not first slide — Show left control
			$('#leftControl').show();
		}
		
		if (position == (numberOfSlides - 1))
		{
			// last slide — Hide right control
			$('#rightControl').hide();
		}
		else
		{
			// Not last slide — Show right control
			$('#rightControl').show();
		}
	}
	
	// Fades out description when a painting is moused over
	/*$('.painting img').mouseover(function ()
		{
			$('.paintingtext').fadeTo('fast', '0.2');
		}).mouseout(function ()
		{
			$('.paintingtext').fadeTo('fast', '1');
		});*/
});
