/*------------------------------------------------------------------
Project:		Financial Crisis Inquiry Commission
Author:			Ryan Dudek
Last change:	2010-01-07
-------------------------------------------------------------------*/

$(document).ready(function() {
	
	// Open links to external sites in a new tab/window
	$("a[href^='http']").attr('target','_blank');
	
	// Add the clear class to any textfield you want to auto clear
	$('.clear').focus(function() { clearInputValue(this); });
	
	// Add an odd class to the lists
	$('.oddRows li:odd').addClass('odd');
	
	// Add a first class to the first column
	$('.column:first').addClass('first');
	$('.column:even').css({'clear':'both'});
	$('.accordion dt:first').addClass('first');
	$('.calendar dt').next().addClass('first');
	
	// If the articleList has a limit attribute, limit the list and create a View More link to show the rest
	$('.articleList').each(function() {
		if($(this).attr('limit')) {
			var list = $(this);
			var limit = list.attr('limit') - 1;
			hiddenArticles = $(list).children(':gt(' + limit + ')');
			hiddenArticles.hide();
			$(list).append('<li class="viewMore"><a href="#">View all...</a></li>');
		}
	});
	$('.viewMore a').click(function(e) {
		e.preventDefault();
		$(this).parent().parent().find(':hidden').show();
		$(this).hide();
	});
	
	
});


/* Tab setup and functionality
--------------------------------------------- */
function setupTabs() {
	$('.tabbedContent li a').click(function(e) {
		e.preventDefault();
		makeActiveTab(this);
		showTabData(this);
	});
}

function makeActiveTab(tab) {
	$(tab).parent().parent().find('li').removeClass('active');
	$(tab).parent().addClass('active');
}

function showTabData(tab) {
	var tabToShow = "#" + $(tab).attr('rel');
	$(tab).parent().parent().parent().find('.tab').hide();
	$(tabToShow).show();
}


/* Accordion
--------------------------------------------- */
function setupAccordion() {
	$('dl.accordion dt').click(function(e) {
		e.preventDefault();
		if($(this).attr('class') == "active") {
			$(this).next().slideUp('fast');
			$(this).removeClass('active');
			return;
		}
		collapseAccordion();
		showTheDefintionFor(this);
	});
	collapseAccordion();
	showTheDefintionFor($('.accordion dt:first'));
}
function collapseAccordion() {
	$('dl.accordion dd').hide();
	$('dl.accordion dt').removeClass('active');
}

function showTheDefintionFor(item) {
	$(item).addClass('active');
	$(item).next().slideDown('fast');
}


// This function takes an input, clears it's value on focus
// and sets it's value back if the user doesn't type anything
function clearInputValue(input) 
{
	var originalValue = $(input).val();
	$(input).val('');
	$(input).blur(function() {
		if($(this).val() == '') {
			$(this).val(originalValue);
		} else {
			$(this).unbind('blur');
		}
	})
}