﻿var intTransitionTimeS = 0.15;
var intStartImageTimesS = 3;
var intDisplayTimeS = 4;
var intUserActionPauseTime = 4;

var timer;


$(document).ready(function () {
    timer = window.setTimeout('StartSlideShow()', intStartImageTimesS * 1000);

    $('.SlideShowImagePosition').click(function () {
        var intIndex = $(this).index();
        var divSlider = $(".SlideShowImages");
        if (divSlider.is(":animated") == false) {
            clearTimeout(timer);
            ShowImage(intIndex, true);
        }
    });
});

function StartSlideShow() {
    clearTimeout(timer);
    ShowImage(0, false);
}

function ShowNextImage() {
    var intIndex = GetImageIndex();
    var intMaxIndex = ($(".SlideShowImages").width() - GetImageWidth()) / GetImageWidth();

    if (intIndex == intMaxIndex - 1) {
        ShowImage(0, false);
    }
    else {
        ShowImage(intIndex + 1, false);
    }
}

/*
* Slides the div tag right or left according to the intIndex
*
* @param intIndex desired image index
*/
function ShowImage(intIndex, userRequested) {
    if (intIndex != GetImageIndex()) {
        if (intIndex > GetImageIndex()) {
            SlideRight(intIndex, userRequested);
        }
        else {
            SlideLeft(intIndex, userRequested);
        }
    }
}


/*
* Slides the div tag left by one image
*
* @param intIndex desired image index
*/
function SlideLeft(intIndex, userRequested) {
    if (GetImageIndex() > 0) {
        var divSlider = $(".SlideShowImages");
        var strLeft = '+=' + GetImageWidth() + 'px';
        divSlider.animate({ left: strLeft }, intTransitionTimeS * 1000, 'swing', function () { SetActiveSelector(intIndex, userRequested); });
    }
}


/*
* Slides the div tag right by one image
*
* @param intIndex desired image index
*/
function SlideRight(intIndex, userRequested) {
    var divSlider = $(".SlideShowImages");
    var intSliderWidth = parseInt(divSlider.width());
    var intSliderLeft = parseInt(divSlider.css("left").replace("px", ""));

    var intImageWidth = GetImageWidth();
    if (intSliderWidth + intSliderLeft > 0) {
        var strLeft = '-=' + GetImageWidth() + 'px';
        divSlider.animate({ left: strLeft }, intTransitionTimeS * 1000, 'swing', function () { SetActiveSelector(intIndex, userRequested); });
    }
}


/*
* Sets the correct light above the image and slides to the next image if necessary
*
* @param intIndex desired image index
*/
function SetActiveSelector(intIndex, userRequested) {
    $(".SlideShowImagePositionActive").attr("class", "SlideShowImagePosition");
    $(".SlideShowImagePosition").eq(GetImageIndex()).attr("class", "SlideShowImagePositionActive");

    if (GetImageIndex() != intIndex) {
        ShowImage(intIndex, userRequested);
    }
    else {
        if (userRequested == true) {
            timer = window.setTimeout('ShowNextImage()', intUserActionPauseTime * 1000);
        }
        else {
            timer = window.setTimeout('ShowNextImage()', intDisplayTimeS * 1000);
        }
    }
}


/*
* Returns the active image index
*
* @return the active image index
*/
function GetImageIndex() {
    var intImageWidth = GetImageWidth();
    var divSlider = $(".SlideShowImages");

    var intSliderLeft = parseInt(divSlider.css("left").replace("px", ""));
    var intImageIndex = Math.abs(intSliderLeft) / intImageWidth;

    return intImageIndex - 1;
}

/*
* Returns the image width of a standard portfolio image
*
* @return the image width
*/
function GetImageWidth() {
    return $(".SlideShowImage:first").width();
}
