var remTimer, refreshTimer;

function remainingText(secondsRemaining)
{
	// CCJ for short form minutes
	function padDigits(n, totalDigits) 
	{ 
		n = n.toString(); 
		var pd = ''; 
		if (totalDigits > n.length) 
		{ 
			for (i=0; i < (totalDigits-n.length); i++) 
			{ 
				pd += '0'; 
			} 
		} 
		return pd + n.toString(); 
	} 

	if ( secondsRemaining == 0 )
	{
		// Refresh the page in 5 seconds if we're ready to process
		if (document.location.href.indexOf("board.php") != -1)
		{
			setTimer("location.reload()", 5000);
		}
	}
	
	if (secondsRemaining <= 0)
	{
		return 'Now';
	}

	var seconds = Math.floor( secondsRemaining % 60); 
	var minutes = Math.floor(( secondsRemaining % (60*60) )/60);
	var hours = Math.floor( secondsRemaining % (24*60*60)/(60*60) );
	var days = Math.floor( secondsRemaining /(24*60*60) );

	// CCJ: proper grammar
	var daystring, hourstring, minutestring, secondstring;

	if (days == 1) daystring = "day"; else daystring = "days";
	if (hours == 1) hourstring = "hour"; else hourstring = "hours";
	if (minutes == 1) minutestring = "minute"; else minutestring = "minutes";
	if (seconds == 1) secondstring = "second"; else secondstring = "seconds";

	// CCJ: proper grammar
	var daystring, hourstring, minutestring, secondstring;

	if (days == 1) daystring = "day"; else daystring = "days";
	if (hours == 1) hourstring = "hour"; else hourstring = "hours";
	if (minutes == 1) minutestring = "minute"; else minutestring = "minutes";
	if (seconds == 1) secondstring = "second"; else secondstring = "seconds";

	if ( days > 0 )
	{
		// D, H
		minutes += Math.round(seconds/60); // Add a minute if the seconds almost give a minute
		seconds = 0;
		
		hours += Math.round(minutes/60); // Add an hour if the minutes almost gives an hour
		minutes = 0;
		
		if ( hours > 0 )
			return days+' '+daystring+', '+hours+' '+hourstring;
		else
			return days+' '+daystring;
	}
	else if ( hours > 0 )
	{
		// H, M
		minutes += Math.round(seconds/60); // Add a minute if the seconds almost give a minute
		seconds = 0;
		
		if( compact )
			return hours+':'+padDigits(minutes, 2)+' hours';
		else
		{
			if ( minutes > 0 )
				return hours+' '+hourstring+', '+minutes+' '+minutestring;
			else
				return hours+' '+hourstring;
		}
	}
	else
	{
		// M, S
		if( compact )
			return minutes+':'+padDigits(seconds, 2)+' minutes';
		else
			return minutes+' '+minutestring+', '+seconds+' '+secondstring;
	}
}

function timer()
{
	phptime++;
	for(var i = 1;; i++) {
		temp = document.getElementById('timer' + i);
		if(temp == null) {
			break;
		} else {
			var timeLeft = remTimer[i] - phptime;

			temp.firstChild.nodeValue = remainingText(timeLeft);
			if(timeLeft <= 300) {
				temp.style.color = '#a00';
			}
		}
	}
}

function initialTime()
{
	remTimer = new Array();
	for(var i = 1;; i++) {
		temp = document.getElementById('timer_timestamp' + i);
		if(temp == null) {
			break;
		} else {
			remTimer[i] = temp.firstChild.nodeValue;
		}
	}
}

function startTimer()
{
	initialTime();
	window.setInterval('timer()', 1000);
}

window.onload = startTimer;
