/*


	jQuery plugin: tickerScroll
	Author: Ian Hart at Grand Union [ianh@thegrandunion.com]


	What?
	=====
	Creates a good ol' ticker from an unordered list.


	How?
	====
	This is a semi-custom plugin for the EON site and requires the following...
	
		HTML:
		
			<div class="ticker-container">
				<ul id="ticker">
					<li>Lorem ipsum dolor sit amet.</li>
					<li>Ut enim ad minim veniam.</li>
					<li>Excepteur sint occaecat.</li>
				</ul>
			</div>
			
		CSS:
		
			.ticker-container 	{ width:750px; height:30px; float:left; display:inline; overflow:hidden; position:relative; }
			#ticker 			{ width:9999px; position:absolute; top:0; left:0; visibility:hidden; }
			#ticker li 			{ float:left; }
			
		JS:
		
			$('#ticker').tickerScroll();
			
			
	Options
	=======
	
	speed: The rate at which the ticker moves (pixels per second).
	
	
	Dependencies
	============
	
	- jQuery 1.3.2 (jquery.com)


*/
(function($){

	$.fn.tickerScroll = function(settings){

		var config = {speed: 0.05},
			G = {
				$list : this,
				liTotalWidth : 0,
				leftStart : 0,
				leftTarget : 0
			};
		
		if(settings){ $.extend(config, settings); }


		function init(el){

			G.leftStart = G.$list.parent().outerWidth();

			$('li', el).each(function(){
				G.liTotalWidth += $(this).outerWidth();
			});

			// Initialise width of list and left position target and start.
			G.$list.width(G.liTotalWidth+500);
			G.leftTarget = 0 - G.liTotalWidth;
			G.$list.css({
				'left' : G.leftStart,
				'visibility' : 'visible'
			});
			
			// Pause on hover.
			/*G.$list.parent().hover(
				function(){
					G.$list.stop();
				},
				function(){					
					doScroll();
				}
			);*/

			// Let's start.
			doScroll();
			
		}
		
		
		function doScroll(){

			// Calculate duration each time since the $list will be in a different position (due to hovering and stopping).
			var time = (G.liTotalWidth + parseInt(G.$list.css('left'), 10)) / config.speed;

			G.$list.animate({
				'left':G.leftTarget
			}, {
				easing : 'linear',
				duration : time,
				complete : function(){
					G.$list.css('left', G.leftStart); // Move to beginning.
					doScroll();
				}
			});
		
		}


		init(this);

		
		return this;
	
	};

})(jQuery);