var newsChangeTime = 8000;
var matchChangeTime = 5000;
var changing = false;
var newsTimer;
var matchTimer;
var currMatchBlock = "";

$(document).ready(function() {
	newsTimer = setInterval("newsUp()", newsChangeTime);
	$("#news_override").hide();
	if ($(".bk_nextmatch").length) {
		matchTimer = setInterval("matchChange()", matchChangeTime);
		$(".block_override").hide();
	}
	$("#news_up img").click(function(){
		if (!changing)
			newsUp();
	});
	$("#news_down img").click(function(){
		if (!changing)
			newsDown();
	});
});
function newsUp() {
	changing = true;
	clearInterval(newsTimer);
	referenceId = $("#showcase .preview:last input[name=id]").val();
	$.ajax({
		type: "POST",
		url: rootUrl + "/home/next_news/",
		data: "next=1&id=" + referenceId,
		cache: true,
		success: function(preview){
			$("#showcase .list_news").append(preview);
			$("#showcase .preview:last").find(".lframe div, .lframe td").ifixpng();
			$("#showcase .preview:first").animate({marginTop: "-142px"}, 1000, function(){
				$(this).remove();
				newsChange($("#showcase .preview:first input[name=id]").val());
			});
		}
	});
}
function newsDown() {
	changing = true;
	clearInterval(newsTimer);
	referenceId = $("#showcase .preview:first input[name=id]").val();
	$.ajax({
		type: "POST",
		url: rootUrl + "/home/next_news/",
		data: "next=-1&id=" + referenceId,
		cache: true,
		success: function(preview){
			$("#showcase .list_news").prepend(preview);
			$("#showcase .preview:first").css({marginTop: "-142px"});
			$("#showcase .preview:first").find(".lframe div, .lframe td").ifixpng();
			$("#showcase .preview:first").animate({marginTop: "0px"}, 1000, function(){
				$("#showcase .preview:last").remove();
				newsChange($("#showcase .preview:first input[name=id]").val());
			});
		}
	});
}
function newsChange(idNews) {
	$.ajax({
		type: "POST",
		url: rootUrl + "/home/news/",
		data: "id=" + idNews,
		cache: true,
		success: function(news){
			$("#news_override").fadeIn(500, function() {
				$("#showcase .news").html(news);
				$("#showcase .bframe div, #showcase .bframe td").ifixpng();
				$("#news_override").fadeOut(500, function() {
					newsTimer = setInterval("newsUp()", newsChangeTime);
					changing = false;
				});
			});
		}
	});
}
function matchChange() {
	if (!changing) {
		clearInterval(matchTimer);
		if ($(".bk_nextmatch").length == 2)
			currMatchBlock = (currMatchBlock == ".block:eq(0) ") ? ".block:eq(1) " : ".block:eq(0) ";
		else
			currMatchBlock = ".block:eq(0) ";
		currMatch = $(currMatchBlock+"input[type=hidden]").val();
		$.ajax({
			type: "POST",
			url: rootUrl + "/home/get_match/",
			data: "curr_match=" + currMatch,
			cache: true,
			success: function(match){
				if (match) {
					$(currMatchBlock+".block_override").fadeIn(500, function() {
						$(currMatchBlock+".bk_nextmatch").html(match);
						$(currMatchBlock).find(".lframe div, .lframe td").ifixpng();
						$(currMatchBlock+".block_override").fadeOut(500, function() {
							matchTimer = setInterval("matchChange()", matchChangeTime);
						});
					});
				} else
					matchTimer = setInterval("matchChange()", matchChangeTime);
			}
		});
	}
}