var target = 'July 15, 2012';
var initialPos = [-2700, -2400, -2100, -1800, -1500, -1200, -900, -600, -300, 0];
var initialMidPos = [-1500, -1200, -900, -600, -300, 0];
var initialSmallPos = [-600, -300, 0];
var classNames = ['days', 'hours', 'minutes', 'seconds'];
var idNames = ['d', 'h', 'm', 's'];
var animationFrames = 5;
var frameShift = 50;

//target = (window.location.search == "") ? target : window.location.search.substring(6);

var now = new Date().getTime();
var end = Date.parse(target);
if (end < now){
	target = 'April 15, 2011';
	end = Date.parse(target);
}
var theDiff = end-now;
var theDiffString = getTimeString(theDiff);

var increment = 1000;
var pace = 1000;

function doCount(){
	var x = getTimeString(theDiff);
	theDiff -= increment;
	var y = getTimeString(theDiff);
	
	if (theDiff <= 0){}
	else{
		digitCheck(x,y);
	}
}

function digitCheck(x,y){
	var a = x.split(':');
	var b = y.split(':');
	for (var i = 0, c = a.length; i < c; i++){
		if (a[i].length < 2) a[i] = '0' + a[i];
		if (b[i].length < 2) b[i] = '0' + b[i];
		var countA = a[i].toString().length;
		var countB = b[i].toString().length;
		if (countB < countA) removeDigit(i, countB);
		for (var j = 0; j < countB; j++){
			if (b[i].charAt(j) != a[i].charAt(j)){
				var which = idNames[i] + j;
				animateDigit(which, a[i].charAt(j), b[i].charAt(j));
			}
		}
	}
	setTimeout(doCount, pace);
}

function getTimeString(d){
	var diff = d;
	var days = Math.floor(diff / 86400000);
	diff -= days * 86400000;
	var hours = Math.floor(diff / 3600000);
	diff -= hours * 3600000;
	var minutes = Math.floor(diff / 60000);
	diff -= minutes * 60000;
	var seconds = Math.floor(diff / 1000);
	return days + ':' + hours + ':' + minutes + ':' + seconds;
}

function getPos(id, digit){
	if (id == 's0' || id == 'm0'){
		return initialMidPos[digit];
	}
	else if (id == 'h0'){
		return initialSmallPos[digit];
	}
	else{
		return initialPos[digit];
	}
}

function animateDigit(which, oldDigit, newDigit){
	var speed = 80;
	var pos = getPos(which, oldDigit);
	var newPos = getPos(which, newDigit);
	for (var k = 0; k < animationFrames; k++){
		pos -= frameShift;
		if (k == (animationFrames - 1)){
			$("#" + which).delay(speed).animate({'background-position': '0 ' + pos + 'px'}, 0, function(){
				$("#" + which).css({'background-position': '0 ' + newPos + 'px'}, 0);
			});
		}
		else{
			$("#" + which).delay(speed).animate({'background-position': '0 ' + pos + 'px'}, 0);
		}
	}
}

function removeDigit(i,count){
	$("li#" + idNames[i] + count).remove();
}

function initialDigitCheck(initial){
	var a = initial.split(':');
	for (var i = 0, c = a.length; i < c; i++){
		if (a[i].length < 2) a[i] = '0' + a[i];
		var count = a[i].toString().length;
		var html = '<div class="set"><ul class="' + classNames[i] + '">';
		var bit = count;
		for (var j = 0; j < count; j++){
			bit--;
			html += '<li id="' + idNames[i] + j + '"></li>';
			if (bit != 0 && bit != (count) && bit % 3 == 0) html += '<li class="comma"></li>';
		}
		html += '</ul><h2>' + classNames[i].toUpperCase() + '</h2>';
		if (i != 3) html += '</div>';
		$("#counter").append(html);
	}
	
	for (var n = 0, cn = a.length; n < cn; n++){
		count = a[n].toString().length;
		for (var m = 0; m < count; m++){
			var thisID = idNames[n] + m;
			var thisPos = getPos(thisID, a[n].charAt(m));
			$("#" + idNames[n] + m).css({'background-position': '0 ' + thisPos + 'px'});
		}
	}

	doCount();
}


