(function($) {
    $.extend({
        myScroller: new function() {
            this.defaults = {
                width: 600,
                height: 20,
                direction: 'vertical',
                currentItem: 0,
                speed: 1000,
                interval: 2000,
                animate_if_only_one: 1
            };

            function buildCache(list){
                var l = {items: []};
                $(list).children().each(function(i){
                    var left = list.config.direction == 'vertical' ? 0 : list.config.width * i;
                    var top = list.config.direction == 'vertical' ? list.config.height * i : 0;
                    $(this).css({width: list.config.width, height: list.config.height, position: 'relative', left: 0, top: 0, display: 'block', 'float': list.config.direction == 'vertical' ? 'none' : 'left'});
                    l.items.push(this);
                })
                return l;
            }

            function doAnimate(list){
                var cache = list.config.cache;                
                var nextItem = $(list).children(':eq(1)');
                var currentItem = $(list).children(':eq(0)');
                var copy = currentItem.clone();
                if(list.config.direction == 'vertical'){
                    currentItem.animate({top: -list.config.height, height: 0}, list.config.speed);
                   // nextItem.animate({top: -list.config.height}, list.config.speed);
                } else {
                    if(list.config.direction == 'horizontal-left' || list.config.direction == 'horizontal')
                        currentItem.animate({left: -list.config.width, width: 0}, list.config.speed);
                    if(list.config.direction == 'horizontal-right')
                        currentItem.animate({left: list.config.width, width: 0}, list.config.speed);
                   // nextItem.animate({left: -list.config.width}, list.config.speed);
                }
                setTimeout(function(){currentItem.hide();currentItem.remove()}, list.config.speed-200);
                copy.appendTo(list);
            }

            function start(list){
                list.interval = setInterval(function(){doAnimate(list)}, list.config.interval);
            }

            function stop(list){
                clearInterval(list.interval)
            }

            this.construct = function(settings) {

                return this.each(function() {
                    this.config = {};
                    var config, $this, cache;
                    config = $.extend(this.config, $.myScroller.defaults, settings);

                    cache = buildCache(this);
                    config.cache = cache;
                    $this = this;

                    if(cache.items.length > 1 || config.animate_if_only_one){
                        start($this);
                    
                        $(this).mouseover(function(){stop($this)}).mouseout(function(){start($this)})
                    }

                });
            };

        }
    });

    // extend plugin scope
    $.fn.extend({
        myScroller: $.myScroller.construct
    });

})(jQuery);
