(function($j){
$j(document).ready(function(){

	// sets last column of figures to align right
	$j('.accordion tr td table tr').find('td:last').css({textAlign: 'right'});

	/* Function to handle the show/hide buttons*/
	function showHide() {
		if($j('.accordion').find('table:visible').length === 0) {
			$j("#show-all").fadeTo(500, '1');
			$j("#hide-all").fadeTo(500, '.4');
		} else if ($j('.accordion').find('table:hidden').length === 0) {
			$j("#show-all").fadeTo(500, '.4');
			$j("#hide-all").fadeTo(500, '1');
		} else {
			$j("#show-all").fadeTo(500, '1');
			$j("#hide-all").fadeTo(500, '1');
		}
	}

	//show all button
	$j("#show-all").click(function() {
		$j('.accordion').find('table').show();
		$j('.accordion>tbody>tr>td').addClass('open');
		showHide();
	});

	//hide all button
	$j("#hide-all").click(function() {
		$j('.accordion').find('table').hide();
		$j('.accordion>tbody>tr>td').removeClass('open');
		showHide();
	});
	showHide();
	$j(".accordion td tr:nth-child(odd)").addClass("even");
	// 2009 Community Impact Report grant links
	$j(".body-2009-grants .accordion td td, .body-2009-grants .accordion td tr").click(function() { return false; });
	$j(".body-2009-grants .accordion td td a").click(function() {
		window.open( $j(this).attr('href') ); //make sure the links click through
	});


	// 2009 CIR Accordion
	$j('.body-2009-grants .accordion>tbody>tr>td').click(function() {
		var child = $j(this).children('table');
		if (child.is(":visible")) {
			child.slideUp('medium', function() {
				showHide();
			});
			$j(this).removeClass('open');
		} else {
			child.slideDown('medium', function() {
				showHide();
			});
			$j(this).addClass('open');
		}
	});
	// 2010 CIR Accordion
	$j('#CIR-2010-grant-list span.area-title').click(function() {
		var parentCell = $j(this).parent();
		var child = parentCell.children('table');
		if (child.is(":visible")) {
			child.slideUp('medium', function() {
				showHide();
			});
			parentCell.removeClass('open');
		} else {
			child.slideDown('medium', function() {
				showHide();
			});
			parentCell.addClass('open');
		}
	});

	if($j("#right-col").height() < $j("#left-col").height())
		$j("#right-col").css({height: ($j("#left-col").height())});//make sure the right col is as tall as left one

	initialHeight = $j("#right-col").height();

	//Letter sidebar boxes
	$j("a.sidebar").click(function() {
		if (!($j(this).hasClass("current"))) {
			$j("#box").show();
			var rel = $j(this).attr('rel');
			var content = $j("#hidden-captions li#" + rel + "").html(); //get corresponding content

			$j("#rounded-box").html(content);// insert content

			/*position box*/
			var colTop = $j("#right-col").offset().top;
			var scrollTop = $j(window).scrollTop();

			if (scrollTop < colTop)
				var boxTop = 0;
			else
				var boxTop = scrollTop - colTop + 15;

			var boxHeight = $j("#box").height();
			var colHeight = $j("#right-col").height();

			if(initialHeight < boxHeight)
				$j("#right-col").css({height:boxHeight});
			else
				$j("#right-col").css({height:initialHeight});

			if(boxTop + boxHeight > colHeight)
				$j("#box").css({paddingTop : '0', bottom:20});
			else
				$j("#box").css({paddingTop:boxTop, bottom : 'auto'});
			$j("a.sidebar").removeClass('current');
			$j(this).addClass('current');
		}
	});

	$j("a.sidebar").click(function() {return false;}); //hijack sidebar links

	jQuery("#financials-landing a").each(function(z) {
		jQuery(this).bind("mouseover", function() {
			var idx = z;
			$j("#right-col .block:eq(" + idx + ")").addClass('current-block');
		});
		jQuery(this).bind("mouseout", function() {
			var idx = z;
			$j("#right-col .block:eq(" + idx + ")").removeClass('current-block');
		});
	});

	// CIR past reports dropdown
	$j("#past-CIRs ul").hide();
	$j("#past-CIRs").hover(function() {
		$j(this).find("ul").slideDown();
	}, function() {
		$j(this).find("ul").slideUp();
	});
});
})(jQuery);
