$(document).ready(function() {				   
	slideShow(4000);
});

function slideShow(speed) {
  
	//Set the opacity of all images to 0
	$('#slideshow a').css({opacity: 0.0});
	  
	//Get the first image and display it (set it to full opacity)
	$('#slideshow a:first').css({opacity: 1.0});
	  
	//Call the gallery function to run the slideshow
	var timer = setInterval('gallery()', speed);
	  
	//pause the slideshow on mouse over
	$('#slideshow').hover(
		function () {
			clearInterval(timer);
		},
		function () {
			timer = setInterval('gallery()', speed);
		}
	);
		
	//go to corresponding slide when nav button is clicked
	$('#ss_nav ul li a').click(
		function() {
			var photoclass = this.getAttribute('href');
			clearInterval(timer);
			$('#slideshow a').css({opacity: 0.0}).removeClass('show');
			$('#ss_nav ul li').removeClass('active');
			$('#slideshow a.' + photoclass).css({opacity: 0.0}).addClass('show').animate({opacity: 1.0}, 500);
			$('#ss_nav ul li.' + photoclass).addClass('active');
			timer = setInterval('gallery()', speed);
			return false;
		}
	);  
}

function gallery() {

	//if no IMGs have the "show" class, give it to the first image
	var current = ($('#slideshow a') ?  $('#slideshow a.show') : $('#slideshow a:first'));
	
	//if no LIs have the "active" class, give it to the first list item
	var currentNav = ($('#ss_nav ul li') ?  $('#ss_nav ul li.active') : $('#ss_nav ul li:first'));
	
	//Get next image, if it reaches the end of the slideshow, rotate it back to the first image
	var nextSlide = ((current.next().length) ? current.next() : $('#slideshow a:first'));
	
	//Ditto for nav item
	var nextNav = ((currentNav.next().length) ? currentNav.next() : $('#ss_nav ul li:first'));
			   
	//Set the fade in effect for the next image, ("show" class has higher z-index)
	nextSlide.css({opacity: 0.0}).addClass('show').animate({opacity: 1.0}, 500);
	
	//Ditto for nav item
	nextNav.addClass('active');
	
	//Hide the current image
	current.animate({opacity: 0.0}, 500).removeClass('show');
	
	//Ditto for nav item
	currentNav.removeClass('active');
	
}
