var delay 	= 8; // # of seconds
var timerID	= 0;
var current = 0;
var slides 	= new Array();

$(document).ready(function() {
	init();
	
	$('#hightslide-tabs li a').focus(function() {
		$(this).blur();
	});
	
	$('#hightslide-tabs li a').click(function(event) {
		event.preventDefault();
		changeSlide( this.id.substr(4) - 1 );
	});
});

function changeSlide(id) {
	if ( id == '' || id == undefined ) {
		var old		= current;
		current 	= (current == 3) ? 0 : current + 1;
		var title	= slides[current]['title'];
		var blurb	= slides[current]['blurb'];
		var pic		= slides[current]['pic'];
		var a 		= document.createElement('a');
		
		$(a).attr('href', slides[current]['link']);
		$(a).append(pic);
		
		$('#switch-img-holder').fadeOut(500, function() {
			$(this).empty().html(a).fadeIn(500, function() {
				b = $(a).clone().empty().append('<strong>'+title+'</strong> - '+blurb);
				
				$('div.hightslide-info div').html(b);
				$('#hightslide-tabs li:eq('+old+')').removeClass('active');
				$('#hightslide-tabs li:eq('+current+')').addClass('active');
			});
		});
	} else {
		$('#hightslide-tabs li').each(function() {
			$(this).removeClass('active');
		});
		
		var old		= (id == 0) ? 3 : id - 1;
		current 	= id;
		
		$('#hightslide-tabs li:eq('+old+')').removeClass('active');
		$('#hightslide-tabs li:eq('+current+')').addClass('active');
		
		var title	= slides[current]['title'];
		var blurb	= slides[current]['blurb'];
		var pic		= slides[current]['pic'];
		var a 		= document.createElement('a');
		
		$(a).attr('href', slides[current]['link']);
		$(a).append(pic);
		
		$('#switch-img-holder').fadeOut(500, function() {
			$(this).empty().html(a).fadeIn(500, function() {
				b = $(a).clone().empty().append('<strong>'+title+'</strong> - '+blurb);
				$('div.hightslide-info div').html(b);
			});
		});
	}

	resetTimer();
}

function init() {
	$('#hightslide-tabs li:eq(0)').toggleClass('active');
	
	$('#hightslide-tabs li a').each(function(i,n) {
		var slide		= new Object();
		var pic 		= new Image(646, 248);
		
		pic.src 		= $(n).attr('rev');
		pic.alt 		= $(n).attr('title');
		
		slide['title'] 	= $(n).attr('title');
		slide['blurb'] 	= $(n).attr('rel');
		slide['pic']	= pic;
		slide['link']	= $(n).attr('href');

		slides.push(slide);
	});
	
	initTimer();
}

function initTimer() {
	timerID	= setTimeout('changeSlide()', delay * 1000);
}

function resetTimer() {
	clearTimeout( timerID );
    initTimer();
}