$(document).ready(function() {
	setInterval("count_down()", 1000);
});
function count_down()
{
	if (timer > 0)
	{
		timer -= 1;
	}
	$("#timer").text(format_timer(timer));
}
function game_ajax_update(data)
{
	if (Math.abs(timer - data.timer) > 1)
	{
		timer = data.timer;
	}
	if (hvz_status != data.hvz_status || hvz_pregame != data.hvz_pregame)
	{
		hvz_status = data.hvz_status;
		hvz_pregame = data.hvz_pregame;
		zombies = data.zombies;
		humans = data.humans;
		reg = data.reg;
		//setTimeout("count_down()", 1000);
		bluebar_refresh();
	}
	else
	{
		// change the counter and flash it
		var bbhighlight = "#ba503f";
		if ($("#bluebar").hasClass("bbzombie"))
		{
			bbhighlight = "#499630";
		}
		if (zombies != data.zombies || humans != data.humans || reg != data.reg)
		{
			zombies = data.zombies;
			humans = data.humans;
			reg = data.reg;
			$("#humans").text(data.humans + " Human" + ((data.humans == 1)?"":"s"));
			$("#zombies").text(data.zombies + " Zombie" + ((data.zombies == 1)?"":"s"));
			$("#reg").text(data.reg + " Registered");
			$("span#counter").effect("highlight", { color: bbhighlight }, 4000); 
			if(hvz_status == "admin")
			{
				var soundEmbed;
   				soundEmbed = document.createElement("embed");
    				soundEmbed.setAttribute("src", "/includes/snd/boing_3.wav");
    				soundEmbed.setAttribute("hidden", true);
    				soundEmbed.setAttribute("autostart", true);
    				document.body.appendChild(soundEmbed);
			}
		} 
	}
}

function bluebar_refresh()
{
	now = new Date().getTime();
	$.post("/ajax/bluebar", {time: now}, function(bluebar_html) {
		$("#bluebar").fadeOut(500, function() {
			$("#game_bluebar").html(bluebar_html);
			bluebar_prep();
			$("#bluebar").removeClass("bbhuman");
			$("#bluebar").removeClass("bbzombie");
			if (!hvz_pregame)
			{
				if (hvz_status == 'zombie')
				{
					$("#bluebar").addClass("bbzombie");
				}
				if (hvz_status == 'human')
				{
					$("#bluebar").addClass("bbhuman");
				}
			}
			$("#bluebar").fadeIn();
		});
	});
}

function format_timer(timer)
{
	var days = Math.floor(timer / 86400);
	var timer = timer % 86400;
	var hours = Math.floor(timer / 3600);
	var timer = timer % 3600;
	var minutes = Math.floor(timer / 60);
	var seconds = timer % 60;
	
	var formated_time = '';
	var plurality = 's';
	if (days > 0)
	{
		if (days == 1) {
			plurality = "";
		}
		formated_time += days + " day" + plurality + " ";
		plurality = "s";
	}
	
	if (days > 0 || hours > 0)
	{
		if (hours < 10)
		{
			formated_time += '0';
		}
		if (hours == 1) {
			plurality = "";
		}
		formated_time += hours + " hour" + plurality + " ";
		plurality = "s";
	}
	if (minutes < 10)
	{
		formated_time += '0';
	}
	if (minutes == 1) {
		plurality = "";
	}
	formated_time += minutes + " minute" + plurality + " ";
	plurality = "s";
	if (minutes < 5 && days == 0 && hours == 0)
	{
		if (seconds < 10)
		{
			formated_time += '0';
		}
		if (seconds == 1) {
			plurality = "\240";
		}
		formated_time += seconds + " second" + plurality;
		plurality = "s";
	}
	return formated_time;
}
