﻿var newReleaseCarousel = {
    animationId: null,
    animationDir: 'right',

    //time in miliseconds to wait before scrolling
    animationTimeout: 5000,

    //time in miliseconds for the scrolling transition
    animationSpeed: 200,


    init: function(carousel) {
        // Disable autoscrolling if the user clicks the prev or next button.
        carousel.clip.hover(function() { voucherCarousel.halt() }, function() { voucherCarousel.animate(carousel); });

        //Disable the previous button
        $('div.left_button').addClass('left-button-disabled');

        //Disable next button if only 3 items or less
        if (carousel.options.size <= 3) {
            $('div.left_button').unbind("click");
            $('div.right_button').addClass('right-button-disabled').unbind("click");
        }
        else {

            voucherCarousel.animate(carousel);

            jQuery('div.right_button').bind('click', function() {
                voucherCarousel.halt();
                carousel.nextN(3);
                voucherCarousel.animate(carousel);
                return false;
            });

            jQuery('div.left_button').bind('click', function() {
                voucherCarousel.halt();
                carousel.prevN(3);
                voucherCarousel.animate(carousel);
                return false;
            });
        }
    },

    animate: function(carousel) {
        voucherCarousel.animationId = setInterval(function() {
            if (carousel.last == carousel.options.size) { voucherCarousel.animationDir = 'left'; }
            else if (carousel.first == 1) { voucherCarousel.animationDir = 'right'; }

            if (voucherCarousel.animationDir == 'right') { carousel.next(); }
            else { carousel.prev(); }


        }, voucherCarousel.animationTimeout);
    },

    halt: function() {
        if (voucherCarousel.animationId) {
            clearInterval(voucherCarousel.animationId);
        }
    }

};

function newReleaseCarousel_itemVisibleOutCallbackAfterAnimation(carousel, item, idx, state) {

    if (carousel.last == carousel.options.size) {
        $('div.right_button').addClass('right-button-disabled').unbind("click");
    }
    else {
        $('div.right_button').removeClass('right-button-disabled').addClass('next-button').bind('click', function() {
            voucherCarousel.halt();
            carousel.nextN(3);
            voucherCarousel.animate(carousel);
            return false;
        });
    }

    if (carousel.first == 1) {
        $('div.left_button').addClass('left-button-disabled').unbind("click");
    }
    else {
        $('div.left_button').removeClass('left-button-disabled').addClass('previous-button').bind('click', function() {
            voucherCarousel.halt();
            carousel.prevN(3);
            voucherCarousel.animate(carousel);
            return false;
        });
    }

};

jQuery(document).ready(function() {

    jQuery('#newreleasecarousel').jcarousel({
        wrap: null,
        scroll: 1,
        visible: 3,
        initCallback: newReleaseCarousel.init,
        easing: 'linear',
        animation: newReleaseCarousel.animationSpeed,
        buttonPrevHTML: null,
        buttonNextHTML: null,
        itemVisibleOutCallback: { onAfterAnimation: newReleaseCarousel_itemVisibleOutCallbackAfterAnimation }
    });
});