﻿var carousel = $("#carousel");
var images = carousel.find("img");
var animateDelay = 1000;
var showTime = 4000;
var imagePadding = 5;
var containerWidth = 718;

if (images.length > 0) {
    $(images[0]).show();
}

if (images.length > 1) {
    $(function() {
        setInterval(function() { moveNext(); }, showTime + animateDelay);
    });

    var currentIndex = 0;

    function moveNext() {
        var cur = getCurrentImage();
        var next = getNextImage();

        next.css("left", containerWidth + imagePadding + "px");

        cur.animate({ left: -containerWidth - imagePadding + "px" }, animateDelay, function() { cur.hide(); });
        next.show();
        next.animate({ left: 0 }, animateDelay);

        currentIndex++;
        if (currentIndex >= images.length) {
            currentIndex = 0;
        }
    }

    function getCurrentImage() {
        return $(images[currentIndex]);
    }
    function getNextImage() {
        var index = currentIndex + 1;
        if (index >= images.length) {
            index = 0;
        }
        return $(images[index]);
    }
}
