/**
 * JavaScript to load for the majority of pages
 *
 * Requires jQuery JavaScript library
 */

/**** Side navigation ****/
/**
 * Controls the display of the sub-lists in the side navigation bar
 *
 * @param object options settings to use with the function
 * @return string
 */
jQuery.fn.toggleNavSubList = function(options)
{
	// Use appropriately named variable.
	var subListLinks = this;

	// Standard strings used during toggle.
	var expandMessage = 'Click to show the \'{title}\' topics';
	var contractMessage = 'Click to hide the \'{title}\' topics';
	var expandPrefix = '\u002B\u00A0';   // '+&nbsp;'
	var contractPrefix = '\u2212\u00A0'; // '&minus;&nbsp;'
	
	// Load default settings then any overrides.
	var settings = jQuery.extend({
		mode: 'toggle'
	}, options);

	return subListLinks.each(function()
	{ 
		var subListLink = jQuery(this);
		switch (settings.mode)
		{
			// Initial setup
			case 'init' :
				// Hide sub-list, add text prefix and title attribute to toggle link.
				subListLink.next('ul').hide();
				subListLink.html('<span>' + expandPrefix + '<\/span>' + subListLink.html());
				subListLink.attr('title', expandMessage.replace('{title}', subListLink.text().substr(2)));
				break;

			// Expand/contract sub-list
			case 'toggle' :
				// Perform the view toggle.
				if (settings.quick)
				{
					subListLink.next('ul').toggle();
				}
				else
				{
					subListLink.next('ul').slideToggle('normal');
				}
	
				// Text prefix node
				var togglePrefixSpan = subListLink.find('span:first');
	
				// Toggle the prefix and title attribute of the toggle link
				if (togglePrefixSpan.text() == expandPrefix)
				{
					togglePrefixSpan.text(contractPrefix);
					subListLink.attr('title', contractMessage.replace('{title}', subListLink.text().substr(2)));
				}
				else if (togglePrefixSpan.text() == contractPrefix)
				{
					togglePrefixSpan.text(expandPrefix);
					subListLink.attr('title', expandMessage.replace('{title}', subListLink.text().substr(2)));
				}
				break;
		}
	});
};

/**
 * Initialise the side navigation scripts
 */
function initSideNav()
{
	if ($('#sideNav').length == 1)
	{
		// Make sure to not use original styles for the moment
		//$('#sideNav').attr('id', 'sideNav2');
		
		// Find the sublists
		var subListLinks = $('#sideNav li.selectedSection li.subSection > a');
	
		// Initialise any sub-lists
		subListLinks.toggleNavSubList({mode: 'init'});
	
		// Add sub-list toggle click event
		subListLinks.click(function() {
			$(this).toggleNavSubList();
			return false;
		});

		// For each link in the selected section
		var sectionLinks = $('#sideNav li.selectedSection a').each(function()
		{
			var pagePath = window.location.pathname;
			var linkPath = $(this).attr('href');
			
			pagePath = pagePath.substr(0, pagePath.lastIndexOf('/')+1);
			linkPath = linkPath.substr(0, linkPath.lastIndexOf('/')+1);

			// Initially check that the link path is within the actual page path.
			//if (pagePath.search(linkPath) > -1)
			//if (linkPath.search(pagePath) > -1)
			if (linkPath == pagePath)
			{
				// Highlight only if the immediate parent list item is NOT a sub-section wrapper
				if ($(this).parent('li.subSection').length == 0)
				{
					$(this).addClass('currentPage');
				}

				// If the link is within a sub-section, expand the sub-section to make the link visible.
				if ($(this).parent('li').parent('ul').parent('li.subSection').length == 1)
				{
					$(this).parent().parent().prev('a').toggleNavSubList({quick: true});
				}
			}
		});
	}
}
/**** /Side navigation ****/

/**** Page loaded ****/
$(function()
{
	initSideNav();
});
/**** /Page loaded ****/