var today = new Date();

if (!$.cookie("VisitDate")) {
	$.cookie("VisitDate", today);
}

var thisYear = today.getFullYear(); 
var fdty = new Date(thisYear, 0, 1); 
pVisit = timeElapsed($.cookie("VisitDate"))*4;
pYear = timeElapsed(fdty)*4;
vVisit = timeElapsed($.cookie("VisitDate")); /* + the amount of seconds elapsed since the start of the session */
vYear = timeElapsed(fdty); /* + the amount of seconds elapsed since the first day of the year */

$(document).ready( function() {
	setInterval("updateCounters()",0250);
	setInterval("swapCounters()",5000);
});

function timeElapsed(xkcd)
{
	var timeNow = new Date();
	var timeThen = Date.parse(xkcd);
	var timeElapsed;
	
	timeElapsed = timeNow - timeThen;

	return timeElapsed/1000;
}

function updateCounters() {
	
	pVisit = timeElapsed($.cookie("VisitDate"))*4;
	pYear = timeElapsed(fdty)*4;
	vVisit = timeElapsed($.cookie("VisitDate")); /* + the amount of seconds elapsed since the start of the session */
	vYear = timeElapsed(fdty); /* + the amount of seconds elapsed since the first day of the year */

	$("#pVisit").html(addCommas(Math.round(pVisit)));
	$("#pYear").html(addCommas(Math.round(pYear)));
	$("#vVisit").html(addCommas(Math.round(vVisit)));
	$("#vYear").html(addCommas(Math.round(vYear)));
	
}


var stat = 1;

function swapCounters() {
	
	$(".stat" + stat).slideUp("slow");
	if (stat < 4)
		stat++;
	else
		stat = 1;
	$(".stat" + stat).slideDown("slow");
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}
