﻿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++;
    });
    $("#slideshow-holder").width(totalWidth);
    $("#slideshow-scroller").attr({ scrollLeft: 0 });

    setInterval(function () { runSlideShow() }, 3000);

});




function runSlideShow() {

    //  moveForward
    if (currentSlide == totalSlides) {
        currentSlide = totalSlides - 1;
        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;

    });

    $("#slideshow-holder").width(contentWidth);
    $("#slideshow-scroller").animate({ scrollLeft: scrollAmount }, 800);
}

function updateButtons(position) {

    galleryControls = $(".gallerycontrol");
    galleryControls.each(function (i) {

        if (i == (position - 1)) {
            $(this).attr("src", "/Content/Images/portfolio-gallery-icon-active.gif");
        }
        else {
            $(this).attr("src", "/Content/Images/portfolio-gallery-icon.gif");
        }
    });

}

