// FUNCTION: accordionInit - Controls accordionList
var accordionInit = function () {
	// check that accordion class exists
	if (!$('dl.accordion').length) {
		return;
	}
	// Get all accordions that exist
	$('dl.accordion').each(function() {
		// Set dd display to none
		$(this).find('dd').css('display','none');
		// Reveal first item
		$(this).find('dt:first a').addClass('accordionExpanded').end().find('dd:first').show();
		// Remove border from last item
		$(this).find('dt:last a').addClass('noBorder');
	});
	// Event listener for click
	$('dl.accordion dt a').click(function() {
		// Get parent <dl>
		var $dl = $(this).parents('dl:first');
		// Get the following <dd>
		var $dd = $(this).parent('dt').next('dd');
		// Check visibility
		if ($dd.is(':hidden')) {
			// FUNCTION slideUpContent - slideUp siblings of newly displayed content
			var slideUpContent = function () {
				var $sibs = $dd.siblings('dd:visible');
				$sibs.slideUp('fast');
			};
			// Expand clicked <dt>'s <dd>, hide the others
			// Full Expand - Then Detract
			//$dd.slideDown('fast', slideUpContent);
			// Expand / Detract at same time
			$dd.slideDown('fast').siblings('dd:visible').slideUp('fast');
			/*--*/
			// Change on state
			$(this).addClass('accordionExpanded').parent('dt').siblings('dt').find('a').removeClass('accordionExpanded');
		}
		// Nofollow anchors
		this.blur();
		return false;
	});
};

// FUNCTION: tabPosition - Reposition tab relative to top pods
var tabPosition = function () {
	var $tabGroup = $('#researchSection .researchGroup.top.tabTop');
	var $tabLoc = $tabGroup.offset().top;
	var $subPods = $('.subSidebars');
	var subHeight = 0;
	var tallest = 0;
	$subPods.each(function() {
		subHeight = $(this).height();
		if (subHeight > tallest) {
			tallest = subHeight;
		}
	});
	var $tabMargin = '-' + (($tabLoc - tallest) - 28) + 'px';
	$tabGroup.css('marginTop',$tabMargin);
};

// FUNCTION: tabInit - Controls tabWrapper
var tabInit = function () {
	// check that tabs class exists
	if (!$('ul.tabs').length) {
		return;
	}
	// Get all tabContent items and reveal content area
	$('div.tabContent').each(function() {
		// Set tab_items display to none
		$(this).find('div.tab_item').hide();
		// Reveal first item
		$(this).find('div.tab_item:first').show();
		// Set Unique ID for each tab_item
		$(this).find('div.tab_item').each(function(itemCount) {
			$(this).attr('id','tabItem_' + itemCount);
		});
		// Set Unique Anchor matching tab_item ID
		$('ul.tabs a').each(function(itemCount) {
			$(this).attr('href','#tabItem_' + itemCount);					 
		});
	});
	// Event listener for click on tabs
	$('ul.tabs a').click(function() {
		// If not current tab
		if (!$(this).hasClass('currentTab')) {
			// Change current indicator
			$(this).addClass('currentTab').parent('li').siblings('li').find('a.currentTab').removeClass('currentTab');
			// Show target, hide others
			$($(this).attr('href')).show().siblings('div.tab_item').hide();
		}
		// Nofollow anchors
		this.blur();
		return false;
	});
	//commented out for testing purposes
	//tabPosition();
};

$(document).ready(function() {
	accordionInit();
	tabInit();
});