(function($){
    $.fn.newsRoller = function(options){
        var container = this;
        var roller = {
            element: container
        };
        
        roller._init = function(){
            this.element.attr('id', opts.id);
            if ($('.headline', roller.element).size() > 1) {
            var currentHeadline = 0, oldHeadline = 0;
            var hiddenPosition =this.element.height() + 10;
            $('.headline', this.element).eq(currentHeadline).css('top', 0);
            var headlineCount = $('.headline', this.element).length;
            var pause;
            var rotateInProgress = false;
            var headlineRotate = function(){
                if (!rotateInProgress) {
                    rotateInProgress = true;
                    pause = false;
                    currentHeadline = (oldHeadline + 1) %
                    headlineCount;
                    $('.headline', this.element).eq(oldHeadline).animate({
                        top: -hiddenPosition
                    }, 'slow', function(){
                        $(this).css('top', hiddenPosition);
                    });
                    $('.headline', this.element).eq(currentHeadline).animate({
                        top: 0
                    }, 'slow', function(){
                        rotateInProgress = false;
                        if (!pause) {
                            pause = setTimeout(headlineRotate, 8000);
                        }
                    });
                    oldHeadline = currentHeadline;
                }
            };
            if (!pause && $('.headline').length > 1) {
                pause = setTimeout(headlineRotate, 8000);
            }
            this.element.hover(function(){
                clearTimeout(pause);
                pause = false;
            }, function(){
                if (!pause) {
                    pause = setTimeout(headlineRotate, 8000);
                }
            });
            } else {
            	var el = $('.headline', this.element);
            	if (el.size()) {
            		el.eq(0).css('top', 0);
            	}
            }
        };
        
        var opts = $.extend($.fn.newsRoller.defaults, options);
        roller._init();
        return roller;
    };
    
    $.fn.newsRoller.defaults = {
        id: 'roller_container'
    };
    
    $(document).ready(function(){
      $('#news_roller_links').newsRoller();
    });
})(jQuery);

