// app settings
var initialAnimationRate = 500;
var isHoverEnabled = 0;
var isFastFlipEnabled = 0;

var animationRate = initialAnimationRate;
var totalSlides = 0;
var currentSlide = 1;
var contentSlides = "";
var keepScrolling = 0;

$(document).ready(function(){
  if (isHoverEnabled == 1){
    $("#slideshow-previous").hover(handlePreviousButton, handleMouseUp);
    $("#slideshow-next").hover(handleNextButton, handleMouseUp);
  }else{
    $("#slideshow-previous").mousedown(handlePreviousButton);
    $("#slideshow-next").mousedown(handleNextButton);
    $("#slideshow-previous").mouseup(handleMouseUp);
    $("#slideshow-next").mouseup(handleMouseUp);
  }

  refreshSlideshow();  
  $("#slideshow-scroller").attr({scrollLeft: 0});
  updateButtons();
});

function refreshSlideshow(){
  var totalWidth = 0;
  totalSlides = 0;
  contentSlides = $(".slideshow-content");
  contentSlides.each(function(i){
    totalWidth += this.clientWidth;
    totalSlides++;
  });
  $("#slideshow-holder").width(totalWidth);
  updateButtons();
}

function handlePreviousButton(e){
    if (e.preventDefault){
        e.preventDefault();
    }
    keepScrolling = 1;
    showPreviousSlide();
}

function handleNextButton(e){
    if (e.preventDefault){
        e.preventDefault();
    }
    keepScrolling = 1;
    showNextSlide();
}

function handleMouseUp(e){
    keepScrolling = 0;
    animationRate = initialAnimationRate;
    //console.log('stop' + keepScrolling + "," + currentSlide);
}

function showPreviousSlide()
{
  if (keepScrolling > 0){
    if (currentSlide > 1){
        currentSlide--;
        updateContentHolder();
        updateButtons();
        setTimeout(showPreviousSlide, animationRate);
        animationRate = Math.max(animationRate/1.2, initialAnimationRate/1.4);
        if (isFastFlipEnabled == 0){
            keepScrolling = 0;
        }
        //console.log('prev' + currentSlide);
    }
  }  
}

function showNextSlide()
{
  if (keepScrolling > 0){
    if (currentSlide < totalSlides){ // advance slides
        currentSlide++;
        updateContentHolder();
        updateButtons();
        setTimeout(showNextSlide, animationRate);
        animationRate = Math.max(animationRate/1.2, initialAnimationRate/1.4);
        if (isFastFlipEnabled == 0){
            keepScrolling = 0;
        }
        //console.log('next' + currentSlide);
    }
    if (currentSlide == totalSlides-2){ // we're *near* the last slide
        // (note: trigger on last slide doesn't work because next button turns off, hence no mouseup events)
        $('#slideshow-area').trigger('completing');
    }
  }
}

function updateContentHolder()
{
  var scrollAmount = 0;
  contentSlides.each(function(i){
    if(currentSlide - 1 > i) {
      scrollAmount += this.clientWidth;
    }
  });
  $("#slideshow-scroller").stop().animate({scrollLeft: scrollAmount}, animationRate);
}

function updateButtons()
{
  if(currentSlide < totalSlides) {
    $("#slideshow-next").show();
  } else {
    $("#slideshow-next").hide();
  }
  if(currentSlide > 1) {
    $("#slideshow-previous").show();
  } else {
    $("#slideshow-previous").hide();
  }
}

