// The timer. We'll reset this if needed.
var timer;

function show_hide(forward) {

	// Get all the items in the spinner.
	var all_items = $('.slideshow-item');

	// Initialise the next item to show.
	var to_show = false;
	
	// Loop through all the items, find the visible one.
	all_items.each(function(i) {
		if ($(this).is(':visible')) {
			
			// Hide the currently visible one.
			$(this).hide();
			
			if (forward) {
				// If we're going forward, get the next item, and adjust for the number of items.
				to_show = i + 1
				if (to_show > (all_items.length - 1)) { to_show = 0; }
			} else {
				// If we're going back, get the previous one, and adjust for going past zero.
				to_show = i - 1
				if (to_show < 0) { to_show = all_items.length - 1; }
			}
		}
	});

	// Show the new selected item.
	all_items.each(function(i) {
		if (i == to_show) {
			
			$('.thumb').css('border-color', 'white');
			$('.thumb' + i).css('border-color', '#bbb');
			
			$(this).show();
		}	
	});	
}

function switch_item(first)
{
	if (!first) {
		show_hide(true);
	}
	
	timer = setTimeout("switch_item(false)", 8000);
}

$(document).ready(function() {

	// Show the first item.
	$('.slideshow-item:first').show();
	$('.thumb0').css('border-color', '#bbb');
	
		
	// Hook up the buttons.
	$('.next').click(function() {
		clearTimeout(timer);
		switch_item(true);
		show_hide(true);
	});	
	
	$('.prev').click(function() {
		clearTimeout(timer);
		switch_item(true);
		show_hide(false);
	});
		
	switch_item(true);

	// function doTimer()
	// {
	// if (!timer_is_on)
	//   {
	//   timer_is_on=1;
	//   timedCount();
	//   }
	// }

});

