
var initialPos = [-257,-566,-875,-1184,-1493,-1802,-2111,-2420,-2729,-3038,-3347];

// Amination frames
var animationFrames = 5;
// Frame shift
var frameShift = 51;

// Starting number
var theNumber = 0;
// Increment
var increment = 1;
// Pace of counting in milliseconds
var pace = 1000;

var daysLeft = 0;
var hoursLeft = 0;
var minutesLeft = 0;
var secondsLeft = 0;

// Initializing variables
var digitsOld = [], digitsNew = [], subStart, subEnd, x, y;

// Function that controls counting
function doCount(){
	x = theNumber.toString();
	
	if(secondsLeft)
		secondsLeft--;
	else {
		secondsLeft = 59;
		if(minutesLeft)
			minutesLeft--;
		else {
			minutesLeft = 59;
			if(hoursLeft)
				hoursLeft--;
			else {
				hoursLeft = 23;
				if(daysLeft)
					daysLeft--;
				else
					daysLeft = 0;
			}
		}			
	}
	
	theNumber = (daysLeft*1000000) + (hoursLeft*10000) + (minutesLeft*100) + secondsLeft;
// 	var digits = splitToArray(theNumber.toString());
// 	for (var i = 0; i < digits.length; i++){
// 		$("#d" + i).css({'background-position': '0 ' + initialPos[digits[i]] + 'px'});
// 	}
	
	y = theNumber.toString();
	digitCheck(x,y);
}

// This checks the old count value vs. new value, to determine how many digits
// have changed and need to be animated.
function digitCheck(x,y){
	digitsOld = splitToArray(x);
	digitsNew = splitToArray(y);
	for (var i = 0; i < digitsNew.length; i++){
		if (digitsNew[i] != digitsOld[i]){
			animateDigit(i, digitsOld[i], digitsNew[i]);
		}
	}
}

// Animation function
function animateDigit(n, oldDigit, newDigit){
	// I want three different animations speeds based on the digit,
	// because the pace and increment is so high. If it was counting
	// slower, just one speed would do.
	// 1: Changes so fast is just like a blur
	// 2: You can see complete animation, barely
	// 3: Nice and slow
/*
	switch (n){
		case 0:
			speed = pace/8;
			break;
		case 1:
			speed = pace/4;
			break;
		default:
			speed = pace/2;
			break;
	}
	// Cap on slowest animation can go
	speed = (speed > 100) ? 100 : speed;
	// Get the initial Y value of background position to begin animation

	var pos = initialPos[oldDigit];
	// Each animation is 5 frames long, and 103px down the background image.
	// We delay each frame according to the speed we determined above.	
	if(oldDigit  == '0' && newDigit == '5')
		pos = initialPos[10];
	
	for (var k = 0; k < animationFrames; k++){
		pos = pos + frameShift;
		if(k == 1 || k == 3 || k == 5)
			pos+=1;
		if (k == (animationFrames - 1)){
//			$("#countdown_d" + n).delay(speed).animate({'background-position': '0 ' + pos + 'px'}, 0, function(){
				// At end of animation, shift position to new digit.
//				$("#countdown_d" + n).css({'background-position': '0 ' + initialPos[newDigit] + 'px'}, 0);
//			});
		}
		else{
			document.getElementById('countdown_d' + n).style.backgroundPosition = '0 ' + initialPos[newDigit] + 'px';
			//document.getElementById('countdown_d' + n).style.backgroundPosition = '0 ' + pos + 'px';
			//$("#countdown_d" + n).delay(speed).animate({'background-position': '0 ' + pos + 'px'}, 0);
		}
	}
	*/ 
	
	document.getElementById('countdown_d' + n).style.backgroundPosition = '0 ' + initialPos[newDigit] + 'px';
	
}

// Splits each value into an array of digits
function splitToArray(input){
	var digits = new Array();
	for (var i = 0; i < 9; i++){
		if(i < input.length) {
			subStart = input.length - (i + 1);
			subEnd = input.length - i;
			digits[i] = input.substring(subStart, subEnd);
		} else {
			digits[i] = '0';
		}
		
	}
	return digits;
}

// Sets the correct digits on load
function initialDigitCheck(){

	today = new Date();
	todayEpoch  = today.getTime();
	
	target = new Date("31 October, 2010"); 
	targetEpoch = target.getTime();
	
	secLeft = Math.floor((targetEpoch - todayEpoch) / 1000);
	daysLeft = Math.floor(secLeft / 86400);
	secLeft -= daysLeft * 86400;
	hoursLeft = Math.floor(secLeft / 3600);
	secLeft -= hoursLeft * 3600;
	minutesLeft = Math.floor(secLeft / 60);
	secLeft -= minutesLeft * 60;
	secondsLeft = Math.floor(secLeft);
	
	theNumber = (daysLeft*1000000) + (hoursLeft*10000) + (minutesLeft*100) + secondsLeft;

	var digits = splitToArray(theNumber.toString());
	
	for (var i = 0; i < digits.length; i++){
		document.getElementById('countdown_d' + i).style.backgroundPosition = '0 ' + initialPos[digits[i]] + 'px';
		//var t = document.getElementById("countdown_d0");
		//$("#countdown_d" + i).css({'background-position': '0 ' + initialPos[digits[i]] + 'px'});
		//t.css({'background-position': '0 ' + initialPos[digits[i]] + 'px'});
	}
}