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 (console) console.log('Showing id ' + 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);

});

function loadSplash(){
	jQuery('#bottomRibbon').hide();
	//jQuery('#body').css('position','static');
	jQuery('#screenMask').remove().appendTo(document.body)
		.show()
		.height(jQuery(document).height())
		.width(jQuery(document).width())
		.click(function(){
			jQuery(this).hide();
			jQuery('#splash').hide();
			jQuery('#bottomRibbon').show();
			//jQuery('#body').css('position','relative');
		});
	jQuery('#splash').remove().appendTo(document.body).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;
	});
	var emailInput = jQuery('#splashEmailAddress');
	var emailDefaultText = 'Enter Email Address';
	emailInput.focus(function(){
		if (emailInput.val() == emailDefaultText) {
			emailInput.val('');
		}
	}).blur(function(){
		if (emailInput.val() == '') {
			emailInput.val(emailDefaultText);
		}
	});
	var zipCodeInput = jQuery('#splashZipCode');
	var zipCodeDefaultText = 'Enter Zip Code';
	zipCodeInput.focus(function(){
		if (zipCodeInput.val() == zipCodeDefaultText) {
			zipCodeInput.val('');
		}
	}).blur(function(){
		if (zipCodeInput.val() == '') {
			zipCodeInput.val(zipCodeDefaultText);
		}
	});
}
