jQuery(function(){

// column resize
//var welcomeMessage = jQuery('#welcomeMessage');
var meetSheriff = jQuery('#meetSheriffNugent');
var richsBlog = jQuery('#recentBlogs');

function resize(){
	//if (richsBlog.height() < meetSheriff.height()) {
		richsBlog.height(meetSheriff.height());
	//}
	//else {
	//	meetSheriff.height(richsBlog.height());
	//}
}

resize();

// blog tabs
var container = jQuery('#recentBlogs');
var tabs = container.find('.tab');
var previous = container.find('.previous a');
var next = container.find('.next a');
var bullets = container.find('.bullet a');
var rotationInterval;
var rotationPeriod = TabRotationPeriod * 1000; // ms

var currentID;
var defaultID = tabs.eq(0).attr('id');
var currentIndex;

function showTab(id) {
	if (currentID == id) return;
	tabs.hide();
	tabs.filter('#' + id).show();
	bullets.removeClass('active');
	bullets.filter('[href=#' + id + ']').addClass('active');
	currentID = id;
	for (var i = 0, s = tabs.size(); i < s; i++) {
		if (tabs.eq(i).attr('id') == id) {
			currentIndex = i;
			break;
		}
	}
	resize();
}

function startRotation(){
	rotationInteval = window.setInterval(function(){
		showTab(tabs.eq(currentIndex == tabs.size() - 1 ? 0 : currentIndex + 1).attr('id'));
	}, rotationPeriod);
}
function stopRotation(){
	if (rotationInterval) {
		window.clearInterval(rotationInterval);
	}
	rotationInterval = null;
}

bullets.click(function(){
	stopRotation();
	showTab(jQuery(this).attr('href').substr(1));
	return false;
});

previous.click(function(){
	stopRotation();
	showTab(tabs.eq(currentIndex == 0 ? tabs.size() - 1 : currentIndex - 1).attr('id'));
	return false;
});
next.click(function(){
	stopRotation();
	showTab(tabs.eq(currentIndex == tabs.size() - 1 ? 0 : currentIndex + 1).attr('id'));
	return false;
});
tabs.click(function(){
	stopRotation();
});

showTab(defaultID);

if (rotationPeriod != 0 && tabs.size() > 1) startRotation();

});

$(document).ready(function(){
	/* ********************************************** DEBT COUNTER ****************************************** */

	var DebtCounter = jQuery('#debtCounter');
	var DollarsPerMilli = 4140000000 / (1000 * 60 * 60 * 24);
	var StartDebt = 13065142202808.88;
	var StartTime = Date.UTC(2010, 5, 4, 13, 42, 25);
	var CurrentDebt;
	var RefreshCount = 100; // milliseconds
	
	function addCommas(n) {
		var commasToAdd = Math.floor((n.length - 1) / 3);
		for (var i = 1, l = n.length; i <= commasToAdd; i++) {
			n = n.substr(0, l - (3 * i)) + ',' + n.substr(l - (3 * i));
		}
		return n;
	}
	
	window.setInterval(function(){
		CurrentDebt = StartDebt + (((new Date()).getTime() - StartTime) * DollarsPerMilli);
		DebtCounter.html('$' + addCommas(Math.round(CurrentDebt).toString()));
	}, RefreshCount);
});

function loadSplash(){
	jQuery('#screenMask')
		.show()
		.height(jQuery(document).height())
		.width(jQuery(document).width())
		.click(function(){
			jQuery(this).hide();
			jQuery('#splash').hide();
		});
	jQuery('#splash').show();
	document.getElementById('splash').style.left = (jQuery(document).width() - jQuery('#splash').width()) / 2 +'px' ;
	jQuery('#skipToSite').click(function(){
		jQuery('#screenMask').hide();
		jQuery('#splash').hide();
		return false;
	});
}
