function handleSubMenuWidth() {
	// make submenus the right width
	$("ul.subnav").show();
	$("ul.subnav").each(function(index, value) {
		var linkPadding = Number($(this).parent().find('a.toplink').css('paddingLeft').substr(0,2)) * 2;
		var widestWidth = $(this).parent().find('a.toplink').textWidth() + linkPadding + 80;
		//$.log('widestWidth: '+widestWidth);
		$(this).find('li').each(function(index, value) {
			//alert($(this));
			//$.log('$(this).textWidth(): '+$(this).textWidth());
			if (widestWidth < $(this).textWidth()) {
				widestWidth = $(this).textWidth();
			}
		});
		//$.log('widestWidth: '+widestWidth);
		$(this).width(widestWidth);
	});
	$("ul.subnav").hide();
}

function handleMenuItemOver() {	
	// fade out all
	$("ul.subnav").stop().animate({
		'opacity': 0
		}, 500, 'easeOutExpo', 
		function() {
			$(this).hide();
		}
   	);
	
	$("a.toplink").each(function(index, value) {
		if (!$(this).hasClass('selected')) {
			$(this).stop().animate({
			'backgroundColor': app_color
			}, 500, 'easeOutExpo');
		}
	});

	$.log('$(this).find("ul.subnav").children().length: '+$(this).find("ul.subnav").children().length);
	if ($(this).find("ul.subnav").children().length > 0) {
		$(this).find("ul.subnav").show();
        $(this).find("ul.subnav").stop().animate({
			'opacity': 1
			}, 500, 'easeOutExpo'); //fade in the subnav
        //$(this).find("ul.subnav").hover(function() {}, hideSubMenu);
		$(this).find("ul.subnav").mouseout(hideSubMenu);
	}
	
	$(this).find("a.toplink").stop().animate({
		'backgroundColor': custom_color_3
		}, 500, 'easeOutExpo');
}

function handleMenuItemOut() {
	if ($(this).find("ul.subnav").children().length == 0 && !$(this).find("a.toplink").hasClass('selected')) {
		$(this).find("a.toplink").stop().animate({
			'backgroundColor': app_color
			}, 500, 'easeOutExpo');
	}
}

function hideSubMenu() {
	$(this).stop().animate({
		'opacity': 0
		}, 500, 'easeOutExpo', 
		function() {
			$(this).hide();
		}
   	);
	
	$.log('isSelected: '+$(this).hasClass('selected'));
	if (!$(this).parent().find("a.toplink").hasClass('selected')) {
		$(this).parent().find("a.toplink").stop().animate({
			'backgroundColor': app_color
			}, 500, 'easeOutExpo'); // change background color of top link
	}	
}

function handleMenuSelection(paths) {
	var itemName = paths[1];
	$("a.toplink").each(function(index, value) {
		var t = $(this).html();
		if (itemName == makeNiceURL(t)) {
			$(this).addClass('selected');
			$(this).stop().animate({
				'backgroundColor': custom_color_3
				}, 500, 'easeOutExpo');
		} else {
			$(this).removeClass('selected');
			$(this).stop().animate({
				'backgroundColor': app_color
				}, 500, 'easeOutExpo');
		}
	});
}

function handleRedirects() {
	$("a.redirect").unbind("click");
	$("a.redirect").click(function() {
		//alert('redirect!');
		window.open($(this).attr("href"), redirect_target);
		autoPauseAudio();
		return false;
	});
}
