/**
 * @author James Barwell
 */

// Initalise the clock?
k = 1;

// Initialising clock
function initSeconds(whichSec)
{
	for(var i = whichSec; i >= 0; i--)
	{
		BlockSec = document.getElementById("s" + i);
		BlockSec.style.backgroundColor = "#FFFFFF";
	}
}

function initMinutes(whichMin)
{
	for(var i = whichMin; i >= 0; i--)
	{
		BlockMin = document.getElementById("m" + i);
		BlockMin.style.backgroundColor = "#FFFFFF";
	}
}

function initHours(whichHour)
{
	for(var i = whichHour; i >= 0; i--)
	{
		BlockHour = document.getElementById("h" + i);
		BlockHour.style.backgroundColor = "#FFFFFF";
	}
}

// Update clock
function updateMillisecs(whichMsec)
{
	for(var i = whichMsec; i >= 0; i--)
	{
		BlockMsec = document.getElementById("ms" + i);
		BlockMsec.style.backgroundColor = "#FFFFFF";
	}
}

function updateSeconds(whichSec)
{
	BlockSec = document.getElementById("s" + whichSec);
	BlockSec.style.backgroundColor = "#FFFFFF";
	delete BlockSec;
}

function updateMinutes(whichMin)
{
	BlockMin = document.getElementById("m" + whichMin);
	BlockMin.style.backgroundColor = "#FFFFFF";
	delete BlockMin
}

function updateHours(whichHour)
{
	BlockHour = document.getElementById("h" + whichHour);
	BlockHour.style.backgroundColor = "#FFFFFF";
	delete BlockHour
}

// Reset clock columns
function resetMillisecs()
{
		for(var j = 9; j >= 1; j--)
		{
			wipeMsec = document.getElementById("ms" + j);
			wipeMsec.style.backgroundColor = "#000000";
		}
}

function resetSeconds()
{
		for(var j = 59; j >= 1; j--)
		{
			wipeSec = document.getElementById("s" + j);
			wipeSec.style.backgroundColor = "#000000";
		}
}

function resetMinutes()
{
		for(var j = 59; j >= 1; j--)
		{
			wipeMin = document.getElementById("m" + j);
			wipeMin.style.backgroundColor = "#000000";
		}
}

function resetHours()
{
		for(var j = 11; j >= 1; j--)
		{
			wipeHour = document.getElementById("h" + j);
			wipeHour.style.backgroundColor = "#000000";
		}
}


function initClock()
{
	var currentTime = new Date();
	var currentSeconds = currentTime.getSeconds();
	var currentMinutes = currentTime.getMinutes();
	var currentHours = currentTime.getHours();

	initSeconds(currentSeconds);
	initMinutes(currentMinutes);
	initHours(currentHours);
}



function updateClock()
{
	if(k==1){
		k=0;
		initClock();
	}
	
	var currentTime = new Date();
	var currentMillisecs = (currentTime.getMilliseconds()/100);
	var currentSeconds = currentTime.getSeconds();
	var currentMinutes = currentTime.getMinutes();
	var currentHours = currentTime.getHours();
	var sendMS = Math.floor(currentMillisecs);

	updateMillisecs(sendMS);
	updateSeconds(currentSeconds);
	updateMinutes(currentMinutes);
	updateHours(currentHours);
	
	if(sendMS == 0) {resetMillisecs();}	
	if(currentSeconds == 0) {resetSeconds();}
	if(currentMinutes == 0) {resetMinutes();}
	if(currentHours == 0) {resetHours();}
		
	//test to prove the clock updates
	/*var debug = document.getElementById('pgtitle');
	debug.firstChild.nodeValue= "jsclock ms_" + sendMS + " s_" + currentSeconds + " m_" + currentMinutes + " h_" + currentHours;
	
	setTimeout("updateClock()", 100);	
	*/
}



