$(function(){
	$("#top-search-submit").click(function(){  
		$("#Under_Search form").submit();
		return false;
	});

	var startPos;
	var selectedLink;
	
	// Hover behavior for all level 0 gray links
	$("#Level_0 > div > ul > li > *").hover(
		function(menu) {
			selectedLink = this;
			startPos = menu.pageY;
			window.status = startPos;

			// Change all level 0 gray link backgrounds to default color and close all submenus
			$("#Level_0 > #Gray > ul > li > *").each( 
				function(i) {
					$(this).css("background-color","#5c5c5c");
					$( "#Level_1_" + $(this).attr("id") ).hide();
				}
			);	
			
			
			// Change all level 0 red link backgrounds to default color and close all submenus
			$("#Level_0 > #Red > ul > li > *").each( 
				function(i) {
					$(this).css("background-color","");
					$( "#Level_1_" + $(this).attr("id") ).hide();
				}
			);			
			
			
			// Show the appropriate submenu and highlight the link
			$( "#Level_1_" + $(this).attr("id") ).show();

			
			
			// Change the level 0 link background appropriately
			if( $(this).attr("id") == "Marketplace" || $(this).attr("id") == "Contests" )
				$(this).css("background-color","#af0000");			
			else
				$(this).css("background-color","#424242");
				
			
            return false;
		},function(e){
			
			// header fix
//			$().mousemove(function(e) {
				if( e.pageY < $("#Level_0").offset().top ) { 

					$( "#Level_1_" + $(selectedLink).attr("id") ).hide();
					
					// Change level 0 link background to default and close the submenu
					if( $(selectedLink).attr("id") == "Marketplace" || $(selectedLink).attr("id") == "Contests" )
						$(selectedLink).css("background-color",""); 
					else
						$(selectedLink).css("background-color","#5c5c5c"); 
				}
//			});
			
		}
	);


	var linkName;
	
	// Submenu hover behavior (covers all submenus)
	$(".Submenu").hover(
		function() {
			
			// Figure out the link the user hovered over to make this menu pop up
			linkName = $(this).attr("id").split("_")[2];
			
			return false;
		},
		
		function() {

			// Change level 0 link background to default and close the submenu
			if( linkName == "Marketplace" || linkName == "Contests" )
				$( "#" + linkName ).css("background-color",""); 
			else
				$( "#" + linkName ).css("background-color","#5c5c5c"); 
			
			
			// Close the submenu
			$(this).hide();
			
			return false;
		}
		
	);

});