$(function() {

	var images = [];
	
	var navigation = [];
	
	var current = 0;
	
	var timeout;
		
	$('.slideshow .slideshowImages img').each(function(index, item) {
		images.push(item);
	});
	
	$('.slideshow .slideshowNavigation a').each(function(index, item) {
		$(item).click(function() {
			showImage(index);		
			clearTimeout(timeout);			
			timeout = setTimeout(nextImage, 10000);
			return false;						
		});
	});
	
	function showImage(id) {
		clearTimeout(timeout);
		
		$('.slideshow .slideshowNavigation a span.selected').each(function(index, item) {
			if (index != id) $(item).fadeOut(250);
			if (index == id) $(item).removeClass('hidden').css('display', 'none').fadeIn(250);	
		});
				
		$(images).each(function(index, item) {
			if (index != id) $(item).fadeOut(250);
			if (index == id) $(item).removeClass('hidden').css('display', 'none').fadeIn(250);		
		});
		
		current = id;
		
		timeout = setTimeout(nextImage, 5000);		
	}
	
	function nextImage() {		
	
		current++;
		
		if (current == images.length) current = 0;
	
		showImage(current);
	}
	
	timeout = setTimeout(nextImage, 5000); 			
});
