﻿/**
 * jQuery Plugin NewsScroll v0.1
 *
 * Vertical drum scroller.  Scrolls child elements within a given target object.
 *
 * This plugin was inspired by Jeffrey Way http://net.tutsplus.com/videos/screencasts/how-to-build-a-super-duper-news-scroller/
 *
 * Copyright (c) 2009 Cundari Group Ltd.
 *
 * @author      Michael Hesler [Mike_Hesler at cundari dot com]
 * @copyright   (C) 2007. All rights reserved.
 *
 * @package     jQuery Plugins
 * @subpackage  NewsScroll
 *
 */
(function($) {
$.fn.newsScroll = function(options) {
    return this.each(function() {	
        var $this = $(this), 
            defaults = {
		  	    rate: 20, 
		  	    delay: 500
            },
            settings = $.extend({}, defaults, options);

        $this.cyclePause = 0;
		$this.mouseover(function(){$this.cyclePause++;});
		$this.mouseout(function(){$this.cyclePause--;});

        setTimeout(function(){scrollIt($this,settings);}, settings.delay);
	}); // end each
}
function scrollIt(container,settings) {
    var topChild = container.children(':first');
    var ms = settings.delay;

    if (!container.cyclePause) {
        var offset = topChild.offsetParent().offset().top - topChild.offset().top;
        var duration = ((topChild.outerHeight() - offset) / settings.rate) * 1000;
        topChild
            .animate(
                {marginTop : '-' + topChild.outerHeight() },
                {duration : duration,
                 step: function(step) {
                        if (container.cyclePause > 0) {
                            topChild.stop();
                            setTimeout(function(){scrollIt(container,settings);}, 100);
                        }
                    },
                 complete: function() {
                        topChild
                            .appendTo(container)
                            .css('marginTop', 0);
                        setTimeout(function(){scrollIt(container,settings);}, settings.delay);
                    }
                }
            ); // end animate
    } else {
        setTimeout(function(){scrollIt(container,settings);}, 100);
    }
}
})(jQuery);

