//array to store IDs of our tabs
var slides = [];

//index for array
var indx = 0;

//store setInterval reference
var interv;

//change tab and highlight current tab title
function change(stringref){
	//hide the other tabs
	$('.slider-content:not(#' + stringref + ')').hide();
	
	//show proper tab, catch IE6 bug
	if (($.browser.msie && $.browser.version.substr(0, 3) == "6.0") || 
		($.browser.msie && $.browser.version.substr(0, 3) == "7.0")) {
		$('.slider-content#' + stringref).show();
	} else {
		$('.slider-content#' + stringref + '.slider-image').show();
		$('.slider-content#' + stringref).fadeIn();
	}
		
	//clear highlight from previous tab title
	$('.slider-control a:not(#' + stringref + ')').removeClass('selected');
	
	//highlight currenttab title
	$('.slider-control a[href=#' + stringref + ']').addClass('selected');
}

function next(){
	//call change to display next tab
	change(slides[indx++]);
	
	//if it's the last tab, clear the index
	if(indx >= slides.length)
		indx = 0;
}
$(document).ready(function(){
	//store all tabs in array
	$(".slider-content").map(function(){
		slides[indx++] = $(this).attr("id");
    })
	
	//set index to next element to fade
	indx = 0;
	
	//initialize tabs, display the current tab
	$(".slider-caption").hide();
	$(".slider-content:not(:first)").hide();
	
	$(".slider-content:first").show();
	$(".slider-caption").show();
	
	//highlight the current tab title
	$('.slider-control a[href=#1]').addClass('selected');
	
	
	//handler for clicking on tabs
	$(".slider-control a").click(function(){
		
		//if tab is clicked, stop rotating 
		clearInterval(interv);
		
		//store reference to clicked tab
		stringref = $(this).attr("href").split('#')[1];
		
		//display referenced tab
		change(stringref);
		
		return false;
	});
	
	//start rotating tabs
	if(slides.length > 1)
		interv = setInterval("next()", 5000);
});
