var InfiniteRotator =
{
    infiniteLoop: null,
    init: function () {
        if (typeof (infiniteLoop) != "undefined") {
            clearInterval(infiniteLoop);
        }
        
        var blocks = $('.fader > :visible .rotating-block');
        //initial fade-in time (in milliseconds)
        var initialFadeIn = 1000;

        //interval between items (in milliseconds)
        var itemInterval = 5000;

        //cross-fade time (in milliseconds)
        var fadeTime = 2500;

        //count number of items
        var numberOfItems = blocks.length;

        //set current item
        var currentItem = 0;

        //show first item
        blocks.eq(currentItem).fadeIn(initialFadeIn);

        //loop through the items
        infiniteLoop = setInterval(function () {
            blocks.eq(currentItem).fadeOut(fadeTime);

            if (currentItem == numberOfItems - 1) {
                currentItem = 0;
            } else {
                currentItem++;
            }
            blocks.eq(currentItem).fadeIn(fadeTime);

        }, itemInterval);
    }
};

var getCarouselWidth = function () {
    var iDevice = (typeof (window.orientation) != "undefined");

    var winWidth = $(window).width();

    var carouselWidth = winWidth;
    if (iDevice) {
        if (winWidth > 960) {
            carouselWidth = 864;
        } else if (winWidth > 768) {
            carouselWidth = 600;
        } else {
            carouselWidth = winWidth;
        }
    } else {
        if (winWidth > 630) {
            carouselWidth = 960;
        } else {
            carouselWidth = winWidth;
        }
    }
    return carouselWidth;
};

var resizeFrames = function () {
    /*	CarouFredSel: an infinite, circular jQuery carousel.
    Configuration created by the "Configuration Robot"
    at caroufredsel.frebsite.nl
    */
    

    var beforeHandler = function (oldItems, newItems, newSizes) {
        var newId = parseInt(newItems[0].id.replace("slide_", ""));
        setBG(newId - 1);
    };

    $("#projector ul.content").carouFredSel({
        width: getCarouselWidth(),
        items: {
            visible: 1
        },
        scroll: {
            pauseOnHover: false,
            wipe: (getCarouselWidth() > 750),
            onBefore: beforeHandler
        },
        auto: {
            play: false,
            pauseDuration: 10000,
            delay: 5000,
            onBefore: beforeHandler
        },
        next: {
            button: ".btnNext"
        },
        prev: {
            button: "#prev"
        }
    });

    $(".caroufredsel_wrapper").css("margin", "1.5em auto 3em");

};

var updateWidth = function () {
    $("#projector ul.content").trigger("configuration", ["width", getCarouselWidth()]);
    $(".caroufredsel_wrapper").css("margin", "1.5em auto 3em");
};

$(function () {
    $(window).bind('resize', function () {
        window.setTimeout(updateWidth, 100);
        InfiniteRotator.init();
    });

    resizeFrames();
    InfiniteRotator.init();
});
