var pathName, pageName;

$(document).ready(function() {
	pathName = window.location.pathname;
	pageName = pathName.substring(pathName.lastIndexOf("/") + 1);
	pageName = (pageName == null || pageName.length == 0) ? 
		"INDEX" : 
		pageName.substr(0, pageName.length - 4);
		
	$("#menu-container").mouseleave(function() {
		$(".submenu").slideUp();
		slideOutCurrentMenu();
	});
		
	$(".menu li").hover(function() {	
		if (!$(this).hasClass("expanded"))
		{
			$("li[id!='" + $(this).attr("id") + "'][class='expanded']").animate({ width: 82 }, { queue: false, duration: 500 });
			$("li[id!='" + $(this).attr("id") + "'][class='expanded']").removeClass("expanded");
			slideMenu($(this).attr("id"), "out", true);
		}
		
		showSubMenu($(this).attr("id"));
	});
	
	$(".menu li").mouseleave(function() {
		if (!$(".submenu li").is(":visible"))
			slideMenu($(this).attr("id"), "in", true);
	});
	
	$(".menu li").click(function() {
		goTo(this);
	});
	
	$(".submenu").mouseleave(function() {
		$(this).slideUp();
		slideMenu($(this).attr("id").replace("submenu-", ""), "in", true);
	});
	
	$(".submenu li").hover(function() {
		$(this).css("background", "#E2E2D6");
	});
	
	$(".submenu li").mouseout(function() {
		$(this).css("background", "transparent");
	});
	
	$(".submenu li").click(function() {
		goTo(this);
	});
	
	// Initialize the menu: expand the appropriate menu
	slideOutCurrentMenu();
});

function slideMenu(menuID, direction, useMatchingID)
{
	var parentID = $("#" + menuID).parents().attr("id");
	if (parentID.indexOf("submenu") == 0)
		return;

	useMatchingID = (useMatchingID == null) ? true : useMatchingID; 
	var menuWidth = direction.toUpperCase() == "IN" ? 82 : 178;
	
	if (direction.toUpperCase() == "OUT")
		$("#" + menuID).addClass("expanded");
	else
		$("#" + menuID).removeClass("expanded");			
	
	if (useMatchingID)
		$("#" + menuID).animate({ width: menuWidth }, { queue: false, duration: 500, easing: "linear"});
	else
		$(".menu li[id!='" + menuID + "']").animate({ width: menuWidth },  { queue: false, duration: 500, easing: "linear" });
}

function showSubMenu(parentMenuID)
{
	$("ul.submenu[id!='submenu-" + parentMenuID + "']").slideUp("fast").fadeOut();
	$("#submenu-" + parentMenuID).slideDown(500);
}

function goTo(object)
{
	var menuPage = ((pathName.indexOf("articles/") < 0) ? 
		"" : "../") + $(object).attr("id") + ".asp";
	document.location = menuPage;
}

function slideOutCurrentMenu()
{
	var queryString = 
		"actionRequested=" + encodeURIComponent("Get page menu status") + 
		"&pageName=" + pageName;
		
	$.ajax({
		url:		"ajaxfunctions.asp", 
		cache:		false,
		data:		queryString, 
		success:	function(data) {
			var isMenuItem = data.toUpperCase() == "TRUE" ? true : false;
			
			if (isMenuItem)
				slideMenu(pageName, "out", true);	
		},
		dataType:	"html"
	});
}

