// NOTE: using # in an URL will mess up this code.  
// (because tomcat rewrites the URI to put the jsessionid after the page
//  name, but before the #)

// some globals to hold the open/closed images
var openImgPath = "/admin/images/xmlTreeOpen.gif";
var closedImgPath = "/admin/images/xmlTreeClosed.gif";
var openImg = new Image();
openImg.src = openImgPath;
var closedImg = new Image();
closedImg.src = closedImgPath;

// cookies used to store menu state and the last item clicked
var initCookie = "menusInited";
var menuCookie = "menusToExpand";
var itemCookie = "itemToHighlight";

// make sure these branches are open
var branchToOpen = null;
var branch2ToOpen = null;

// disable text selection (for IE) in the whole app 
// rbaynes: not what we want, we want this in the menu only
//document.onselectstart = new Function ("return false");
  
// initialize the menu state
window.onload = initializeMenus;


function clearMenuCookies()
{
    deleteCookie( initCookie );
    deleteCookie( menuCookie );
    deleteCookie( itemCookie );
}

function initializeMenuState( branch, leafHREF )
{
    if( null == getCookie( initCookie )) {
        setCookie( initCookie, "true" );   // only init menu once
        setCookie( menuCookie, branch );
        setCookie( itemCookie, leafHREF );
    }
}

// this is ONLY used in the onload handler of each page.
function initializeMenus()
{
    // now show the menu using the previous state (from cookies)
    var menusToExpand = getCookie( menuCookie );
    if( null != menusToExpand ) {
        // if more than one menu has been menusToExpanded,
        // create an array of menusToExpanded menus
        if( -1 != menusToExpand.indexOf( "," ) ) {
            menuArray = menusToExpand.split( "," );
            for( var i=0; i < menuArray.length; i++ ) {
                openBranch( menuArray[i] );
            }
        } else {
            openBranch( menusToExpand );
        }
    }
    // highlight current menu item
    showLeaf();
    // open a branch specified by a page
    if( null != branchToOpen ) {
        openBranch( branchToOpen );
        branchToOpen = null;
    }
    if( null != branch2ToOpen ) {
        openBranch( branch2ToOpen );
        branch2ToOpen = null;
    }
    // now show the entire menu, this prevents flashing and CGrim whining
    var entireMenu = document.getElementById( "1EntireMenu" );
    if( null != entireMenu && null != entireMenu.style ) {
        entireMenu.style.display = "block";
    }
} 

// open a branch, only if its closed
function openBranch( branch )
{
    if( undefined == document.getElementById( branch )) {
        return;
    } 
    var objImg = document.getElementById( 'I_' + branch );
    if( null == objImg ) {
        return;
    }
    var objBranch = document.getElementById( branch ).style;
    if( null == objBranch ) {
        return;
    }
    if( objImg.src.indexOf( closedImgPath ) > -1 ) {
        // branch is closed, so open
        objImg.src = openImg.src;
        objBranch.display = "block";
    }

    // add this branch to the saved cookie if its not in there already
    var mcook = getCookie( menuCookie );
    if( null == mcook ) {
        setCookie( menuCookie, branch );
    } else if( -1 == mcook.indexOf( branch )) {
        setCookie( menuCookie, mcook + "," + branch );
    }
}

// show or hide a menu branch, and save the tree state in a cookie
// this is ONLY used in the onclick handler in the menu
function showBranch( branch )
{
    if( undefined == document.getElementById( branch )) {
        return;
    } 

    // tree branch obj (image plus text in a span)
    var objBranch = document.getElementById( branch ).style;
    if( null == objBranch ) {
        return;
    }
    // branch image (open or closed)
    var objImg = document.getElementById( 'I_' + branch );
    if( null == objImg ) {
        return;
    }
    // tree state saved in a cookie
    var mcook = getCookie( menuCookie );

    // if the image is currently open, then close the branch, or vice versa
    if( objImg.src.indexOf( openImgPath ) > -1 ) {
        // branch is open, so close
        objImg.src = closedImg.src;
        objBranch.display = "none";
        // remove this branch from the menu cookie
        if( -1 != mcook.indexOf( branch )) {

            if( -1 != mcook.indexOf( "," + branch )) {
                mcook = mcook.replace( "," + branch, "" );
            } else if( -1 != mcook.indexOf( branch + "," )) {
                mcook = mcook.replace( branch + ",", "" );
            } else {
                mcook = mcook.replace( branch, "" );
            }
            // delete cookie if no branches open or update it.
            if( "" == mcook ) {
                deleteCookie( menuCookie );
            } else {
                setCookie( menuCookie, mcook );
            }
        }
    } else {
        // branch is closed, so open
        objImg.src = openImg.src;
        objBranch.display = "block";
        // add to the menu cookie to remember this open branch
        if( null == mcook ) {
            setCookie( menuCookie, branch );
        } else if( -1 == mcook.indexOf( branch )) {
            setCookie( menuCookie, mcook + "," + branch );
        }
    }
}

// unhighlight any previous link and save last link clicked.  
// usage: onclick="saveLeaf( this.href );"
function saveLeaf( href ) 
{
    // strip the ;jsessionid=xxx from the url before saving
    var semi = href.indexOf( ";" );
    var ques = href.indexOf( "?" );
    var hash = href.indexOf( "#" );
    if( semi > -1 ) {
        var str = href.substring( 0, semi ); // string before the ;
        if( ques > -1 ) {
            href = str + href.substring( ques ); // string after the ?
        } else if( hash > -1 ) {
            href = str + href.substring( hash ); // string after the #
        } 
    }

    // save last link clicked.  
    setCookie( itemCookie, href );
    // show it
    showLeaf( href );
}

// highlight the current leaf
function showLeaf( href ) 
{
    var itemToHighlight = href ? href : getCookie( itemCookie );
    if( null == itemToHighlight ) {
        return;
    } 

    var links = document.getElementsByTagName( "a" );
    for( i=0; i < links.length; i++ ) {
        // only modify links with the 'menua' style
        if( links[i].className.indexOf( "menua" ) > -1 ) {
            // unhighlight any previous link
            links[i].className = "menua";
            if( links[i].href.indexOf( itemToHighlight ) > -1 ) {
                // highlight current link
                links[i].className += " menuhighlight";
            }
        }
    }
}

// go to the current leaf
function goToLeaf() 
{
    var links = document.getElementsByTagName( "a" );
    for( i=0; i < links.length; i++ ) {
        // only modify links with the 'menuhighlight' style
        if( links[i].className.indexOf( "menuhighlight" ) > -1 ) {
           if( window.location != links[i].href ) {
               window.location = links[i].href;
           }
        }
    }
}
   
// cookie functions, used to remember menu settings between page changes
function setCookie( name, value, expires, path, domain, secure ) 
{
    document.cookie = name + "=" + escape( value ) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path)    ? "; path=" + path : "") +
        ((domain)  ? "; domain=" + domain : "") + 
        ((secure)  ? "; secure" : "");
}
function getCookie( name ) 
{
    var prefix = name + "=";
    var start = document.cookie.indexOf( prefix );

    if( start == -1 ) {
        return null;
    }
    
    var end = document.cookie.indexOf( ";", start + prefix.length );
    if( end == -1 ) {
        end = document.cookie.length;
    }

    var value = document.cookie.substring( start + prefix.length, end );
    return unescape( value );
}
function deleteCookie( name, path, domain ) 
{
    if( getCookie( name )) {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}


