$(document).ready(function(){
	
	addSelected();
	
	//add hover effect to navigation
	$('.nav').hover(hoverIn, hoverOut);
	
	//remove hover effect from selected link
	if($('.selected')){
		$('.selected').unbind();
		reveal();
	}
	
	
});

function addSelected() {
// *** Navigational Selector *** //	
//marks which link brought you to the current page

	//get the current page url
	var url = window.location.href;

	//display the string for debugging purposes
	//$('#content').append(url);
	
	//for each a element in the nav...
	$('#nav a').each(function(i){
		//get the href
		var href = $(this).prop('href');
		
		//add it to content for debugging purposes
		//$('#content').append('<p>' + i + ': ' + href + '</p>');
				
		//compare href to url
		if(href == url){
			//if they match, add class 'selected'
			$(this).addClass('selected');
			//and remove the link
			$(this).attr('href', '#');
		};
	});
		
		$('.selected').attr('title', 'You are here');
		
// *** End Navigational Selector *** //
}
		
// ** reveal sub-menus of selected ** //
function reveal(){
		var selected = $('.selected').parent().addClass('foundIt');
		
		if($('li.foundIt > ul.children')){
			$('li.foundIt > ul.children').addClass('visible');
			$('.visible').slideUp(0);	
			animateReveal();
		};
};

function animateReveal() {
	
	$('.visible').slideDown(750);
	
}

//** Hover effects **//

function hoverIn(){
	$('.nav').stop(false, true);
	$(this).css({backgroundColor: '#CBD095'});
	$(this).animate({
		backgroundColor: "#440",
		color: "#EEE"
	}, 500);
};

function hoverOut(){
	$(this).css({color: '#333'});
	$(this).animate({
		backgroundColor: '#CBD095',
		color: "#333"
	}, 250, backgroundFix($(this)));
};

function backgroundFix(){
	//alert("in background fix with: " + $(this));
	$('.nav').addClass('bgFix');
}
