var analogClock = function (divElem, index) {
	this.mainElem = divElem;
	this.index = index;
	
	var numStyles = Array();
	numStyles["1"] = Array("1","2","3","4","5","6","7","8","9","10","11","12");
	numStyles["i"] = Array("i","ii","iii","iv","v","vi","vii","viii","ix","x","xi","xii");
	numStyles["I"] = Array("I","II","III","IV","V","VI","VII","VIII","IX","X","XI","XII");
	numStyles["."] = Array("·","·","—","·","·","|","·","·","—","·","·","||");
	
	this.position = analogClock.prototypes.position;
	numstyle = String(this.mainElem.getAttribute('numstyle'));
	if ("1I.".indexOf(numstyle.toUpperCase())==-1) numstyle="1";
	var numstyle=numStyles[numstyle];
	
	var radius = Math.floor(parseInt(this.mainElem.getAttribute('size'))/2);
	if (isNaN(radius)) radius=50;
	this.mainElem.style.width=radius*2 + "px";
	this.mainElem.style.height=radius*2 + "px";
	this.mainElem.style.position='relative';
	this.mainElem.style.overflow='hidden';
	this.mainElem.style.margin='0px';
	this.mainElem.style.padding='0px';
	//Create Hour labels
	this.hourLabels = Array();
	for (var i=0; i<12; i++) {
		this.hourLabels[i]=document.createElement("div");
		this.hourLabels[i].appendChild(document.createTextNode(numstyle[i]));
		this.hourLabels[i].style.position='absolute';
		this.mainElem.appendChild(this.hourLabels[i]);		
	}
	var numhours = Math.floor(radius*0.5);
	this.hourHand = Array();
	for (i=0; i<numhours; i++) {
		this.hourHand[i]=document.createElement("div");
		this.hourHand[i].style.backgroundColor=this.mainElem.getAttribute("hours");
		this.hourHand[i].style.width=(Math.ceil(radius*2/75)+1) + "px";
		this.hourHand[i].style.height=(Math.ceil(radius*2/75)+1) + "px";
		this.hourHand[i].style.position='absolute';
		this.hourHand[i].style.overflow='hidden';
		this.mainElem.appendChild(this.hourHand[i]);
	}
	var numminutes = Math.floor(radius*0.75);
	this.minuteHand = Array();
	for (i=0; i<numminutes; i++) {
		this.minuteHand[i]=document.createElement("div");
		this.minuteHand[i].style.backgroundColor=this.mainElem.getAttribute("minutes");
		this.minuteHand[i].style.width=(Math.round(radius*2/75)+1) + "px";
		this.minuteHand[i].style.height=(Math.round(radius*2/75)+1) + "px";
		this.minuteHand[i].style.position='absolute';
		this.minuteHand[i].style.overflow='hidden';
		this.mainElem.appendChild(this.minuteHand[i]);
	}
	var numseconds = Math.floor(radius*0.90);
	this.secondHand = Array();
	for (i=0; i<numseconds; i++) {
		this.secondHand[i]=document.createElement("div");
		this.secondHand[i].style.backgroundColor=this.mainElem.getAttribute("seconds");
		this.secondHand[i].style.width=(Math.floor(radius*1/75)+1) + "px";
		this.secondHand[i].style.height=(Math.floor(radius*1/75)+1) + "px";
		this.secondHand[i].style.position='absolute';
		this.secondHand[i].style.overflow='hidden';
		this.mainElem.appendChild(this.secondHand[i]);
	}
	this.AmPm=document.createElement("div");
	this.AmPm.style.position="absolute";
	this.mainElem.appendChild(this.AmPm);
	
	this.DoW=document.createElement("div");
	this.DoW.style.position="absolute";
	this.mainElem.appendChild(this.DoW);

	this.position();
}
analogClock.prototypes = Array();
analogClock.prototypes.position = function () {
	var Week = Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
	var radius = Math.floor(parseInt(this.mainElem.getAttribute('size'))/2);
	if (isNaN(radius)) radius=50;
	var Today = new Date();
	var Mills = Today.getMilliseconds();
	var Second = Today.getSeconds()+Mills/1000;
	var Minute = Today.getMinutes()+Second/60;
	var Hour = Today.getHours()+Minute/60;
	if (this.mainElem.getAttribute('tick')) {
		Hour=Math.floor(Hour);
		Minute=Math.floor(Minute);
		Second=Math.floor(Second);
	}
	if ("1\\yes\\true".indexOf(this.mainElem.getAttribute('localzone').toLowerCase())==-1) {
		Hour-=analogClock.getGMT();
		cz=parseInt(this.mainElem.getAttribute('clockzone'));
		Hour+=cz;
		Hour+=analogClock.needDST(cz);
	}
	
	while (Hour>=24) Hour-=12;
	while (Hour<0) Hour+=12;
	for (var i=0; i<this.hourLabels.length; i++) {
		radpos = (i+1)*2*Math.PI/12-Math.PI/2;
		this.hourLabels[i].style.top=(radius+Math.sin(radpos)*radius*0.90-parseInt(this.hourLabels[i].offsetHeight)/2) + "px";
		this.hourLabels[i].style.left=(radius+Math.cos(radpos)*radius*0.90-parseInt(this.hourLabels[i].offsetWidth)/2) + "px";
	}
	
	radpos=(Hour%12)*Math.PI*2/12-Math.PI/2;
	radpos+=Minute*2*Math.PI/60/12;
	
	for (i=0; i<this.hourHand.length; i++) {
		this.hourHand[i].style.top=(radius+Math.sin(radpos)*i) + "px";
		this.hourHand[i].style.left=(radius+Math.cos(radpos)*i) + "px";
	}
	radpos=Minute*Math.PI*2/60-Math.PI/2;
	for (i=0; i<this.minuteHand.length; i++) {
		this.minuteHand[i].style.top=(radius+Math.sin(radpos)*i) + "px";
		this.minuteHand[i].style.left=(radius+Math.cos(radpos)*i) + "px";
	}
	radpos=Second*Math.PI*2/60-Math.PI/2;
	for (i=0; i<this.secondHand.length; i++) {
		this.secondHand[i].style.top=(radius+Math.sin(radpos)*i) + "px";
		this.secondHand[i].style.left=(radius+Math.cos(radpos)*i) + "px";
	}

	this.AmPm.innerHTML=(Hour>=12)?"PM":"AM";
	this.AmPm.style.left=radius-parseInt(this.AmPm.offsetWidth)/2 + "px";
	this.AmPm.style.zIndex=-1;
	
	this.DoW.innerHTML=Week[Today.getDay()];
	this.DoW.style.left=radius-parseInt(this.DoW.offsetWidth)/2 + "px";
	this.DoW.style.zIndex=-1;
	
	if (Hour >= 12) Hour-=12;
	if ((Hour < 3) || (Hour > 9))
	{
		this.AmPm.style.top=1.400*radius + "px";
		this.DoW.style.top=1.080*radius + "px";
	}
	else
	{
		this.AmPm.style.top=0.333*radius + "px";
		this.DoW.style.top=0.666*radius + "px";
	}
}

analogClock.arClocks = new Array();

analogClock.replace = function () {
	var Divs = document.getElementsByTagName('div');
	for (var i=0; i<Divs.length; i++) 
		if (Divs[i].className.toLowerCase().indexOf("analogclock")!=-1)
			analogClock.arClocks[analogClock.arClocks.length]=new analogClock(Divs[i],analogClock.arClocks.length);
	setTimeout('analogClock.update();', 100);
}

analogClock.update = function () {
	for (var i=0; i<analogClock.arClocks.length; i++) 
		analogClock.arClocks[i].position();
	var interval = Math.floor(50*Math.pow(analogClock.arClocks.length,1.5));
	setTimeout('analogClock.update();', (analogClock.active)?(interval):(1000))
}

analogClock.getGMT = function () {
	var day;
	var Today = new Date();
	var gmtAdjust=-Today.getTimezoneOffset()/60;
	return gmtAdjust;
}

analogClock.needDST = function (a) {
	if ((a<-8)||(a>-5)) return(0);

	var Today = new Date();

	// getMonth = 0..11
	// getDate = 0..31

	// DST never during Jan, Feb, or Dec
	if ((Today.getMonth() < 2) || (Today.getMonth() == 11))
		return(0);
	
	// 2008 - 3/9 to 11/2
	// 2009 - 3/8 to 11/1
	// 2010 - 3/14 to 11/7
	// 2011 - 3/13 to 11/6
	// 2012 - 3/11 to 11/4
	// 2013	- 3/10 to 11/3
	// 2014	- 3/9 to 11/2
	// Month-1, Day-1
	
	if (Today.getFullYear() == 2011)
	{
		var myDate1 = new Date(Date.UTC(2011, 2, 12));
		var myDate2 = new Date(Date.UTC(2011, 10, 5));	
	}
	if (Today.getFullYear() == 2012)
	{
		var myDate1 = new Date(Date.UTC(2012, 2, 10));
		var myDate2 = new Date(Date.UTC(2012, 10, 3));	
	}
	if (Today.getFullYear() == 2013)
	{
		var myDate1 = new Date(Date.UTC(2013, 2, 9));
		var myDate2 = new Date(Date.UTC(2013, 10, 2));	
	}
	if (Today.getFullYear() == 2014)
	{
		var myDate1 = new Date(Date.UTC(2014, 2, 8));
		var myDate2 = new Date(Date.UTC(2014, 10, 1));	
	}

	if ((Today > myDate1) && (Today < myDate2))
		return(1);
	else	
		return(0);
}



analogClock.suspend = function () {
	analogClock.active=false;
}

analogClock.resume = function () {
	analogClock.active=true;
}

analogClock.attach = function () {
	if (window.attachEvent) {
		window.attachEvent('onload',analogClock.replace);
		window.attachEvent('onblur',analogClock.suspend);
		window.attachEvent('onfocus',analogClock.resume);
	}
	if (window.addEventListener) {
		window.addEventListener('load',analogClock.replace,false);
		window.addEventListener('blur',analogClock.suspend,false);
		window.addEventListener('focus',analogClock.resume,false);
	}
}

analogClock.active=true;
analogClock.attach();

   
    


