/*

jQuery row_two_newsticker (BBC style)

Structure inspired by Byan Gullan's plugin at http://www.makemineatriple.com/jquery

*/

$(document).ready(function() {

function runTicker(settings) {

	// Initialisation on first run
	if (settings.firstRun == 1) {
		currentItem = settings.currentItem;
		currentWidth = 0;
		settings.firstRun = 0;
		// Add news reveal bar
		$('#row_two_newsticker').after('<div id="row_two_newsrevealbar">_</div>');
		// Pause news when reveal bar hovered over (and hide reveal bar)
		$('#row_two_newsrevealbar').mouseover(function() {
			$(this).hide();
			clearTimeout(newsTimer);
		});
		// Pause news when headline hovered over (and hide reveal bar), restart afterwards
		$('#row_two_newsticker')
			.mouseover(function() {
				$('#row_two_newsrevealbar').hide();
				clearTimeout(newsTimer);
			})
			.mouseout(function() {
				newsTimer = setTimeout(function() {runTicker(settings);}, 5);
			})
	}

	// Loop over at end of news list
	if (currentItem == settings.newsItemCounter + 1) {
		currentItem = 0;
	}

	// If this is the start of animating a new line of news
	if (currentWidth == 0) {
		// Does this news item contain a link?
		if (settings.newsLinks[currentItem].length > 0) {
			// Yes
			$('#row_two_newsticker').empty().append('<li><span><a href="' + settings.newsLinks[currentItem] + '" target="_blank">' + settings.newsItems[currentItem] + '</a></span></li>');
		} else {
			// No
			$('#row_two_newsticker').empty().append('<li><span>' + settings.newsItems[currentItem] + '</span></li>');
		}
		targetWidth = $('#row_two_newsticker li span').outerWidth();

		// Set new position of reveal bar
		pos_newsitem = $('#row_two_newsticker li span').position();
		xpos = pos_newsitem.left;
		ypos = pos_newsitem.top;
		$('#row_two_newsrevealbar')
			.show()
			.css('left', xpos + 'px')
			.css('top', ypos + 'px')
			.css('width', targetWidth + 'px');
	}
	
	// Is reveal bar needing more animation?
	if (currentWidth < targetWidth) {
		// Yes - move it 1 pixel
		currentWidth++;
		xpos = $('#row_two_newsrevealbar').css('left');
		xpos = parseInt(xpos.substr(0, xpos.length - 2)); // Remove 'px' suffix and convert to num
		$('#row_two_newsrevealbar')
			.css('left', (xpos + 1) + 'px')
			.css('width', (targetWidth - currentWidth) + 'px');
		newsTimer = setTimeout(function() {runTicker(settings);}, 5);
	} else {
		// No, animation complete, hide reveal bar and do set up for showing next news item
		$('#row_two_newsrevealbar').hide();
		currentItem++;
		currentWidth = 0;
		newsTimer = setTimeout(function() {runTicker(settings);}, 3000);
	}
}

var newsItems = new Array();
var newsLinks = new Array();
var newsItemCounter = 0;
var newsTimer;

// Run through each li placing news headlines and links into arrays
$('#row_two_newsticker li').each(function() {
	// Does li contain a link?
	if ($(this).children('a').length) {
		// Yes - store headline + link
		newsItems[newsItemCounter] = $(this).children('a').text();
		newsLinks[newsItemCounter] = $(this).children('a').attr('href');
	} else {
		// No - store only headline
		newsItems[newsItemCounter] = $(this).text();
		newsLinks[newsItemCounter] = '';
	}
	newsItemCounter++;
});

var settings = {
	newsItems: newsItems,
	newsLinks: newsLinks,
	newsItemCounter: newsItemCounter - 1,
	currentItem: 0,
	firstRun: 1
}

newsTimer = setTimeout(function() {runTicker(settings);}, 5);

});
