jQuery.fn.extend({
    featurePlayer2: function() {
        if (this.data('featurePlayer2')) return;
        this.data('featurePlayer2', true);

        var TIMEOUT = 4000;
        var $featurePlayer = this;
        var $featureStories = jQuery('.storyPanel > ul > li', this);
        var $featureStoriesRandom = jQuery('.storyPanel ul.random', this);
        var $featureStoriesTabs = jQuery('.storyList > ul > li', this);
        var $featureStoriesTabsRandom = jQuery('.storyList ul.random', this);
        var $buttonNext = jQuery('.buttonContainer .next', this);
        var $buttonPrev = jQuery('.buttonContainer .prev', this);
        var timer;
        var currentIndex = 0;

        function _init() {
            $featureStoriesTabs.bind('click', function() {
                _pause();
                currentIndex = $featureStoriesTabs.index(this);
                _show(currentIndex);
            });

            $featureStoriesTabsRandom.each(function(i) {
                var $randomStories = jQuery('li', this);
                var randomIndex = Math.floor(Math.random() * $randomStories.length);
                $randomStories.eq(randomIndex).show();
                $featureStoriesRandom.eq(i).find('li').eq(randomIndex).show();
            });

            $buttonNext.bind('click', _next);
            $buttonPrev.bind('click', _prev);
            $featurePlayer.bind('mouseenter', _pause).bind('mouseleave', _play);
            _show(currentIndex);
            _play();
        }

        function _show(index) {
            $featureStories.eq(index)
                .fadeIn('slow')
                .siblings('li').fadeOut('slow');
            $featureStoriesTabs.eq(index)
                .addClass('selected')
                .siblings('li').removeClass('selected');
        }

        function _next() {
            currentIndex = (currentIndex + 1) % $featureStories.length;
            _show(currentIndex);
        }

        function _prev() {
            currentIndex = (currentIndex - 1 + $featureStories.length) % $featureStories.length;
            _show(currentIndex);
        }

        function _play() {
            clearInterval(timer);
            timer = setInterval(function() {
                _next();
            }, TIMEOUT);
        }

        function _pause() {
            clearInterval(timer);
        }

        _init();
    }
});