//location of cursor
var hovering_button = false;
var hovering_submenu = false;

function turn_button_black(this_button_id){
	$j("li#" + this_button_id + ">a").addClass('menu_bg_black');
}

function revert_button(this_button_id){
	$j("li#" + this_button_id + ">a").removeClass('menu_bg_black');
}

/* Adjusts position of any submenu about to protrude
 * Beyond the right edge of the page
 */
function fix_sub_position(menu_item){
	var PAGE_WIDTH = 960;
	//Find the <ul> within you young padawan!
	var thissubmenu = menu_item.find("ul");
	
	/* Figure out if it's protruding beyond the right side.
	 * Can't get position of hidden element with IE.
	 * We know the menu item's left side matches, so use that.
	 */
	var position = menu_item.position();
	var rightpos = position.left + thissubmenu.width();

	if (rightpos > PAGE_WIDTH){
		thissubmenu.css('right', 2);
	}
}

$j(document).ready(function(){
	
	//Mouse over menu button
	$j("ul#menu > li").hover( function(){
		turn_button_black($j(this).attr("id"));
		fix_sub_position($j(this));
		hovering_button = true;
	}, function(){ //mouse out
		hovering_button = false;
		if( !hovering_submenu )
			revert_button($j(this).attr("id"));
	});

	//Mouse over submenu opened by button
	$j("ul.sub-menu").hover( function(){
		turn_button_black($j(this).parent().attr("id"));
		hovering_submenu = true;
	}, function(){ //mouse out
		hovering_submenu = false;
		if( !hovering_button )
			revert_button($j(this).parent().attr("id"));
	});
});
