/**
 * jQuery newsScroller Plugin by pmercier
 * Created for InfoPRO use only
 * version: 0.1 (20-NOV-2010)
 * @requires jQuery v1.4.4 or later
 * 
 * Documentation:
 * Calling:
 *     jQuery('#newsScroller').newsScroller();
 * Options:
 *     speed         'fast', 'slow' or milliseconds
 *     itemPause     milliseconds before switching to the next item in list
 *     pauseOnhover  if true when mouse is hover the scroller it'll wait
 *                   the cursor to leave before displaying next item
 *     tickerClass   The classname to add to the scroller container
 *                   Used to provide an unobstrusive behavior
 */
(function(jquery) {
	jQuery.fn.newsScroller = function(options) {
		var defaults = {  
			speed: 500,
			itemPause: 3000,
			pauseOnHover: true,
			tickerClass: 'hasJs'
		};
		
		var options = jQuery.extend(defaults, options),
			jContainer = jQuery(this),
			jUL = jQuery('ul:first', this);
		
		var animationPaused = false,
			animationRunning = false,
			animationInterval = 100,
			itemPauseCounterMax = options.itemPause,
			itemPauseCounter = itemPauseCounterMax;
		
		// Install pause on hover
		if (options.pauseOnHover) {
			jContainer.addClass(options.tickerClass).hover(function() {
				animationPaused = true;
			}, function() {
				animationPaused = false;
			});
		}
		
		// How the animation is done
		function animate() {
			if (!animationRunning && !animationPaused && itemPauseCounter <= 0) {
				var jFirstItem = jUL.children('li:first');
				jFirstItem.fadeTo(options.speed, 0).slideUp(options.speed, function() {
					jQuery(this).detach().appendTo(jUL).slideDown('1').fadeTo(options.speed, 1);
					animationRunning = false;
					itemPauseCounter = itemPauseCounterMax;
				});
				animationRunning = true;
			}
			if (!animationPaused) {
				itemPauseCounter -= animationInterval;
			}
		}
		window.setInterval(animate, animationInterval);
		
	}
})(jQuery);
