$(document).ready(function(){

/* The following code implements animation of the main menu -
	by removing control of the drop down container from CSS, ... */
	$('#top-nav li .nav-container').css({
		display: "none",
		left: "auto"
	});
/*	... then reapplying it when a hover intention is detected. */
	$('#top-nav li').hoverIntent(function() {
		$(this)
			.find('div')
			.stop(true, true)
			.slideDown('fast');
	}, function() {
		$(this)
			.find('div')
			.stop(true,true)
			.fadeOut('fast');
	});

/* The following code implements better hover & click effects in the drop-down menus -
	by adding a new class that effectively replaces the CSS :hover pseudoclass, ... */

/*
	$('#top-nav ul li .nav-container li a').mouseover(function() {
		$(this).addClass('nav-dropdown-hover');
	});
	$('#top-nav ul li .nav-container li a').mouseout(function() {
		$(this).removeClass('nav-dropdown-hover');
	});
*/

/*********************************************************************************************************************************************************/
/* Toggle sidebar  */

/* When the page loads, it has no sidebar toggle buttons, we add the toggle buttons so they are only available if Javascript is actually enabled */
	$('<ul><li><a id="sidebar-show" href="#">Show Sidebar</a></li><li><a id="sidebar-hide" href="#">Hide Sidebar</a></li></ul>').prependTo('#sidebar-toggle-btn-locn');

/*********************************************************************************************************************************************************/
/* Toggle supplementary information (e.g. FAQ answers)  */

/* When the page loads, it has no toggle button and all page content is visible (for users without Javascript), 
	we add the toggle-on button and hide the toggle-able content */
	$('<a><img src="../../z-site-infrastructure/images/site-sundry/toggle-on-11.png" class="toggle-btn-img" alt="toggle button - show/hide additional information" /></a> ')
		.prependTo('.toggle-btn-locn')
		.addClass('toggle-btn-link');
	$('.toggle-content')
		.hide();
/* click event on a toggle button propagates up to the list item that contains it - this will trigger toggling of the button and content contained in that list item */	
	$('.toggle-btn-link').click(function() {
/* detect the click and store the prior state of the content associated with the clicked button */
		if ($(this)
				.parent().parent().parent().children('.toggle-content')
				.is(':visible')) {
			priorState = "visible";
		} else {
			priorState = "invisible";
		}
/* set all toggle buttons (images with the identifying class) to "on" and hide all toggleable content */
		$('img.toggle-btn-img')
			.replaceWith('<img src="../../z-site-infrastructure/images/site-sundry/toggle-on-11.png" class="toggle-btn-img" alt="toggle button - show/hide additional information" />');
		$('.toggle-content')
			.fadeOut('fast');
/* if the prior state was invisible, then set the clicked button to "off" and show the content associated with it */
		if (priorState == "invisible") {
			$(this)
				.children('img.toggle-btn-img')
				.replaceWith('<img src="../../z-site-infrastructure/images/site-sundry/toggle-off-11.png" class="toggle-btn-img" alt="toggle button - show/hide additional information" />');
			$(this)
				.parent().parent().parent().children('.toggle-content')
				.slideDown('fast');
		}
	});

});
