/**
 * jQuery - Rotator plugin file.
 */
(function($)
{
    //Rotator class
    function Rotator(settings)
    {
        this.rotatorId = null;
        this.activeItem = 0;
        this.items = null;
        this.itemLinks = null;
        this.animation = false;
        this.container = null;
        this.pane = null;
        this.prevPageNav = null;
        this.nextPageNav = null;
        this.userStopTimer = false;
        this.timerRunning = false;
        this.itemWidth = 0;
        this.settings = $.extend({}, $.fn.rotator.defaults, settings || {});

        this.LEFT = 1;
        this.RIGHT = -1;
    }

    //Rotator class methods
    $.extend(Rotator.prototype,
    {
        /**
         * init Rotator
         */
        init:function($container)
        {
            this.container = $container;
            this.rotatorId = this.container.data('rotator');

            //items
            this.items = $container.find('.slide');
            this.itemWidth = $(this.items[0]).width();

            //navigation kyes
//            this.prevPageNav = $('[data-rotator-prev-page=' + this.rotatorId +  ']');
//            this.nextPageNav = $('[data-rotator-next-page=' + this.rotatorId +  ']');
//            this.prevPageNav.click($.proxy(this.prevPage, this));
//            this.nextPageNav.click($.proxy(this.nextPage, this));

            //navigation links
            this.itemLinks = $('[data-rotator-nav=' + this.rotatorId +  '] li');
            this.itemLinks.each($.proxy(function(index, element)
                {
                    $(element).click($.proxy(function() { this.stopTimer(true); this.show(index) }, this));
                }, this)
            );

            //pane
            this.pane = $container.find('.pane');
            this.pane.width(this.items.length * this.itemWidth);

            //autorotate
            if(this.settings.autorotate)
            {
                this.startTimer();
//                $(window).scroll($.proxy(this.onWindowScroll, this));
//                $container.mouseenter($.proxy(function() { this.stopTimer(false) }, this));
//                $container.mouseleave($.proxy(this.startTimer, this));
            }
        },

        /**
         * show
         */
        show:function(index, direction)
        {
            if(this.animation) return;
            if(this.activeItem == index) return;

            direction = direction || (index > this.activeItem ? this.LEFT : this.RIGHT);
            
            if(this.items[index])
            {
                this.animation = true;

                if(direction == this.LEFT)
                {
                    //ak je pred akt. prvkom musi sa posunut aj pane, aby bol v rovnakej polohe aj po presunuti
                    if($(this.items[index]).offset().left < $(this.items[this.activeItem]).offset().left)
                    {
                        var left = this.pane.position().left;
                        this.pane.css('left', left + this.itemWidth + 'px');
                    }

                    //presunie sa za aktualny prvok
                    $(this.items[index]).insertAfter(this.items[this.activeItem]);

                    var left = this.pane.position().left;
                    var move = left - this.itemWidth;
                }
                else
                {
                    //ak je za akt. prvkom musi sa posunut aj pane, aby bol v rovnakej polohe aj po presunuti
                    if($(this.items[index]).offset().left > $(this.items[this.activeItem]).offset().left)
                    {
                        var left = this.pane.position().left;
                        this.pane.css('left', left - this.itemWidth + 'px');
                    }

                    //presunie sa pred aktualny prvok
                    $(this.items[index]).insertBefore(this.items[this.activeItem]);

                    var left = this.pane.position().left;
                    var move = left + this.itemWidth;
                }


                this.pane.animate({left: move}, this.settings.duration, $.proxy(this.animationEnd, this));

                $(this.itemLinks.get(this.activeItem)).removeClass('active');
                this.activeItem = index;
                $(this.itemLinks.get(this.activeItem)).addClass('active');
            }
        },

        /**
         * animationEnd
         */
        animationEnd: function()
        {
            this.animation = false;
        },

        /**
         * next page
         */
        nextPage: function()
        {
            var length = this.items.length;
            var index = (this.activeItem + 1) % length;
            this.show(index, this.LEFT);
        },

        /**
         * prev page
         */
        prevPage: function()
        {
            var length = this.items.length;
            var index = (this.activeItem - 1 + length) % length;
            this.show(index, this.RIGHT);
        },

        /**
         * onWindowScroll
         */
        onWindowScroll: function()
        {
            var containerTop = this.container.offset().top;
            var scrollTop = $(window).scrollTop();
            if($(window).height() + scrollTop > containerTop && this.container.height() + containerTop > scrollTop)
            {
                //timer sa pusti az ked je vo viditelnej zone obrazovky
                this.startTimer();
            }
            else
            {
                //ak nie je vidielna tak sa zastavi;
                this.stopTimer(false);
            }
        },

        /**
         * stopTimer
         */
        stopTimer: function(userStopTimer)
        {
            this.userStopTimer = userStopTimer;
            if(userStopTimer)
            {
                $(window).unbind('scroll');
            }
            if(this.timerRunning)
            {
                clearInterval(this.timer);
//                this.container.stopTime();
                this.timerRunning = false;
            }
        },

        /**
         * startTimer
         */
        startTimer: function()
        {
            if(!this.userStopTimer && !this.timerRunning)
            {
                this.timerRunning = true;
                this.timer = setInterval($.proxy(this.nextPage, this), this.settings.interval)
//                this.container.everyTime(this.settings.interval, $.proxy(this.nextPage, this));
            }
        }
    });

    $.extend($.fn,
    {
        rotator:function(settings)
        {
            return this.each(function()
            {
                var rotator = new Rotator(settings);
                rotator.init($(this));
            });
        }
    });


    //plugin defaults settings
    $.fn.rotator.defaults = {
        autorotate : true,
        interval: '5000',
        duration: '1s'
    };


})(jQuery);
