/**
 * User: tleo
 * E-mail: tleo.tlsoft@gmail.com
 * Date: 21.12.11
 * Time: 20:56
 */

(function ($) {
    $.fn.tlCarousel = function (options) {
        var o = {
            speed:5,
            auto:100,
            btnNext:null,
            btnPrev:null
        };

        return this.each(function () {
            if (options) {
                $.extend(o, options);
            }

            var $this = $(this);

            var $carousel = $this.children('ul:first');
            var itemsTotal = $carousel.children().length;
            var running = false;
            var direction = 1;
            var intID = null;

            var nowSpeed = o.speed;

            $this.css({
                'position':'relative',
                'overflow':'hidden'
            });

            var maxWidth = 0;

            $carousel.children().each(function () {
                maxWidth += $(this).width() + 20;
            });


            $carousel.css({
                'position':'relative',
                'width':maxWidth * 3 + 'px',
                'left':maxWidth * -1 + 'px'
            });

            $($carousel).prepend($($carousel).children('li').clone()).append($($carousel).children('li').clone());

            function slide() {
                if (!running) {
                    running = true;

                    if (intID) {
                        window.clearInterval(intID);
                    }

                    $carousel.animate({
                        left:'+=' + nowSpeed * direction
                    }, o.auto, 'linear', function () {
                        var leftIntent = parseFloat($carousel.css('left').substr(0, $carousel.css('left').length - 2));

                        if (leftIntent > maxWidth * -1)
                            $carousel.css('left', (leftIntent - maxWidth) + 'px');
                        if (leftIntent < maxWidth * -2) {
                            $carousel.css('left', (leftIntent + maxWidth) + 'px');
                        }

                        running = false;
                        slide();
                    });

                }

                return false;
            }

            var oldDir;

            $($carousel).hover(function () {
                    oldDir = direction;
                    direction = 0;
                },
                function () {
                    direction = oldDir;
                }
            );

            $(o.btnNext).hover(function () {
                    direction = -1;
                    nowSpeed = o.speed * 2;
                },
                function () {
                    nowSpeed = o.speed;
                }
            );

            $(o.btnNext).bind('mousedown', function () {
                nowSpeed = o.speed * 4;
            });

            $(o.btnNext).bind('mouseup', function () {
                nowSpeed = o.speed * 2;
            });

            $(o.btnPrev).hover(function () {
                    direction = 1;
                    nowSpeed = o.speed * 2;
                },
                function () {
                    nowSpeed = o.speed;
                }
            );

            $(o.btnPrev).bind('mousedown', function () {
                nowSpeed = o.speed * 4;
            });

            $(o.btnPrev).bind('mouseup', function () {
                nowSpeed = o.speed * 2;
            });

            intID = window.setInterval(function () {
                slide()
            }, o.auto);
        });
    }
})(jQuery);
