var slideInterval = 5000; // in miliseconds
var nFirstSlide = -1;
var nCurrentSlide = -1;
var sButtonContent = "";
var itSlideShow = 0;

$(document).ready(function(){
	// build the pip buttons
	$('.slide').each(
	function()
	{	
		// set the first slide
		if (nFirstSlide<0) {
			nFirstSlide = parseInt($(this).attr('id').replace(/[^0-9]/g,''));
			nCurrentSlide = nFirstSlide;
			
			// starts the auto slideshow interval
			showSlide(false);
		}
	});
	
	// bind the tabs to have onclick function
	$('.slidetab').bind('click', onClickSlide);
});

// auto slide show handler
var autoSlide = function() {
	// move to next slide, if the slide not exists, startover with first slide
	nCurrentSlide++;
	if (!$('#slide_'+parseInt(nCurrentSlide)).get("0")) nCurrentSlide = nFirstSlide;
	
	showSlide(false);
}

// user clicks on the buttons
var onClickSlide = function() {
	nCurrentSlide = parseInt($(this).attr('id').replace(/[^0-9]/g,''));
	showSlide(true);
}

// display the current slide
var showSlide = function(userclick) {
	clearTimeout(itSlideShow);
	
	thisSlideID = 'slide_'+parseInt(nCurrentSlide);
	
	$('.slide').hide(); // hide all the slides
	$('.slidetab').removeClass("tabon"); // hide all the slides
	
	if ($('#slide_'+parseInt(nCurrentSlide)).html()=='' && aRotate.length>=parseInt(nCurrentSlide)) {
		a = '<div class="slidecontent" style="overflow: hidden;">';
		a += '<a href="'+aRotate[parseInt(nCurrentSlide)-1].URL+'"><img src="SysImages/'+aRotate[parseInt(nCurrentSlide)-1].SRC+'" width="750" height="250" alt="'+aRotate[parseInt(nCurrentSlide)-1].ALT+'"></a>';
		a += '</div>';
		$('#slide_'+parseInt(nCurrentSlide)).html(a);
	}
	
	$('#slide_'+parseInt(nCurrentSlide)).show(); // show the current slide
	$('#slideLink_'+parseInt(nCurrentSlide)).addClass("tabon"); // show the current slide
	
	if (!userclick) itSlideShow = setTimeout(autoSlide, slideInterval);
}
