//
// Loosely based on liScroll:
// http://www.gcmingati.net/wordpress/wp-content/lab/jquery/newsticker/jq-liscroll/scrollanimate.html
//
(function($) {
	$.fn.icker = function(options) {
		options = $.extend({ }, $.fn.icker.defaults, options ? options : { });
		return this.each(function() {
			var $ticker = $(this);
			$ticker.wrap('<div class="' + options.maskClass + '"></div>');
			$ticker.parent().wrap('<div class="' + options.containerClass + '"></div>');

			var maskWidth  = $ticker.parent().parent().width();
			var totalWidth = 0;
			$ticker.find('li').each(function(i) {
				totalWidth += $(this, i).width();
			});
			$ticker.width(totalWidth);
			totalWidth += maskWidth;

			//
			// Unbinding and rebinding the hover() actions aren't
			// doing the right thing but a 'stopped' flag works
			// just as well.
			//
			var stopped   = false;
			var loopsLeft = options.loopFor;
			function animateTicker(dx) {
				$ticker.animate({ left: '-=' + dx }, 1000 * dx / options.speed, 'linear', runAnimation);
			}
			function runAnimation() {
				if(typeof loopsLeft != 'undefined') {
					if(loopsLeft <= 0) {
						stopped   = true;
						loopsLeft = options.loopFor;
						$ticker.css('left', '-' + $ticker.css('padding-left'));
						if(options.onLooped)
							options.onLooped();
						return;
					}
					--loopsLeft;
				}
				$ticker.css('left', maskWidth);
				animateTicker(totalWidth);
			}
			function pauseAnimation() {
				if(stopped)
					return;
				$ticker.stop();
			}
			function unpauseAnimation() {
				if(stopped)
					return;
				animateTicker(totalWidth + $ticker.offset().left);
			}
			function stopAnimation() {
				stopped = true;
				$ticker.stop();
			}
			function playAnimation() {
				stopped = false;
				animateTicker(totalWidth + $ticker.offset().left);
			}

			if(options.hoverPause)
				$ticker.hover(pauseAnimation, unpauseAnimation);
			if(options.controlPlay)
				$('#' + options.controlPlay).click(playAnimation);
			if(options.controlRestart
			&& options.onRestart)
				$('#' + options.controlRestart).click(function() { stopped = false; options.onRestart(); runAnimation(); });
			else if(options.controlRestart)
				$('#' + options.controlRestart).click(function() { stopped = false; runAnimation(); });
			if(options.controlStop)
				$('#' + options.controlStop).click(stopAnimation);

			runAnimation();
		});
	};

	$.fn.icker.defaults = {
		containerClass: 'ickerContainer',	// class for the container <div>
		controlPlay:     null,				// id of the "play" button
		controlStop:     null,				// id of the "stop" button
		controlRestart:  null,				// id of the "restart" button
		hoverPause:      true,				// pause the ticker on :hover?
		loopFor:         undefined,			// how many times should we loop? 'undefined' means forever
		maskClass:      'ickerMask',		// class for the mask <div>, this goes inside the container
		speed:           50,				// ticker speed, px/s
		onLooped:        null,				// callback for when we stop looping due to loopFor
		onRestart:     	 null				// callback for when the restart control is hit
	};
})(jQuery);
// $HeadURL: http://localhost/svn/isos/tmpl/static/jquery/icker.js $
// $Id: icker.js 5862 2010-07-28 16:47:52Z mu $
