/* 
 * Expanding/Contracting Menus using XHTML, CSS, and Javascript
 * Author: Matthew Elliott
 * matthew.elliott@pnl.gov
 * Updated: August 19, 2004
*/



/* This array defines the menus in your page that will be dynamic. The values are the ids from the div that surrounds the menu title and graphic in each menu. Each value must be in double-quotes and seperated by commas */
var menusArray = new Array("ff","pr","ids","prog","tw","report","award","more","contact","new","regions");



/* "menus" function recieves menu name variable from XHTML, sets "Links" and "Arrow" names for use in the function, checks status, switches status, switches graphic, and sets cookie */
function leftMenus(menuName)
{
    /* Set menu name for function based on variable passed from XHTML and adding "Links" */
    var thisMenuName = document.getElementById(menuName + "List");
    /* Set menu arrow name for function based on variable passed from XHTML and adding "Arrow" */
    var thisMenuArrow = menuName + "Arrow";
    
    /* Check if menu is contracted */
    if(thisMenuName.style.display == "none")
    {
        /* Expand menu */
        thisMenuName.style.display = "block";
        /* Set graphic to "open". Be sure to change src to direct to your "open" graphic */
        document.getElementById(thisMenuArrow).src = "/images/arrow_down1.jpg";
        /* Set cookie saying menu is expanded. Last variable (default '/menus/') must be changed to the directory of your site, starting from the root. IE, http://www.thissite.com/here/is/the/directory must be written '/here/is/the/directory/' This specifies that only your site can use this cookie. */
        setCookie(menuName,'1','','/');
    }
    else
    {
        /* Contract menu */
        thisMenuName .style.display = "none";
        /* Set graphic to "closed". Be sure to change src to direct to your "closed" graphic */
        document.getElementById(thisMenuArrow).src = "/images/arrow_up1.jpg";
        /* Set cookie saying menu is contracted. Last variable (default '/menus/') must be changed to the directory of your site, starting from the root. EX: http://www.thissite.com/here/is/the/directory must be written '/here/is/the/directory/' This specifies that only your site can use this cookie. */
        setCookie(menuName,'0','','/');
    }
    
    return false;
}



/* "initializeMenus" function is called on load of the XHTML. This establishes the status of all menus. */
function initializeMenus(menusArray)
{
   for(i=0; i<menusArray.length; i++)
   {
        establishMenus(menusArray[i]);
   }    
    
}



/* "establishMenus" function receives menu name, gets associated cookie, and sets menus and graphics to pre-defined values */
function establishMenus(menuName)
{
    /* By default, all menus are contracted (value of "0") */
    var menuStatus = "0";
    /* Get cookie value and apply to variable */
    menuStatus = getCookie(menuName);
    /* Create name of menu to alter */
    menuLinks = menuName + "List";
    /* Create name of graphic to alter */
    menuArrow = menuName + "Arrow";
    
    /* Check if cookie has set menu status to expanded (value of "1") */
    if(menuStatus == "1")
    {
        /* Set menu status to Expanded */
        document.getElementById(menuLinks).style.display = "block";
        /* Set graphic to "opened" value. Be sure to change src to direct to your "open" graphic */
        document.getElementById(menuArrow).src = "/images/arrow_down1.jpg";
    }
    else
    {
        /* Set menu status to Contracted */
        document.getElementById(menuLinks).style.display = "none";
        /* Set graphic to "closed" value. Be sure to change src to direct to your "closed" graphic */
        document.getElementById(menuArrow).src = "/images/arrow_up1.jpg";
    }
}


    
/* "setCookie" function sets cookie to define menu status. DO NOT CHANGE ANYTHING IN THIS FUNCTION */
function setCookie(name, value, expires, path, domain, secure) 
{
    var today = new Date();
    var expires = new Date();
    expires.setTime(today.getTime() + 1000*60*60*24*365*5);
    expires = "";
    
    var curCookie = name + "=" + escape(value) + 
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
    document.cookie = curCookie;
}



/* "getCookie" function gets cookie value for menu status. DO NOT CHANGE ANYTHING IN THIS FUNCTION */
function getCookie(name) 
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) 
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
        begin += 2;
    var end = document.cookie.indexOf(";", begin);

    if (end == -1)
    end = dc.length;
    
    return unescape(dc.substring(begin + prefix.length, end));
}

function selectNav()
{
    fileURL = document.URL;
    slashPlace = fileURL.lastIndexOf("/");
    dotPlace = fileURL.lastIndexOf(".");
    
    if(dotPlace != -1)
    {
        fileName = fileURL.substring(slashPlace+1,dotPlace);
    
        directory = fileURL.substr(0,slashPlace);
        newSlashPlace = directory.lastIndexOf("/");
        folder = directory.substring(newSlashPlace+1,directory.length);
        
        if(folder != "hqda-energypolicydev.pnl.gov" && folder != "army-energy.hqda.pentagon.mil")
        {
            if(folder == "calendar" || folder == "archive" || folder == "newsletter")
            {
                idValue = folder;
            }
            else
            {
                idValue = folder + "_" + fileName;
            }
        }
        else
        {
            idValue = fileName;
        }

        document.getElementById(idValue).style.borderColor = "#D61823";
    }
    else
    {
        directory = fileURL.substr(0,slashPlace);
        newSlashPlace = directory.lastIndexOf("/");
        folder = directory.substring(newSlashPlace+1,directory.length);
        
        alert(folder);
    
        if(folder == "calendar" || folder == "archive" || folder == "newsletter")
        {
            idValue = folder;
            document.getElementById(idValue).style.borderColor = "#D61823";
        }
    }
}