// Array to hold each digit's starting background-position Y value
var initialPos = [0, -618, -1236, -1854, -2472, -3090, -3708, -4326, -4944, -5562];
// Amination frames
var animationFrames = 5;
// Frame shift
var frameShift = 103;

// Starting number
/*var theNumber = 1000000;*/
var startingWatts = 10000000;
var startDate = new Date('1/1/2010').getTime(); // date of the startingWatts
var additionalWattsPerDay = 1000000 / 365; // how many more watts to show, this time tomorrow

var elapsedMilliseconds = new Date().getTime() - startDate;
var daysSinceStart = elapsedMilliseconds / 1000 / 60 / 60 / 24;

var theNumber = 0;
var numberFromCookie = parseInt($.cookie('il_digits'));
	//console.log(numberFromCookie);
if (numberFromCookie > 0) {
	theNumber = numberFromCookie;
}
else {
	 theNumber = parseInt(startingWatts + (daysSinceStart * additionalWattsPerDay));
}

// Pace of counting in milliseconds
var pace = 500;

// Interval of setting cookie in milliseconds
var cookieInterval = 2000;

// Increment
var minimumIncrement = 1;
// code below calculates the actual increment.  anything < 1.0 won't animate
var increment = additionalWattsPerDay / ( 1000 * 60 * 60 * 24) * pace;
//console.log(increment);
increment = parseInt(increment);
if (increment < minimumIncrement) increment = minimumIncrement; // enforce minimum increment
//console.log(increment);

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

// Function that controls counting
function doCount(){
	var x = theNumber.toString();
	theNumber += increment;
	var y = theNumber.toString();
	rememberDigits(y);
	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){
	var digitsOld = splitToArray(x),
	digitsNew = splitToArray(y);
	for (var i = 0, c = digitsNew.length; i < c; i++){
		if (digitsNew[i] != digitsOld[i]){
		//	console.log(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
	var speed;
	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.
	for (var k = 0; k < animationFrames; k++){
		pos = pos - frameShift;
		if (k == (animationFrames - 1)){
			$("#d" + n).delay(speed).animate({'background-position': '0 ' + pos + 'px'}, 0, function(){
				// At end of animation, shift position to new digit.
				$("#d" + n).css({'background-position': '0 ' + initialPos[newDigit] + 'px'}, 0);
			});
		}
		else{
			$("#d" + n).delay(speed).animate({'background-position': '0 ' + pos + 'px'}, 0);
		}
	}
}

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

// Sets the correct digits on load
function initialDigitCheck(initial){
	var digits = splitToArray(initial.toString());
	for (var i = 0, c = digits.length; i < c; i++){
		$("#d" + i).css({'background-position': '0 ' + initialPos[digits[i]] + 'px'});
//		console.log("D" + i + "..." +  '0 ' + initialPos[digits[i]] + 'px');
	}
}

// Remember the digits every once in a while
function rememberDigits() {
	$.cookie('il_digits', theNumber, { expires: 1, path: '/'});
}

// Start it up
initialDigitCheck(theNumber);
setInterval(doCount, pace);
setInterval(rememberDigits, cookieInterval)

