
// When the DOM is ready
$(document).ready(function() {
	// If the wrapper of News Ticker is present:
	var $ticker = $('#news-wrapper');
	if($ticker.length > 0) {
		// Activate the News Ticker, by loading in some extra news articles 
		// via AJAX:
		$.ajax({
			// The URL at which to go and fetch news articles:
			url      : jQueryBaseHref + '/nieuws/ticker',
			// The expected data type in the response
			dataType : 'xml',
			// When we receive the news items:
			success  : function(data) {
				// For each of the news items we have loaded:
				$('item', data).each(function() {
					// Get the properties of the news article
					var title   = $(this).children('title').text();
					var href    = $(this).children('link').text();
					var preview = $(this).children('preview').text();

					// Create a new item in the list of news articles. We compose the
					// HTML to do so:
					var h = '';
					h += '<div class="news reading">';
					h +=    '<h1>'+ title +'</h1>';
					//h +=    '<p>'+ preview +'</p>';
					h +=    '<a href="'+ href +'" title="'+ title +'" class="readmore">'+ $.t('Lees meer') +'</a>';
					h += '</div>';

					// Add the HTML
					$('#news-wrapper').append($(h).hide());
				});

				// Start animating into view:
				showNewsTickerItem(0, 0);
				
				// Refresh cufon
				Cufon.refresh();
			}
		});
	}
});

// We define the function that animates a news item into view (news ticker)
function showNewsTickerItem(iCurrent, iPrevious) {
	// The jQuery Search Expression, with which we fetch news headlines in the
	// ticker's wrapper
	var jQuerySelector = '#news-wrapper .news';
	
	// Fade out the previously showing news item
	$(jQuerySelector + ':eq('+ iPrevious +')').fadeOut(200, function() {
		// Fade in the next one
		$(jQuerySelector + ':eq('+ iCurrent +')').fadeIn(500);
	});
	
	// Define the javascript to be executed in the next loop of the animation:
	var js = '';
	if((iCurrent + 1) == $(jQuerySelector).length) {
		js = 'showNewsTickerItem(0, '+ iCurrent +')';
	} else {
		js = 'showNewsTickerItem('+ (iCurrent + 1) +', '+ iCurrent +')';
	}
	
	// Set the interval, with the javascript:
	setTimeout(js, 4000);
}

