function countdown(mdate,now) {

	diff = mdate-now;

	if ($("#count_days")&&$("#count_hrs")&&$("#count_min")&&$("#count_sec")) {

	if (diff>0) {

		days = Math.floor(diff/86400);
		diff = diff-(days*86400);
		label = "дней";
		tmp = ""+days+"";
		if (tmp.charAt(tmp.length-1)=="1") label = "день";
		if (parseInt(tmp.charAt(tmp.length-1))>=2&&parseInt(tmp.charAt(tmp.length-1))<=4) label = "дня";
		if (parseInt(tmp)>=10&&parseInt(tmp)<=20) label = "дней";

		hours = Math.floor(diff/3600);
		hours = (hours<10) ? "0"+hours : hours;
		diff = diff-(hours*3600);

		minutes = Math.floor(diff/60);
		minutes = (minutes<10) ? "0"+minutes : minutes;
		
		seconds = diff-(minutes*60);
		seconds = (seconds<10) ? "0"+seconds : seconds;

		if (days>0) { days_display = days+" "+label+","; }
		else { days_display = ""; }
		$("#count_days").html(days_display); 
		$("#count_hrs").html(hours);
		$("#count_min").html(minutes);
		$("#count_sec").html(seconds);
		setTimeout("countdown("+mdate+","+(now+1)+")",1000);
	} else {
		$("#count_days").html("");
		$("#count_hrs").html("00");
		$("#count_min").html("00");
		$("#count_sec").html("00");
	}

	}
}

function update(id) {
	$('#player_area').html("<div style=\"text-align:center;padding-top:200px;\"><img src=\"/images/ajax-loader.gif\" alt=\"\"/></div>");
	$.ajax({
		url: '/includes/ajax/players.php',
		type: 'POST',
		data: 'pid='+id,
		success: function (r) { $('#player_area').html(r); }
	});
	
	return false;
}

function filter_standings(table,champ) {

	$('#standings_tbl').css("opacity","0.5");

	$.ajax({
		url: '/includes/ajax/standings.php',
		type: 'POST',
		data: 'filter='+table+'&champ='+champ,
		success: function (r) { $('#standings_tbl').html(r); $('#standings_tbl').fadeTo(300,1); }
	});

	$('#filter2_tabs_wrap div').each(function() {
		if ($(this).attr("title")=="filter:filter_standings,'"+table+"',"+champ) { $(this).addClass("selected"); }
	});

	return false;
}

function filter_seasons(year) {

	$('#seasons-content').css("opacity","0.5");

	$.ajax({
		url: '/includes/ajax/seasons.php',
		type: 'POST',
		data: 'year='+year,
		success: function (r) { $('#seasons-content').html(r); $('#seasons-content').fadeTo(300,1); }
	});

	$('#filter_tabs_wrap div').each(function() {
		if ($(this).attr("title")==year) { $(this).addClass("selected"); }
	});

	return false;
}

$(document).ready(function() {

	$('#div_main_menu li').hover(
		function() {
			$(this).addClass("dact");
		},
		function() {
			$(this).removeClass("dact");
		}
	);
	$('#div_main_menu li').click(function() {
		if (!$(this).children('div').length) {
			location.href = $('a',this).attr("href");
		}
	});

	$('#div_main_menu li div div').hover(
		function() {
			if (this.className!="sep"&&this.className!="subm") { $(this).addClass("subact"); }
		},
		function() {
			if (this.className!="sep"&&this.className!="subm") { $(this).removeClass("subact"); }
		}
	);
	$('#div_main_menu li div div').click(function() {
			if (this.className!="sep"&&this.className!="subm"&&$(this).children('a').length) { location.href = $('a',this).attr("href"); }
	});

	$('#filter_tabs_wrap div').hover(
		function() {
			if (this.className!="selected") { $(this).addClass("dact"); }
		},
		function() {
			$(this).removeClass("dact");
		}
	);
	$('#filter_tabs_wrap div').click(function() {
		$('#filter_tabs_wrap div').removeClass("selected");
		$(this).addClass("selected");
		filter_seasons($(this).attr("title"));
	});

	$('#filter2_tabs_wrap div').hover(
		function() {
			if (this.className!="selected") { $(this).addClass("dact"); }
		},
		function() {
			$(this).removeClass("dact");
		}
	);
	$('#filter2_tabs_wrap div').click(function() {
		$('#filter2_tabs_wrap div').removeClass("selected");
		$(this).addClass("selected");
		tmp = /^filter:(.*?),(.*)$/.exec($(this).attr("title"));
		eval("if (typeof "+tmp[1]+"==\"function\") { "+tmp[1]+"("+tmp[2]+"); }");
	});

	$('.fixtures-table table th').click(function() {
		day = /day_(.*)/.exec(this.className)[1];
		another = (day%2==1) ? parseInt(day)+1 : parseInt(day)-1;

		$('.fixtures-table table.day_'+day+' tr.highlightable, .fixtures-table table.day_'+day+' tr.day_sep, .fixtures-table table.day_'+day+' th div div').toggle();
		$('.fixtures-table table.day_'+another+' tr.highlightable, .fixtures-table table.day_'+another+' tr.day_sep, .fixtures-table table.day_'+another+' th div div').toggle();
	});

	$('.header-link').hover(
		function() {
			$(this).addClass("light");
		},
		function() {
			$(this).removeClass("light");
		}
	);
	$('.header-link').click(function() {
		location.href=$(this).attr("abbr");
	});

	$('.external-link').click(function(e) {
		e.preventDefault();
		window.open($(this).attr("href"));
	});

	$('.highlightable').hover(
		function() {
			$(this).addClass("highlightable-style");
			if ($(this).attr("title")) {
				$(this).css("cursor","pointer");
				$(this).click(function() {
					location.href=$(this).attr("title");
				});
			}
		},
		function() {
			$(this).removeClass("highlightable-style");
		}
	);
});
