var selector;
var prev_background;
var prev_color;
var prev_shadow;

//window.location.protocol will be http: or https:
var hover_image_loc = window.location.protocol + "//www.websavers.ca/wp/wp-content/themes/websavers/images/menu-black.gif";

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

function turn_button_black(this_button_class){

	/* We need the second class applied to the button
	 * because we're taking it from elements like 
	 * <li class="page_item page-item-9">
	 * We also need to correct for the current page 
	 * which looks like <li class="current_page_item">
	*/
	var classes = this_button_class.split(" ");
	
	if ( this_button_class == "current_page_item" ){
		//button is the current page button
		selector = "div#nav1>ul>li." + this_button_class + " a";
	}
	else{
		//button has no class at all: home button when not current page
		selector = "#nav1>ul>li a:not(div#nav1 ul li.page_item a)";
	}	
	if ( classes[1] != null ){ 
		//Always use the second class if available
		selector = "div#nav1 ul li." + classes[1] + " a";
	}
	
	//save current values before we change them
	if (prev_background == null && prev_color == null){
		prev_background = $j(selector).css('background-image');
		prev_color = $j(selector).css('color');
		prev_shadow = $j(selector).css('text-shadow');
	}

	$j(selector).css({
		'background': 'url(' + hover_image_loc + ') repeat-x left',
		'color': 'white',
		'text-shadow': '#000 -1px -1px 0'
	});
	/*
	$j(selector + " span").css({
		'background': 'url(' + hover_image_loc + ') no-repeat right'
	});
	*/
	
	// Ensure the above doesn't take over for submenu elements
	fix_submenu();
}

function revert_button(){
	$j(selector).css({
		'background': prev_background + " repeat-x left",
		'color': prev_color,
		'text-shadow': prev_shadow
	});
	/*
	$j(selector + " span").css({
		'background': prev_background + " no-repeat right"
	});
	*/
	
	// Ensure the above doesn't take over for submenu elements
	fix_submenu();
	
	selector = null;
	prev_background = null;
	prev_color = null;
}

function fix_submenu(){
	//Corrects submenu styles
/*	$j("div#nav1 ul li ul li a, div#nav1 ul li ul li a span").css({ */
	$j("div#nav1 ul li ul li a").css({
		//'background': 'none',
		//'color': '#000',
		'text-shadow': 'none'
	});
}

/* 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 padewan!
	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("#nav1 ul li:not(#nav1 ul li ul li)").hover( function(){
		turn_button_black($j(this).attr("class"));
		fix_sub_position($j(this));
		hovering_button = true;
	}, function(){ //mouse out
		hovering_button = false;
		if( !hovering_submenu )
			revert_button();
	});

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