// Used to calculate a per second increment towards an annual value (rate)
//
// Written by Jason Kessler	Version 1.0
//
// Please email jkessler@dirtt.net with any comments or bug reports.
//
// rate is the annual end value
// yearSeconds is calculated using the Date.parse function returning the # of seconds elapsed from January 1, 1970
// seconds_base is the number of seconds elapsed for the current year.
// calc_rate is derived from rate / yearsSeconds
// vals_count is derived from seconds_base * calc_rate

var vals, lg, tills, x, posy_run, cnt_blank, yearSeconds
var calc_rate = 0;
var nums="";
var group="";
var vals_count = 0;
vals = "";
titleposy = 0;
var values=new Array();
var posy=new Array(242,484,726,968,1210,1452,1694,1936,2178,2420,0);
var cur_posy=new Array();
var myDate = new Date();

var rate = 47495361;

function settime(){
	
	yearSeconds = ((Date.parse("Dec 31, "+myDate.getFullYear())/1000)-(Date.parse("Jan 1, "+myDate.getFullYear())/1000)); // adjust seconds count for leap years
	calc_rate = (rate/yearSeconds);
	
	var seconds_base=((Date.parse(Date())/1000)-(Date.parse("Jan 1, "+myDate.getFullYear())/1000)); // will resolve leap years
	
	vals_count = seconds_base*calc_rate;

	vals_count= vals_count+calc_rate+(rate*(myDate.getFullYear()-2010));
	
	vals = Math.round(vals_count).toString();
	tills = "";
	posy_run = 0;
	
	lg = vals.length;

	if (lg < 11){
		cnt_blank = 11-lg;
	}
	
	for (x=0;x<cnt_blank;x++){
		 tills = tills + "~";
	}

	vals = tills + vals;

	for (x=0;x<11;x++){
		if (vals.substr(x,1) == "~") {
			values[x] = 11;
			if (x == 0){
				document.getElementById("comma1").style.display = "none";
			}
			if (x == 4){
				document.getElementById("comma2").style.display = "none";
			}
			if (x == 7){
				document.getElementById("comma3").style.display = "none";
			}
		} else {
			values[x] = vals.substr(x,1);
			if (x == 0){
				document.getElementById("comma1").style.display = "block";
			}
			if (x == 4){
				document.getElementById("comma2").style.display = "block";
			}
			if (x == 7){
				document.getElementById("comma3").style.display = "block";
			}
		}
	}
	
	if(group!=""){
		window.clearInterval(nums);
		window.clearInterval(group);
		nums="";
		group="";
	}
	
	group = window.setInterval("settime()",1000);
	nums = window.setInterval("flip_nums()",25);
}


function flip_nums(){
	
	for (x=10;x>=0;x--){
		if (values[x] == 11){
			document.getElementById("cd"+x).style.backgroundPosition = "0px 0px";
			document.getElementById("cd"+x).style.display = "none";
		} else {
			document.getElementById("cd"+x).style.display = "block";
			if (document.getElementById("cd"+x).style.backgroundPosition == "0px " + posy[values[x]] + "px") {
				document.getElementById("cd"+x).style.backgroundPosition = "0px " + posy[values[x]] +"px";
			} else {
				if (values[x] == 0){
					document.getElementById("cd"+x).style.backgroundPosition = "0px 242px";
				} else {
					document.getElementById("cd"+x).style.backgroundPosition = "0px " + (posy[values[x]-1]+posy_run)  +"px";
				}
			}
		}
	}
	
	posy_run = posy_run + 22;

	if (posy_run == 2662){
		posy_run = 0;
	}
	
}

function startup(){
	if(nums!=""){
		window.clearInterval(nums);
		nums="";
	}
	group = window.setInterval("settime()",1000);
}
