$(document).ready(function() {

//	The time in millesconds to delay the displaying
var newsRSSTimeDelay = 9000;

//	Read the items array generated by the PHP code
var newsRSSitems = "";

var timeout = 0;

var i = 0;
function showNextNewsRSSItem() {
	$(".displayer").fadeOut("fast", function() {
	
		//	Display the new item in the desired HTML element
		$(".myText").text(newsRSSitems[i]['title']);
		
		//	Give the link tag the right destination
		$("#newsItem").attr("href", newsRSSitems[i]['link']);
		
		//	Fade in the HTML which contains the text
		$(".displayer").fadeIn("fast", function() {
			i++;
			if (i >= newsRSSitems.length) {
				i = 0;
			}
		});
	});
		
	//	show the next item after a short wait
	timeout = setTimeout(showNextNewsRSSItem, newsRSSTimeDelay);
}
/*
	//On Hover
	$(".myText").hover(function() {
		clearTimeout(timeout); //Stop the rotation
	}, function() {
		showNextNewsRSSItem(); //Resume rotation
	});	
*/

$("img.RSSPrev").click(function() {
	clearTimeout(timeout);
	i = i-2;
	if (i < 0) {
		i = newsRSSitems.length+1;
	}
	showNextNewsRSSItem();
	
	return false;
});

$("img.RSSNext").click(function() {
	clearTimeout(timeout);
	showNextNewsRSSItem();
	
	return false;
});

$("img.RSSPause").click(function() {
	clearInterval(timeout);
	});

//	Start showing the news!
$.get("/includes/news.php", null, function(items) {
	newsRSSitems = $.parseJSON(items);
	showNextNewsRSSItem();
});

});

