var menuTimerId = null;

$(document).ready(function() {
	$('ul#nav-dropdown li a.heading').mouseover(function() {
		
		var heading = $(this);	     
	    clearTimeout(menuTimerId);
		heading.css('outline','none');
		if(!heading.parent().hasClass('over')) {

			$('ul#nav-dropdown li.over ul').each(function(){
				$(this).hide();
				$(this).parent().removeClass('over');			
				$(this).parent().find('a').removeClass('on');
			});
			
			$('ul#nav-dropdown li.over').each(function(){
				$(this).removeClass('over');
				$(this).find('a').removeClass('on');
			});
					
			heading.siblings('ul').show();

			$(this).parent().addClass('over');
			$(this).addClass('on');
		}
		return false;
	});
	
	$('ul#nav-dropdown ul.sub-navigation li a').mouseover(function() {
	var heading = $(this);	   
		if(!$(this).parent().hasClass('over')) {
			$(this).parent().siblings('li').find('ul.sub-navigation').each(function(){
				$(this).hide();
			});
			
			$(this).parent().siblings('li.over').each(function(){
				$(this).removeClass('over');
				$(this).find('a').removeClass('on');
			});		
			
			$(this).siblings('ul.sub-navigation').show();
			$(this).siblings('ul.sub-navigation').find('li').each(function(){
				$(this).removeClass('over');
				$(this).find('a').removeClass('on');
			});
			$(this).parent().addClass('over');	
			$(this).addClass('on');	
		}
	});
		
	$('ul#nav-dropdown li ul li a').mouseover(function() {
		clearTimeout(menuTimerId);
	});
	
	$('ul#nav-dropdown').mouseleave(function(){
		isOver = false;
		menuTimerId = setTimeout(function(){

				$('ul#nav-dropdown li.over ul').each(function(){
					$(this).fadeOut('fast',function(){
						$(this).parent().removeClass('over');
						$(this).parent().find('a').removeClass('on');
					});
				});
				
				$('ul#nav-dropdown li.over').each(function(){
					$(this).removeClass('over');
					$(this).find('a').removeClass('on');
				});

				clearTimeout(menuTimerId);
			}
			
			, 800);
	});

});
