var totalSlides = 0;
var currentSlide = 1;
var contentSlides = "";
var prevSlide = 1;
var t = 0;

auto_on();

$(document).ready(function(){
  
 // $.preloadCssImages(); //breaks the thing

  $("#slideshow-previous").click(showPreviousSlide);
  $("#slideshow-next").click(showNextSlide);
  
  var totalWidth = 0;
  contentSlides = $(".slideshow-content");
  contentSlides.each(function(i){
    totalWidth += this.clientWidth;
    totalSlides++;
  });
  $("#slideshow-holder").width(totalWidth);
  
  
});

function showPreviousSlide()
{
	auto_off();
	prevSlide = currentSlide;
	
	if (currentSlide == 1) {
		currentSlide = totalSlides;
	} else {
		currentSlide--;
	}
  	
  	updateContentHolder();
	updateButtons();
	auto_on();
}

function showNextSlide()
{
	auto_off();
	prevSlide = currentSlide;
	
	if (currentSlide == totalSlides) {
		currentSlide = 1;
	} else {
		currentSlide++;
	}

	updateContentHolder();
	updateButtons();
	auto_on();
}

function updateContentHolder()
{
	$("#hero-slider-" + prevSlide).fadeOut('slow');
	$("#hero-slider-" + currentSlide).fadeIn('slow');

/*
  var scrollAmount = 0;
  contentSlides.each(function(i){
    if(currentSlide - 1 > i) {
      scrollAmount += this.clientWidth;
    }
  });
  $("#slideshow-scroller").animate({scrollLeft: scrollAmount}, 1000);
*/

}

function updateButtons()
{
	$("#slideshow-next").show();
	$("#slideshow-previous").show();
	/*
  if(currentSlide < totalSlides) {
    $("#slideshow-next").show();
  } else {
    $("#slideshow-next").hide();
  }
  if(currentSlide > 1) {
    $("#slideshow-previous").show();
  } else {
    $("#slideshow-previous").hide();
  }
  */
}

function auto_on()
{
	t = setInterval("showNextSlide()",6500);
}
function auto_off()
{
	clearInterval(t);
}

