jQuery(function($){
	// hide all sub navs 
	$('.side-nav dd ul').each(function() {
		$(this).hide();
	});
	
	// change minus icons to pluses
	$('.side-nav dt').each(function() {
		$(this).find('a').addClass('side-nav-plus')
	});	
		
	// determine which page and show relevant menu options
	
	// We now should have the unique identifier as a string which we can use to search the navigation to find the link to the page we are on
	
	var link;
	
	var pathname = '';
	var pathels = window.location.pathname.split("/");
	var found = false;
	var pathend = '';
	
	do {
		pathname = pathels.join("/") + pathend;
		// test whether the page we are on exists as a link in any submneu 
		if ($('.side-nav a[href$="'+pathname+'"]').length > 0 ) {
			// if it does
			link = $('.side-nav a[href$="'+pathname+'"]'); //find the link which links to the page we are on
			link.parent().addClass("selected"); // highlight the link
			link.parent().parent().parent().prev().find('a').toggleClass('side-nav-plus');  // find the title of this section of the navigation and change the + image to -
			link.parent().parent().toggle(); // toggle the state so that it's open when we hit the page
			found = true;
		}
		last = pathels.pop();
		pathend = "/";
	} while (!found && last);
	
	if (!found) {
		var menu = $('.side-nav dd ul:first'); // find the first submenu
		menu.parent().prev().find('a').toggleClass('side-nav-plus');  // find the title of this section of the navigation and change the + image to -
		menu.toggle(); // toggle the state so that it's open when we hit the page
	}
	
	
	// show all dd elements under selected
	$('.side-nav dt a').click(function(e) {
		$(e.target).toggleClass('side-nav-plus');
		$(e.target).parent().next().find('ul').toggle("blind");
		return false;
	});
	
});

