/***************************************
Analogue clock script - useless but fun
Written by Mark Wilton-Jones, 27/5/2002
****************************************

Please see http://www.howtocreate.co.uk/jslibs/ for details and a demo of this script
Please see http://www.howtocreate.co.uk/jslibs/termsOfUse.html for terms of use

To use:
	Add the following lines just before the </body> tag - you can use more than one:

<script src="PATH TO SCRIPT/anclock.js" type="text/javascript" language="javascript1.2"></script>
<script type="text/javascript" language="javascript1.2"><!--
drawClock(
	'#0000ff', //colour of hour hand
	'#009900', //colour of minute hand
	'#ff0000', //colour of second hand
	'#000000', //colour of numbers
	25,        //size of clock - best around 50
	4,         //position of clock: 1 = top-left, 2 = top-right, 3 = bottom-left, 4 = bottom-right
	-100,      //horizontal offset from normal position in corner (normally 0)
	-50,       //vertical offset from normal position in corner (normally 0)
	true       //scroll with screen
);
//--></script>

*/

function getRefToDivNest( divID, oDoc ) {
	if( !oDoc ) { oDoc = document; }
	if( document.layers ) {
		if( oDoc.layers[divID] ) { return oDoc.layers[divID]; } else {
			for( var x = 0, y; !y && x < oDoc.layers.length; x++ ) {
				y = getRefToDivNest(divID,oDoc.layers[x].document); }
			return y; } }
	if( document.getElementById ) { return document.getElementById(divID); }
	if( document.all ) { return document.all[divID]; }
	return document[divID];
}
function drawClock(h,m,s,f,oAmp,p,xOf,yOf,s2) {
	window.clockCount = ( typeof( window.clockCount ) == 'number' ) ? window.clockCount + 1 : 0;
	var oCol = [h,m,s];
	//create the numbers
	for( var x = 1; x < 13; x++ ) {
		if( document.layers ) {
			document.write( '<layer id="c' + window.clockCount + 'num' + x + '" left="0" top="0"><font size="1" color="' + f + '">' + x + '</font></layer>' );
		} else {
			document.write( '<div id="c' + window.clockCount + 'num' + x + '" style="position:absolute;left:0px;top:0px;"><font size="1" color="' + f + '">' + x + '</font></div>' );
		}
	}
	//create the hands
	for( var y = 2; y >= 0; y-- ) { for( var x = 0; x < y + 3; x++ ) {
		if( document.layers ) {
			document.write( '<layer id="c' + window.clockCount + 'hand' + y + 'dot' + x + '" clip="0,0,2,2" left="0" top="0" bgcolor="' + oCol[y] + '">&nbsp;</layer>' );
		} else {
			document.write( '<div id="c' + window.clockCount + 'hand' + y + 'dot' + x + '" style="position:absolute;left:0px;top:0px;height:2px;width:2px;background-color:' + oCol[y] + ';clip:rect(0px 2px 2px 0px)"></div>' );
		}
	} }
	window.setInterval('aniClock(' + p + ',' + s2 + ',' + oAmp + ',' + xOf + ',' + yOf + ',' + window.clockCount + ')',100);
}
function aniClock(p,s,oAmp,xOf,yOf,c) {
	if( !getRefToDivNest('c'+c+'hand2dot4') ) { return; } //not loaded yet or not supported
	//get the screen size so that we can put it in a corner and scroll . . .
	var scrW = 0, scrH = 0, scrOfX = 0, scrOfY = 0;
	if( typeof( window.innerWidth ) == 'number' ) { scrW = window.innerWidth; scrH = window.innerHeight; } else {
		if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			scrW = document.documentElement.clientWidth; scrH = document.documentElement.clientHeight; } else {
			if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
				scrW = document.body.clientWidth; scrH = document.body.clientHeight; } } }
	if( typeof( window.pageYOffset ) == 'number' ) { scrOfY = pageYOffset; scrOfX = pageXOffset; } else {
		if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) { scrOfY = document.body.scrollTop; scrOfX = document.body.scrollLeft; } else {
			if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) { scrOfY = document.documentElement.scrollTop; scrOfX = document.documentElement.scrollLeft; } }
	}
	//now we have the screen settings in all known DHTML browsers - cool huh?
	//apply those settings along with positioning and scrolling preference to find offset positions
	var allXoffset = ( p % 2 ) ? oAmp + 10 + ( s ? scrOfX : 0 ) : ( scrW - ( oAmp + 10 ) ) + ( s ? scrOfX : 0 );
	var allYoffset = ( p < 3 ) ? oAmp + 10 + ( s ? scrOfY : 0 ) : ( scrH - ( oAmp + 19 ) ) + ( s ? scrOfY : 0 );
	//calculate relative positions of each number and put them on the page
	var oPix = document.childNodes ? 'px' : 0;
	for( var x = 1; x < 13; x++ ) {
		var divRef = getRefToDivNest( 'c' + c + 'num' + x ); if( divRef.style ) { divRef = divRef.style; }
		divRef.top = ( Math.round( -1 * oAmp * Math.cos( ( Math.PI * 2 *  x ) / 12 ) ) + allYoffset + yOf - 6 ) + oPix;
		divRef.left = ( Math.round( oAmp * Math.sin( ( Math.PI * 2 *  x ) / 12 ) ) + allXoffset + xOf - ( ( x > 9 ) ? 4 : 1 ) ) + oPix;
	}
	var theTime = new Date(); theTime = [( theTime.getHours() % 12 ) / 12,theTime.getMinutes() / 60,theTime.getSeconds() / 60];
	//calculate relative positions of each hand and put them on the page
	for( var y = 0; y < 3; y++ ) { for( var x = 0; x < y + 3; x++ ) {
		var divRef = getRefToDivNest( 'c' + c + 'hand' + y + 'dot' + x ); if( divRef.style ) { divRef = divRef.style; }
		divRef.top = ( Math.round( -1 * oAmp * ( x / 4 ) * Math.cos( Math.PI * 2 * theTime[y] ) ) + allYoffset + yOf ) + oPix;
		divRef.left = ( Math.round( oAmp * ( x / 4 ) * Math.sin( Math.PI * 2 * theTime[y] ) ) + allXoffset + xOf ) + oPix;
	} }
}