$(document).ready(function() {

		
	// Recommended Reads chevron 		
	function toggleIcon(e) {
		$(e.target)
			.prev('.rr-heading')
			.find('.glyphicon')
			.toggleClass('glyphicon-chevron-down glyphicon-chevron-up');
	}
	
	$('.rr-panel').on('hidden.bs.collapse', function(e) {
			toggleIcon(e);
		});
	$('.rr-panel').on('shown.bs.collapse', function(e) {
			toggleIcon(e);
		
		});
	
	// Related documents
	$('.MCDropDownHead').on('click', function() {
		alert(1);
			$('.MCDropDownBody').slideToggle();
			$('.MCDropDownBody').show();
		});		
	
	
	$('.toc .toc-menu').each(function() {
			$(this).before($('<button class="btn-expand" type="button">COLLAPSE</button>'));
		});	
	
	
	// Add TOC "Expand" button handler.
	$(".btn-expand").on("click", function() {
			var $button = $(this);
	
			var $toc_levels = $('.toc').find('li.has-children');
			
			// Open all toc_levels
			if ($button.hasClass('closed')) {
				$toc_levels.removeClass('menuArrowRight');
				$toc_levels.addClass('menuArrowDown');
				$children = $toc_levels.find('ul');
				$children.css({'display':'block'});
				//	$children.addClass('open');
	
				// Close all toc_levels
			} else {
				$children = $toc_levels.find('ul');
				$children.css({'display':'none'});
				//	$childen.removeClass('open');
				$toc_levels.removeClass('menuArrowDown');
				$toc_levels.addClass('menuArrowRight');	
			}
	
			// Change button text
			if ($(this).html() == 'COLLAPSE') {
				$(this).html('EXPAND');
			} else {
				$(this).html('COLLAPSE');
			}
	
			// Change button status
			$button.toggleClass('closed');
		})
	
		/*
		 * Resizes content area based on the "Recommended Reads" content,
		 * to make it visible when sliding-down.
		 */
		function resizeContentArea() {
			var $tocRecommendedReadsBodyElement = $(".toc-menu ul:first");
				tocTopMargin = parseInt($(".toc:first").css('margin-top')),
				tocContentsHeader = $(".toc-menu").height(),
				tocContentsBody = $tocRecommendedReadsBodyElement.height(),
				tocRecommendedReads = $(".rr-panel").height(),
				tocRecommendedReadsBody = $(".rr-panel table").height(),
				tocHeight = tocTopMargin + tocContentsHeader + tocContentsBody + tocRecommendedReads;

			if (getContentAreaHeight() < tocHeight) {
				$tocRecommendedReadsBodyElement.css('max-height', tocContentsBody-tocRecommendedReadsBody);
			} else {
				$tocRecommendedReadsBodyElement.css('max-height', getContentAreaHeight()-tocTopMargin-tocContentsHeader-tocRecommendedReads);
			}
		}
	
		/*
		 * Event handler to increase the content area height 
		 * when the Recommended-Reads area is expanded (after 400 ms).
		 */
		$(".panel-title").click(function() {
			setTimeout(function(){resizeContentArea()}, 400);
		});
	
});