/**
 * Module Group: VL
 * Controller JavaScript
 */

(function() {
	if ("jQuery" in window && 'C4News' in window) { 

		C4News.ticker = function(){
			
			var tickerId = 'ticker'; // id of ticker list (ul)
			var pauseDuration = 5000; // delay between news items
			var animateDuration = 300; // scroll speed
			
			var ticker;
			var timer;
			var items = 0;
			var currentItem = 0;
			var nextItem = 1;
			
			var init = function(){
				items = $('#' + tickerId + ' li').length;
				if(items > 1) {
					timer = setInterval(function(){
						$('#' + tickerId + ' li:eq(' + currentItem + ')').animate({top:-40}, animateDuration, function(){
							$(this).css('top', '40px');
							$('#' + tickerId + ' li:eq(' + nextItem + ')').css('display', 'block').animate({top:0}, animateDuration);					
							if(currentItem < (items-1)) {
								currentItem = currentItem +1;
								nextItem = (currentItem == (items-1)) ? 0 : currentItem +1;
							} else {
								currentItem = 0;
								nextItem = 1;
							}
						});

					}, pauseDuration);
				}
			};
			
			return{
				init:init
			};
			
		}();
			
		$(document).ready(function(){
			if($('.moduleID-VL14')){
				C4News.ticker.init();
			}
		});

	}
})();
