﻿// Set times in milliseconds to control delay, scroll, and fade times.
var delayTime = 6000;
var scrollTime = 900;
var fadeTime = 300;

var totalSlides = 0;
var currentSlide = 1;
var contentSlides = "";

$(document).ready(function(){
  window.setInterval('autoScroll()', delayTime);

  $("#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);
  $("#slideshow-scroller").attr({scrollLeft: 0});
  updateButtons();
});

function showPreviousSlide()
{
  currentSlide--;
  updateContentHolder();
  updateButtons();
}

function showNextSlide()
{
  currentSlide++;
  updateContentHolder();
  updateButtons();
}

function updateContentHolder()
{
  var scrollAmount = 0;
  contentSlides.each(function(i){
    if(currentSlide - 1 > i) {
      scrollAmount += this.clientWidth;
    }
  });
  $("#slideshow-scroller").animate({scrollLeft: scrollAmount}, scrollTime);
}

function updateButtons()
{
  if(currentSlide < totalSlides) {
    $("#slideshow-next").show();
  } else {
    $("#slideshow-next").hide();
  }
  if(currentSlide > 1) {
    $("#slideshow-previous").show();
  } else {
    $("#slideshow-previous").hide();
  }
}

function autoScroll() {
  if(currentSlide < totalSlides) {
    currentSlide++;
    updateContentHolder();
    updateButtons(); }
  else if (currentSlide == totalSlides) {
    currentSlide = 1;
    $("#slideshow-scroller").fadeOut(fadeTime);//Control fade in and out at the end of the slide
    updateContentHolder();
    $("#slideshow-scroller").fadeIn(fadeTime); //Control fade in and out at the end of the slide

    updateButtons();
  }
}



