
var curr_slide = 0;
var next_slide = 1;
var num_slides = 0;
var wait = 8000;
var animation;

jQuery(document).ready(function() {
	
	num_slides = jQuery("#brandbox ul#slides li.slide").length;
	
	// initial set up
	jQuery("#brandbox ul#slides li.slide").fadeOut(0);
	jQuery("#brandbox ul#slides li.slide:eq(0)").fadeIn(0);
	cufonBrandbox();	
	
	// hook pagination buttons
	jQuery("#brandbox #thumbs .thumb a").click(function(e) {
		e.preventDefault();
		var pageid = jQuery(this).attr("id");
		pageid = parseInt(pageid.substr(5));
		goToSlide(pageid - 1, true);
	});
	
	animation = setInterval(function() {
			goToSlide(curr_slide + 1, false);
		}, wait);

});

function goToSlide(next_slide, clicked) {

	if (next_slide >= num_slides) {
		next_slide = 0; // go back to the beginning
	}
	
	// okay, so fade out the current slide
	jQuery("#brandbox ul#slides li.slide:eq(" + curr_slide.toString() + ")").fadeOut(125);
	
	// now fade in the next slide
	jQuery("#brandbox ul#slides li.slide:eq(" + next_slide.toString() + ")").fadeIn(125);

	// update pagination buttons
	// remove "current" from the page we're leaving
	jQuery("#brandbox #thumbs .thumb a").parent().removeClass('current');
	// add "current" to page we're going to
	jQuery("#brandbox #thumbs .thumb a#page-" + (next_slide+1).toString()).parent().addClass('current');
	
	if (clicked) {
		clearInterval(animation);
		animation = setInterval(function() {
				goToSlide(curr_slide + 1, false);
			}, wait * 2);		
	} else {
		clearInterval(animation);
		animation = setInterval(function() {
				goToSlide(curr_slide + 1, false);
			}, wait);
	}

//	cufonBrandbox();

	curr_slide = next_slide;
	
}
