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