/*
Mini vertical scroller
2008 - Magic Wand
*/

MiniVerticalScroller = function(selector, height, delay, speed) {
    this.selector = selector;
    this.delay = delay;
    this.speed = speed;
    this.height = height;
    this.pos = 0;
    
    this.init();
}

MiniVerticalScroller.prototype = {
    init: function() {
        if(this.getLength() > 1)
            this.startTimer();
    },
    
    startTimer: function() {
        var _this = this;
        setTimeout(function() { _this.onTimeout(); }, this.delay);
    },
    
    onTimeout: function() {
        if(this.pos >= this.getLength() - 1)
            this.doRewind();
        else
            this.doScroll();
    },
    
    getLength: function() {
        return $('.news_item', this.getEl()).length;
    },
    
    doScroll: function() {
        this.pos ++;
        var _this = this;
        this.getEl().animate({
            top: '-=' + this.height
        }, this.speed, 'swing', function() { _this.startTimer(); });
    },
    
    doRewind: function() {
        var _this = this;
        this.pos = 0;
        this.getEl().fadeOut(this.speed, function() {
            _this.getEl()
                 .css('top', 0)
                 .fadeIn(_this.speed, function() { _this.startTimer(); });
        });
    },
    
    getEl: function() {
        return $(this.selector);
    }
}

