var searchClass = "bs"; //Class name for link items
var honorMenuID = "Menu1_5718"; //Menu that always displays
var cookieMenuName = "";
function initMenu()
{
    var menus = document.getElementsByTagName("span");
    for (var i=0;i<menus.length;i++)
    {
        if (menus[i].className == searchClass && menus[i].id != honorMenuID)    //Menu1_5716 = Ingredient Menu
        {
            menus[i].style.display = "none";
            swapFolder('I' + menus[i].id,"none");
        }
    }
}

function deHydrateMenu()
{
    var menus = document.getElementsByTagName("span");
    var menuState = "";
    for (var i=0;i<menus.length;i++)
    {
        if (menus[i].className == searchClass)
            menuState = menuState + "&" + menus[i].style.display;
    }
    createCookie(cookieMenuName,menuState);
}

function redirectMenu(destination,collectionID)
{
    
    //Collection param is used to tell the template that the master page needs to be changed
    if (collectionID != null && collectionID > "")
    {
       if (destination.indexOf("?") > -1)
        destination = destination + "&collection="+collectionID;
       else
        destination = destination + "?collection="+collectionID;
    }
    window.location.href = destination;
}

function hydrateMenu(menuName)
{
    cookieMenuName = menuName;
    var menus = document.getElementsByTagName("span");
    var menuState = readCookie(cookieMenuName);
    showBranch(honorMenuID);
    if (menuState == null)
    {
        return;
    } 
    var j = 0;
    var menusStateArray = menuState.split("&");
    for (var i=0;i<menus.length;i++)
    {
        if (menus[i].className == searchClass)
        {
            j++;
            showBranch(menus[i].id,menusStateArray[j],true);
            
        }
           
    }
}
function showBranch(branch,display,noResetMenu){
	if (branch == "" || document.getElementById(branch) == null) return;
	var currentDisplay = document.getElementById(branch).style.display;
	var objBranch = document.getElementById(branch).style;
	if (noResetMenu == null)
	    initMenu();

	if (display != null)
	{
	   objBranch.display = display;
	   swapFolder('I' + branch,display);
	}
	else
    {
	    //If the display is currently none, then init menu to hide it and show it
	    if (currentDisplay=="none")
	    {
	        objBranch.display="block";
		    swapFolder('I' + branch,"block");
	    }
	    else
	    {
	        //If the display is currently block, then init menu to hide it
		    swapFolder('I' + branch,"none");
	    }
	}
	
	deHydrateMenu();
	

}

function swapFolder(img,display){
	var MyArray;
	var MyArrays;
	var Path;
	objImg = document.getElementById(img);
	try
	{
	    if(display == "block") {
		    MyArray = objImg.src 
		    MyArrays = objImg.src.lastIndexOf("_");
		    Path = MyArray.substring(0,MyArrays) + "_expanded.gif";
		    objImg.src = Path
	    } else {
		    MyArray = objImg.src 
		    MyArrays = objImg.src.lastIndexOf("_");
		    Path = MyArray.substring(0,MyArrays) + "_collapsed.gif";
		    objImg.src = Path
	    }
	 }
	 catch(ex)
	 {
	 
	 }
}


