var slideInterval= null;
var slideTimeout = null;
var $tabsetLinks = null;
var $firstTabsetLink = null;
var $tabs = null;

function slideSwitch() {
	var $active = $("#tabs li.active");
	
	if ( $active.length == 0 ) $active = $('#tabs li:last');
	var $next =  $active.next().length ? $active.next() : $('#tabs li:first');
	
	$active.addClass('last-active');
	$active.animate({opacity: 0}, 1000);
	$next.css({opacity: 0.0})
	.addClass('active')
	.animate({opacity: 1.0}, 1000, function() {
		$active.removeClass('active last-active');
		var index = $tabs.index(this);
		var $thisTabsetLink = $("#tabset li a:eq(" + index + ")");
		
		$tabsetLinks.css("borderTop", "2px solid #fff");
		$tabsetLinks.removeClass("active");
		$thisTabsetLink.css("borderTop", "2px solid " + $thisTabsetLink.parent().css("backgroundColor"));
		$thisTabsetLink.addClass("active");	
	});
}

function startSlideshow() {
	clearInterval(slideInterval);
	slideInterval = setInterval("slideSwitch()", 9000 );	
}

$(function() {
	$tabsetLinks = $("#tabset li a");
	$firstTabsetLink = $("#tabset li a:eq(0)");
	$tabs = $("#tabs li");
	
	$firstTabsetLink.css("borderTop", "2px solid " + $firstTabsetLink.parent().css("backgroundColor"));
	
	if ($tabsetLinks.length > 1) {
		startSlideshow();
	
		$("#tabset a").click(function() {
			clearTimeout(slideTimeout);
			clearInterval(slideInterval);
			slideTimeout = setTimeout("startSlideshow()", 9000);					  
		});
		
		$("#tabset li a").click(function() {
			$(this).blur();
			var index = $tabsetLinks.index($(this));
			
			$tabsetLinks.css("borderTop", "2px solid #fff");
			$tabsetLinks.removeClass("active");
			
			$(this).css("borderTop", "2px solid " + $(this).parent().css("backgroundColor"));
			$(this).addClass("active");
			
			$tabs.removeClass("active");	
			$("#tabs li:eq(" + index + ")").addClass("active").css({opacity: 1.0});		
		});
	}
});
