(function ($) {
    Promo = function Promo (target) {
        this.active = 0;
        var self = this;
        
        this.node = target;
        this.tab = target.find('.tabs a');
        this.content = target.find('.contents a');

        this.tab.eq(this.active).parent().addClass('active').find('img').hide();
        this.tab.eq(this.active).find('em').css({width: '100%'}).find('b').css({height: 33});
        
        this.tab.each(function (i) {
            $(this).data({i: i});
        }).click($.proxy(this._onClick, this));

        this.amount = this.content.size() - 1;
        this.width = this.content.width();

        this.content.css({width: (this.amount + 1) * this.width});

        this.content.eq(this.active).fadeIn(function () {
            self.interval();
        });
    };

    Promo.prototype = {
        /*
         * Container
         */
        node: null,

        /*
         * Switching tabs
         */
        tab: null,

        /*
         * Switching contents
         */
        content: null,

        /*
         * Active tab indicator
         */
        active: 0,

        /*
         * Active animation indicator
         */
        off: 0,

        /*
         * On tab click event
         */
        _onClick: function (e) {
            var target = $(e.currentTarget);
            if (this.off == 1 || $(e.target).parents('td.active').size() == 1) return;
            this.off = 1;

            var self = this;
            var index = target.data('i');
            
            this.tab.eq(this.active).find('em i').stop()
                .animate({width: 0}, 'fast')
                .next().animate({height: 25}, 'fast')
                .parent().fadeOut('fast', function(){
                    $(this).css({width: 0});
                });
            this.tab.eq(this.active).parent().removeClass('active').find('img').fadeIn('fast');
            
            this.tab.eq(index).parent().addClass('active').find('img').fadeOut('fast');
            this.tab.eq(index).find('em').animate({width: '100%'}).find('b').animate({height: 33});

            this.content.eq(index).insertAfter(this.content.eq(this.active));
            this.content.eq(this.active).animate({left: -this.width}, 555);
            this.content.eq(index).show()
                .css({left: this.width})
                .animate({left: 0}, 500, function () {
                    self.content.eq(self.active).hide();
                    self.active = index;
                    self.interval();
                });
        },

        /*
         * Interval setter
         */
        interval: function () {
            this.off = 0;
            this.tab.eq(this.active).find('em i').animate({width: '100%'}, 5000);
            this.slide = window.clearInterval(this.slide);
            this.slide = window.setInterval($.proxy(this._onSlide, this), 5000);
        },

        /*
         * On interval timeout
         */
        _onSlide: function () {
            var index = this.active + 1;
            if (index > this.tab.size() - 1) index = 0;
            this.tab.eq(index).trigger('click');
        }
    };

    $.fn.promo = function () {
        $(this).each(function () {
            var node = $(this);
            node.data('promo', new Promo(node));
        });

        return $(this);
    };
})(jQuery);
