// World evangelism status clock - Curt Turner 2007
// Based on a concept which originated (to the best of my knowledge) at
// thegreatcom.net.

// The original script had many problems, including trying to re-calculated 
// every cell in the table 1000 times per second, even though its use of 
// rounding functions caused the display to only be updated once per second. Not 
// only did this consume as much processing power as it possibly could, it made 
// for a somewhat clunky display where all the figures 'snapped' once per 
// second. This script calculates all of the totals ahead of time, then runs 
// three seperate interval timers to increment those totals. The first row of 
// figures is obtained by calculating the total of last three rows as they change. 
// This insures that the totals are correct as well as giving it a more organic
// feel.

// Also, the original script based all of its totals on a global population 
// increase of 2.77 per second. Using the current figures of 133,284,000 births 
// and 56,674,000 deaths annually, this comes out to roughly 2.43 births / 
// second. (133,284,000 - 56,674,000) / (365.25 * 24 * 60 * 60) This adjustment 
// brings the total calculated population MUCH closer to the actual (as of early 
// 2007) total of 6.6 billion.

birthspersec=2.427624;

// following figures are based on percentages of previous calculations
hear_believe=birthspersec*0.11046931407942238267148014440433; 
hear_not=birthspersec*0.3862815884476534296028880866426;
hear_no_chance=birthspersec*0.50324909747292418772563176895307;

meters=new Array();
now=new Date();
todate=new Date(now);
thisYear=now.getFullYear();
todate.setHours(0,0,0);

newyears99=new Date();
newyears99.setFullYear(1999,4,4);
newyears99.setHours(0,0,0);

newyearsthis=new Date();
newyearsthis.setFullYear(thisYear,0,1)
newyearsthis.setHours(0,0,0);

mark_thisyear=newyearsthis.getTime();
mark99=newyears99.getTime();
mark_today=todate.getTime();

now=new Date();
msnow=now.getTime();
secsToday=(msnow-mark_today)/1000;
secsSince=(msnow-mark_thisyear)/1000;
secsSincePop=(msnow-mark99)/1000; 
	
meters[0]=secsToday*birthspersec;                    // pop increase today
meters[1]=secsToday*hear_believe;                    // hear/believe today
meters[2]=secsToday*hear_not;                        // hear/not believe today
meters[3]=secsToday*hear_no_chance;                  // no chance to hear today
meters[4]=secsSince*birthspersec;                    // pop increase year
meters[5]=secsSince*hear_believe;                    // hear/believe year
meters[6]=secsSince*hear_not;                        // hear/not believe year
meters[7]=secsSince*hear_no_chance;                  // no chance to hear year
meters[8]=(secsSincePop*birthspersec)+6020543616;    //total pop
meters[9]=(secsSincePop*hear_believe)+664845308;     //total hearing/believing
meters[10]=(secsSincePop*hear_not)+2326958575;       //total hearing/not believing
meters[11]=(secsSincePop*hear_no_chance)+3028739733; //total not chance to hear

function startClock() {
	for(k=0;k<12;k++){
		meters[k]=Math.round(meters[k]);
		$('Meter'+k).innerHTML=fmt(meters[k]);
	}
	timer1=setInterval("cntUpdate(1)",1000/hear_believe);
	timer2=setInterval("cntUpdate(2)",1000/hear_not);
	timer3=setInterval("cntUpdate(3)",1000/hear_no_chance);
}

function cntUpdate(which){
	for(i=0;i<3;i++){
		ind=which+i*4;
		meters[ind]++;
		$('Meter'+ind).innerHTML=fmt(meters[ind]);
		tUpdate(i);
	}
}

function tUpdate(which){
	$('Meter'+which*4).innerHTML=fmt(meters[which*4+1]+meters[which*4+2]+meters[which*4+3]);
}

function fmt(number) {
	out="";
	inc=0;
	tempnum= Math.round(number).toString();
	for(j=tempnum.length-1;j>=0;j--){
		if(inc==3){
			out=","+out;
			inc=0;
		}
		out=tempnum.charAt(j)+out;
		inc++;
	}
	return out;
}

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