var newsSaved = new Object();
var rotateLocked = new Object();
var rotateTimer = new Object();

function RotateNews(elementid, from, num, seconds, clear) {
	if(from == null) from = Math.round(Math.random()*10);
	if(num == null) num = 1;
	if(seconds == null) seconds = 8;
	if(clear == null) clear = false;
	
	if(clear) {
		clearTimeout(rotateTimer[elementid]);
		rotateLocked[elementid] = false;
	}
	
	if(newsSaved[elementid] == null || clear) {
		newsSaved[elementid] = new Array();
	}
	else {
		if(newsSaved[elementid].length > 3) return;
	}
	
	if(rotateLocked[elementid] == null) {
		rotateLocked[elementid] = false;
		
		$("#"+elementid).mouseover(function() {
			rotateLocked[elementid] = true;
			if(elementid == "top-sport-news") {
				$(this).css("height", "185px");
				$(this).css("overflow", "auto");
			}
		});
		$("#"+elementid).mouseout(function() {
			rotateLocked[elementid] = false;
			if(elementid == "top-sport-news") {
				$(this).css("height", "100px");
				$(this).css("overflow", "hidden");
			}
		});
	}
	else if(rotateLocked[elementid]) {
		setTimeout(function() {
			RotateNews(elementid, from, num, seconds);
		}, seconds*1000);
		return;
	}
	
	var q = "";
	q += "from="+from+"&"+"len="+num+"&small=true";
	
	if(clear || $.trim($("#"+elementid).html()).length == 0)
		$("#"+elementid).html('<div style="background: url(images/bg-ajax-loader.gif) no-repeat center center; height: 90px; line-height: 90px;"></div>');
	
	/*$.ajax({
		type: "GET",
		//url: "http://www.webfantacalcio.it/script/classes/getNews.php",
		url: "index.php?page=get-news",
		data: q,
		ifModified: true,
		success: function(msg) {
			$("#"+elementid).fadeOut("fast", function() {
				if(elementid == "top-sport-news") $("#"+elementid).html('<div style="height: 155px; overflow: hidden">' + msg + '</div>');
				else $("#"+elementid).html(msg);
				
				newsSaved[elementid].push(msg);
				
				$("#"+elementid).fadeIn("slow");
				
				rotateTimer[elementid] = setTimeout(function() {
					RotateNews(elementid, from+num, num, seconds);
				}, seconds*1000);
				
				//if(elementid == "top-sport-news") {
					$("#"+elementid).append('<div style="text-align: right; margin-bottom: 8px; margin-right: 5px;"><a style="font-size: 10px; font-family: Verdana, sans-serif" href="?page=serie_a&sub=notizie">Tutte le notizie</a>&nbsp;&nbsp;<a class="next-prev" href="javascript:void(0);" onclick="RotateNews(\''+elementid+'\', '+(from - num)+', '+num+', '+seconds+', true);"><img src="images/previous.gif" width="16" height="16"></a> <a class="next-prev" href="javascript:void(0);" onclick="RotateNews(\''+elementid+'\', '+(from + num)+', '+num+', '+seconds+', true);"><img src="images/next.gif" width="16" height="16"></a></div>');
				//}
			});
		},
		error: function(m) { this.success(m); }
	});*/
	
	$.get("index.php?page=get-news&"+q, function(msg) {
		$("#"+elementid).fadeOut("fast", function() {
			if(elementid == "top-sport-news") $("#"+elementid).html('<div style="height: 155px; overflow: hidden">' + msg + '</div>');
			else $("#"+elementid).html(msg);
			
			newsSaved[elementid].push(msg);
			
			$("#"+elementid).fadeIn("slow");
			
			rotateTimer[elementid] = setTimeout(function() {
				RotateNews(elementid, from+num, num, seconds);
			}, seconds*1000);
			
			//if(elementid == "top-sport-news") {
				$("#"+elementid).append('<div style="text-align: right; margin-bottom: 8px; margin-right: 5px;"><a style="font-size: 10px; font-family: Verdana, sans-serif" href="news-serie-a;">Tutte le notizie</a>&nbsp;&nbsp;<a class="next-prev" href="javascript:void(0);" onclick="RotateNews(\''+elementid+'\', '+(from - num)+', '+num+', '+seconds+', true);"><img src="images/prev.gif" width="10" height="16" alt="" /></a> <a class="next-prev" href="javascript:void(0);" onclick="RotateNews(\''+elementid+'\', '+(from + num)+', '+num+', '+seconds+', true);"><img src="images/next.gif" width="10" height="16" alt="" /></a></div>');
			//}
		});
	});
}