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

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

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

	showHide();

	$(".accordion td tr:nth-child(odd)").addClass("even");

	$(".accordion td td, .accordion td tr").click(function() {
		return false;
	});

	$(".accordion td td a").click(function() {
        window.open( $(this).attr('href') ); //make sure the links click through
	});
	
	//Accordion
	$('.accordion>tbody>tr>td').click(function() {
		var child = $(this).children('table');
		if (child.is(":visible")) {
			child.slideUp('medium', function() {
				showHide();					
			});
			$(this).removeClass('open');
		} else {
			child.slideDown('medium', function() {
				showHide();			
			});
			$(this).addClass('open');			
		}
	});
	
	//$( '#box' ).scrollFollow();	
	if($("#right-col").height() < $("#left-col").height())
		$("#right-col").css({height: ($("#left-col").height())});//make sure the right col is as tall as left one

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

	//$("#box").hide(); //hide box onload

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

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

			/*position box*/
			var colTop = $("#right-col").offset().top;
			var scrollTop = $(window).scrollTop();
			
			if (scrollTop < colTop)
				var boxTop = 0;
			else
				var boxTop = scrollTop - colTop + 15;	
			
			var boxHeight = $("#box").height();
			var colHeight = $("#right-col").height();

			if(initialHeight < boxHeight)
				$("#right-col").css({height:boxHeight});
			else
				$("#right-col").css({height:initialHeight});				
			
			if(boxTop + boxHeight > colHeight)
				$("#box").css({paddingTop : '0', bottom:20});
			else
				$("#box").css({paddingTop:boxTop, bottom : 'auto'});
			$("a.sidebar").removeClass('current');		
			$(this).addClass('current');
		}
	});
	
	$("a.sidebar").click(function() {return false;}); //hijack sidebar links

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

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