/* jdNewsScroll 1.1 (2007-02-08)
 * Copyright (c) 2006,2007 Jonathan Sharp (http://jdsharp.us)
 */
(function($){
    var ELMS = [];
    $.fn.jdNewsScroll = function (o) {
    o = $.extend({ delay : 60, step : 2 } , o);
    $(this).each(function () {
        this.$o       = o;
        this.$pause   = false;
        this.$counter = (Math.floor(Math.random() * 10) * 10);
        $(this).hover(function () {
            $(this).jdNewsScrollPause(true)
        }
        , function () {
            $(this).jdNewsScrollPause(false)
        });
        $('> ul', this).bind('mouseover', function (e) {
            if ($(e.target).is('li')) {
                $(e.target).addClass('hover'); 
            }
        }).bind('mouseout', function (e) {
            if ($(e.target).is('li')) {
                $(e.target).removeClass('hover'); 
            }
        });
        ELMS.push(this); 
    });
    return this;
    
    };
    $.fn.jdNewsScrollPause = function (pause) {
        return this.each(function () {
            this.$pause = pause; 
        });
    }
    // Activate our 'scrolling agent'
    setInterval(scroll, 85);
    // Go through our list of elements and step each one
    function scroll() {
        for (var i = 0; i < ELMS.length; i++) {
            var elm = ELMS[i];
            if (elm && !elm.$pause) {
                if (elm.$counter == 0) {
                    var ul  = $('> ul', elm)[0];
                    if (!elm.$steps) {
                        // Set the number of steps (the height of the li element)
                        elm.$steps  = $('> li:last-child', ul).outerHeight();
                        // Reset our step which will count backwards towards $steps
                        elm.$step   = 0;
                    }
                    if ((elm.$steps + elm.$step) <= 0) {
                        elm.$counter    = elm.$o.delay;
                        elm.$steps      = false;
                        $(ul).css('top', '0').find('> li:last-child').after($('> li:first-child', ul));
                        $('> *', ul).not('li').remove();
                    } else {
                        elm.$step -= elm.$o.step;
                        if (-elm.$step > elm.$steps) {
                            elm.$step = -elm.$steps;
                        }
                        ul.style.top = elm.$step + 'px';
                    }
                } else {
                    elm.$counter--;
                }
            }
        }
    };
})(jQuery);

