(function($){
    $.storyBox = function(el, options){
        // To avoid scope issues, use 'base' instead of 'this'
        // to reference this class from internal events and functions.
        var base = this;

        // Access to jQuery and DOM versions of element
        base.$el = $(el);
        base.el = el;

        // Add a reverse reference to the DOM object
        base.$el.data("storyBox", base);

        var storiesCount = base.$el.find('tr').length;

        var currentStory = 0;

        var $stories = base.$el.find('tr.storyBox');

        var timer;

        var $current;


        base.init = function(){
            base.options = $.extend({},$.storyBox.defaultOptions, options);

            base.$el.find('tr.storyBox').css('display', 'block').hide().click(function() {
                showNext();
            });
            $current = base.$el.find('tr.storyBox:first').show();

            timer = timer = setTimeout(function () {
                showNext();
            }, 7000);
        };

        //$stories.show();

        switchDiv = function() {
            showNext($(this));
        }

        function showNext() {
            clearTimeout(timer);
            base.$el.find('tr.storyBox:visible').fadeOut(500, function() {

                $next = $current.next();

                if ($next.length == 0) {
                    $next = base.$el.find('tr.storyBox:first');
                }

                $next.fadeIn(500, function() {
                    $current = $next;
                    timer = setTimeout(function () {
                        showNext();
                    }, 7000);
                });
            });
        }

        // Sample Function, Uncomment to use
        // base.functionName = function(paramaters){
        // };

        // Run initializer
        base.init();

    };

    $.storyBox.defaultOptions = {
    };

    $.fn.storyBox = function(options){
        return this.each(function(){
            (new $.storyBox(this, options));
        });
    };

})(jQuery);
