/*
 * jdNewsScroll 1.1 (2007-02-08)
 *
 * Copyright (c) 2006,2007 Jonathan Sharp (http://jdsharp.us)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * http://jdsharp.us/
 *
 * Built upon jQuery 1.1.1 (http://jquery.com)
 * This also requires the jQuery dimensions plugin
 */
jQuery(function() {
    jQuery('div.jd_news_scroll').jdNewsScroll();
});
(function(jQuery){
 	var ELMS = [];
 	jQuery.fn.jdNewsScroll = function(settings) {
		settings = jQuery.extend({}, arguments.callee.defaults, settings);
		jQuery(this).each(function(){
			this.jQuerysettings	= settings;
			this.jQuerypause 	= false;
			// Randomize when the scrolling will start, afterwards it will pause by delay
			this.jQuerycounter	= (Math.floor(Math.random() * 10) * 10);
			jQuery(this).hover(function(){ jQuery(this).jdNewsScrollPause(true) }, function(){ jQuery(this).jdNewsScrollPause(false) });
			jQuery('> ul', this)
				.bind('mouseover', function(e) {
					if (jQuery(e.target).is('li')) {
						jQuery(e.target).addClass('hover');
					}
				})
				.bind('mouseout', function(e) {
					if (jQuery(e.target).is('li')) {
						jQuery(e.target).removeClass('hover');
					}
				});
			ELMS.push(this);
		});
		return this;
	};
	jQuery.fn.jdNewsScroll.defaults = {
		// Delay in seconds is (delay * 85)/1000
		delay: 	480,
		// Number of pixels to step
		step:	2
	};
	jQuery.fn.jdNewsScrollPause = function(pause) {
		return this.each(function() {
			this.jQuerypause = pause;
		});
	}
	// Activate our 'scrolling agent'
    window.setTimeout(function(){
        goscroll();
    },8000);

    function goscroll(){
      	setInterval(scroll, 1);
    }

	// 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.jQuerypause) {
				if (elm.jQuerycounter == 0) {
					var ul 	= jQuery('> ul', elm)[0];
					if (!elm.jQuerysteps) {
						// Set the number of steps (the height of the li element)
						elm.jQuerysteps 	= jQuery('> li:last-child', ul).outerHeight();
						// Reset our step which will count backwards towards jQuerysteps
						elm.jQuerystep	= 0;
					}
					if ((elm.jQuerysteps + elm.jQuerystep) <= 0) {
						elm.jQuerycounter 	= elm.jQuerysettings.delay;
						elm.jQuerysteps 		= false;
						jQuery(ul).css('top', '0').find('> li:last-child').after(jQuery('> li:first-child', ul));
						jQuery('> *', ul).not('li').remove();
					} else {
						elm.jQuerystep -= elm.jQuerysettings.step;
						if (-elm.jQuerystep > elm.jQuerysteps) {
							elm.jQuerystep = -elm.jQuerysteps;
						}
						ul.style.top = elm.jQuerystep + 'px';
					}
				} else {
					elm.jQuerycounter--;
				}
			}
		}
	};
})(jQuery);

