var clocks=new Array();

function setupEvents()
{
	var divs=document.getElementsByTagName('div');

	for (var i=0; i < divs.length; i++)
	{
		if (inClass(divs[i], 'clock'))
		{
			clocks[clocks.length]=divs[i];
			createClock(divs[i]);
		}
	}

	updateAllClocks();
}

function createClock(parent)
{
	var hands=['hour', 'minute', 'second'];
	var cattr='class';
	var prefix='';

	if (document.all && !window.opera)
	{
		cattr='className';
		prefix='ie_';
	}

	for (var i=0; i < hands.length; i++)
	{
		var h=hands[i];
		var im=document.createElement('img');
		im.setAttribute(cattr, h+'s');
		im.setAttribute('src', 'assets/'+prefix+h+'_hands.png');
		parent.appendChild(im);
	}

	if (prefix == '')
	{
		var im=document.createElement('img');
		im.setAttribute(cattr, 'shine');
		im.setAttribute('src', 'assets/shine.png');
		parent.appendChild(im);
	}
}

function updateAllClocks()
{
	var d=new Date();
	
	//d.getMinutes();
	for (var i=0; i < clocks.length; i++)
	{
		var c=clocks[i];

		updateClock(c, d);		
	}

	setTimeout('updateAllClocks()', 1000);
}

function updateClock(clock, date)
{
	ims=clock.getElementsByTagName('img');

//	var status='';

	for (var i=0; i < ims.length; i++)
	{
		var im=ims[i];
		im.h=im.offsetHeight;
		
		var t;
		if (inClass(im, 'hours'))
			t=(date.getHours()%12)*5+parseInt((date.getMinutes()+6)/12);
		else if (inClass(im, 'minutes'))
			t=date.getMinutes();	
		else if (inClass(im, 'seconds'))
			t=date.getSeconds();
		else continue;

		var unith=im.offsetHeight/60.0;

		var offs=parseInt((clock.offsetWidth-unith)/2);

		im.style.clip='rect('+(t*unith)+'px, '+im.offsetWidth+'px, '+((t+1)*unith)+'px, 0)';
		im.style.top=(offs-t*unith)+'px';
		im.style.left=offs+'px';

//		status+=im.getAttribute('class')+": "+im.style.top+"\n";
	}

//	document.getElementById('status').innerHTML=status;
}

addEvent(window, 'load', setupEvents, false);

