﻿
var totalSlides = 0;
var currentSlide = 1;
var contentSlides = "";

var moveForward = true;

$(document).ready(function () {

    var totalWidth = 0;
    contentSlides = $(".slideshow-content");
    contentSlides.each(function (i) {
        totalWidth += this.clientWidth;
        totalSlides++;
    });
    $("#homeslideshow-holder").width(totalWidth);
    $("#homeslideshow-scroller").attr({ scrollLeft: 0 });
           
    setInterval(function () { runSlideShow() }, 6000);  

});

 
function runSlideShow() {
    
    //  moveForward
    if (currentSlide == totalSlides) {
        currentSlide = totalSlides--;
        moveForward = false;
    }
    else if (currentSlide == 1) {
        moveForward = true;
        currentSlide++
    }
    else
    {
        if(moveForward)
            currentSlide++
        else
            currentSlide--;
    }        
       
    $('#photoControl' + currentSlide).click();
   
}


function showSlide(position) {
  
    currentSlide = position;
    updateContentHolder();
    updateButtons(position);
}

function updateContentHolder() {
    var scrollAmount = 0;
    var contentWidth = 0;
    contentSlides.each(function (i) {
        if (currentSlide - 1 > i) {
            scrollAmount += this.clientWidth;
        }
        contentWidth += this.clientWidth;
    });

    $("#homeslideshow-holder").width(contentWidth);
    $("#homeslideshow-scroller").animate({ scrollLeft: scrollAmount }, 800);
}

function updateButtons(position) {

    galleryControls = $(".gallerycontrol");
    galleryControls.each(function (i) {

        if (i == (position-1)) {
            $(this).attr("src", "/Content/Images/Selected2.jpg");
        }
        else {
            $(this).attr("src", "/Content/Images/Unselected2.jpg");
        }
    });
}


