//  Copyright Andrew Johnson - Beechwood Communications Ltd
//  
//	structure is    	    <div class="newsScroller"...>
//				            	<div class="newsScroller-inner" ...>
//									<ul>
//										<li>[<div>...[news item]...</div>]</li>
//										<li>...etc
//	Requires jquery.timer-0.8.js
//


$(function() {  
	var newsTimer = $.timer();
	var yPos=[];
	var i=0;
	var speed=500;
	var delay=6500;
	var numElements=0;
	
	// get the ticker's position
	var tickPos=$(".newsScroller-inner").position();
	var tickOffset=tickPos.top;
	
	var tickerHeight=$(".newsScroller").height();
	var contentHeight=$(".newsScroller-inner ul").height();
//	alert("the scroller height is "+tickerHeight+" and the height of its contents is "+contentHeight);
	
	// get the vOffset of each LI in the list
	$(".newsScroller-inner li").each(function(index, domEl) {
		var posn=$(domEl).position();
		yPos[index]=Math.round(posn.top-tickOffset);
//		alert(" ["+index+"] "+yPos[index]);
		});
	numElements=yPos.length;
	
	// Add enough copies of the LIs to the UL to hide the jump after the last item
	$(".newsScroller-inner li").each(function(index, domEl) {
		if (yPos[index]<tickerHeight) {
			$(domEl).clone().appendTo($(".newsScroller-inner ul"));
			}
		});
		
	var nextposn=$(".newsScroller-inner li").eq(numElements).position();
	//alert(nextposn.top);
	yPos[numElements]==Math.round(nextposn.top-tickOffset);


	var curr=0;
	newsTimer.set({
        action : function() { 
			 curr++;
			 var offset=-1*(yPos[curr]);
			 $(".newsScroller-inner").animate( {top:offset}, speed, function() {
				 	 if (curr==numElements) { // should fire when we are at at the nth+1 element 
			 			$(".newsScroller-inner").css( {top:0} );
						curr=0;
			 			}
			 		});
			},
        time : delay
        }).play();
	
	$(".newsScroller").hover(
		function() { newsTimer.pause();},
		function() { newsTimer.play();}
		);
	});
