/**
 * Unstopable cycle carousel
 *
 * maded by Anton Zaycev
 * please visit www.a2design.biz
 */

(function ($) {                  // Compliant with jquery.noConflict()
    $.fn.a2carousel = function(o) {
        o = $.extend({
            speed: 300
        }, o || {});

        return this.each(function() {
            var container = $(this);
            var carousel = container.find('ul');
            var total = 0;

            carousel.find('li').each(function () {
                var _this = $(this,true);
                total += _this.outerWidth(true);
            });

            var time = total*2000/o.speed;
            
            carousel.width(total);

            function animate() {
                if (window.stopCarousel == 1) {return false;}
                window.carouselAnimate = carousel.animate({left: -total/2},time,'linear',goToStart);
            }

            function goToStart() {
                    carousel.css({left: 0});
                    animate();
            }

            // Alt + M stop the carousel
            $(document).keyup(function(e){
                if (e.altKey) {
                    if (e.keyCode == 77) {
                        window.carouselAnimate.stop();
                    }
                }
            })

            $(window).load(function () {
                animate(); // starting carousel
            });

        });

    }
})(jQuery);

