jQuery(function(){

// FEATURED STORY TABS --------------------------------------------------
var tabListLinks = jQuery('.numLink a');
var prevLink = jQuery('#previousTab');
var nextLink = jQuery('#nextTab');
var tabs = jQuery('.tab');
var tabIDs = [];
tabs.each(function(){
	tabIDs.push(this.id);
});
var defaultTab = tabIDs[0];
var currentID;
var currentIndex;

// show the tab with the given id
function showTab(id) {
	if (id == currentID) return;
	tabs.hide();
	jQuery('#' + id).show();
	tabListLinks.removeClass('active');
	jQuery('a[href="#' + id + '"]').addClass('active');
	currentID = id;
	for (var i = 0, l = tabIDs.length; i < l; i++) {
		if (tabIDs[i] == currentID) {
			currentIndex = i;
			break;
		}
	}
}

// enable numbered tab link functionality
tabListLinks.click(function(){
	showTab(jQuery(this).attr('href').substr(1));
	return false;
});

prevLink.click(function(){
	showTab(tabIDs[currentIndex <= 0 ? tabIDs.length - 1 : currentIndex - 1]);
	return false;
});

nextLink.click(function(){
	showTab(tabIDs[currentIndex >= tabIDs.length - 1 ? 0 : currentIndex + 1]);
	return false;
});

// show the default tab
showTab(defaultTab);

// DEBT COUNTER -------------------------------------------------------------------------------------

var debtContainer = jQuery('#debtContainer');

// format an integer with commas 
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;
}

function updateCounter(){
	var dateDiff = (new Date()).getTime() - debtStartDate;
	var newAmount = Math.round(debtStartAmount + (debtRate * dateDiff));
	debtContainer.html('$' + addCommas(newAmount.toString()));
}

updateCounter();

window.setInterval(function(){ updateCounter(); }, debtUpdateEvery);

});