var count_down_el;
var count_down_time;

function init_countdown()
{
		if( !document.getElementById("count_down_text") )  { return false; } // There is no current song on the page
		count_down_el = document.getElementById("count_down_text");
//		alert("Count Page"); // Test
		count_down_time = count_down_el.innerHTML; // Get the remaining seconds
		count_down(); // Start the countdown
}

function count_down() 
{
	count_down_time--;
	if (count_down_time == 0) 
	{
		// Adam's hack not to record stats on a reload
		var here = window.location+""; // Make it a string
		here = here.replace(/&reload=true/g,""); // Remove the previous reload
		if (here.indexOf("?") == -1) here += "?"; // If there's not already a GET, add it.
    if(!reload_a)
      window.location = here; 
    else
      init_a(); // Reload the info
		return;
	}
	else if (count_down_time < 0) //Something is wrong, just count from 10
	{
		count_down_time = 10; 
	}
	count_down_el.innerHTML = secs_to_min(count_down_time);
	counter = setTimeout("count_down()", 1000); //Reruns this every second
}

function secs_to_min(n) {
	var minutes = Math.floor(n / 60);
	var seconds = (n % 60);
	if ( seconds < 10) {		seconds = "0" + seconds; } // Add a 0 onto the seconds if below 10
	return(minutes + ":" + seconds);
}

function niceTime(total_seconds) {
	var hours = "00";
	var minutes = 0;
	var seconds = 0;
	if( total_seconds >= 3600 ) { 
		hours = Math.floor(total_seconds / 3600);
		total_seconds -= hours * 3600
	}
	minutes = Math.floor(total_seconds / 60);
	seconds = (total_seconds % 60);
	if (minutes < 10) { minutes = "0" + minutes; }
	if (seconds < 10) { seconds = "0" + seconds; }
	return(hours + ":" + minutes + ":" + seconds);
}

var regen_counter = [];
function do_regen(reset) 
{
	if(regen_counter['timer']) { clearTimeout(regen_counter['timer']); } // Clear the timeout to start over.
	if(!document.getElementById('regen_counter')) { return false; } // The regen counter doesn't exist on the page.
	
	if(reset) // Init for the regen counter element
	{ 
		regen_counter['el'] = document.getElementById("regen_counter"); // Get the element
		if(isNaN(regen_counter['el'].innerHTML * 1)) { return false; } // No need for a timer, they are full
		regen_counter['seconds'] = regen_counter['el'].innerHTML * 1; // Get the seconds and force it to be an INT
		regen_counter['interval'] = 60; // The seconds for each refresh of regen
	}
	//document.getElementById('debug').innerHTML = regen_counter['el'].innerHTML.indexOf(':') + ' - ' + regen_counter['el'].innerHTML + ' - ' + regen_counter['interval'];
	if (regen_counter['seconds'] <= 1 || regen_counter['interval'] <= 0) // If the timer hits 0 or every 5 minutes
	{
		clearTimeout(regen_counter['timer']);
		// Reload regen
		init_user_regen();
		if(tool_tip['type'] == "regen_info")
		{
			tool_tip['elmt'].hide();
		}
	}
	regen_counter['seconds']--;
	regen_counter['interval']--;
	regen_counter['el'].innerHTML = niceTime(regen_counter['seconds']);
	regen_counter['timer'] = setTimeout("do_regen()", 1000);
}
