var storedBackground;

jQuery(document).ready(function(){
	/* This covers all the nav functions for the site in one file */
	
	
	/* Left Menu Drop-down */
	$('#SideNav .HasMenu').hover(
		function(){
			$(this).find('.slidingmenu').show();
		},
		function(){
			$(this).find('.slidingmenu').hide();
		}
	);

	/* Top Menu Additional Image */
	storedBackground = $('#PrimaryNav > ul > li').css('backgroundImage');

	/* Top Menu Drop-down */
	$('#PrimaryNav li').hover(
		function(){
			$(this).find('.Sub').show();
			topNavButton_On($(this));
		},
		function(){
			$(this).find('.Sub').hide();
			topNavButton_Off($(this));
		}
	);
	
	$('#PrimaryNav .Sub').hover(
		function(){
			$(this).closest('li').find('a').first().addClass('Active');
		},
		function(){
			if(!$(this).closest('li').find('a').hasClass('CurrentPage')){
				$(this).closest('li').find('a').first().removeClass('Active');
			}
		}
	);
	
	/* Sort out current page */
	$("#PrimaryNav .CurrentPage").first().each(
		function(){
			var prev = $(this).closest('li').prev('li');
			if(prev.length > 0){
				if(!$(prev).find('a').first().hasClass('Active')){
					$(prev).css('backgroundImage','none');	
				}
			}
		}
	);
	

});

function topNavButton_On(element){
	/* Change the background on the previous link if it exists and it's not set to active */
	var prev = $(element).prev('li');
	if(prev.length > 0){
		if(!$(prev).find('a').first().hasClass('Active')){
			$(prev).css('backgroundImage','none');	
		}
	}
}

function topNavButton_Off(element){
	var prev = $(element).prev('li');
	if(prev.length > 0){
		if(!$(element).find('a').hasClass('CurrentPage')){
			$(prev).css('backgroundImage',storedBackground);	
		}
	}
}
