// script for SiteIndex.aspx

// when the window loads, toggle the lower levels of the tree closed.
addLoadEvent(function()
{
    var LInode = document.getElementById("rootOfTree");
    SetupExpandBoxes(LInode);
    ToggleLevel(LInode);
});

function SetupExpandBoxes(LInode)
{
    IterateTree(LInode, 0, function(level, imgSpan, ULnode)
    {
        // create IMG objects
        LInode = ULnode.parentNode;
        img = new Image();
        img.src = "../images/minus.gif";
        imgSpan.appendChild(img);
        
        // wire up toggleTreeNode events
        img.onclick = toggleTreeNode;
    });
}

function ToggleLevel(LInode)
{
    IterateTree(LInode, 0, function(level, imgSpan, ULnode)
    {
        // ########## change this number to set the initial depth of the displayed tree. ###########################
        // 0 = all closed, 1 = first level open, 2 = first & second level, 3 = all open.
        if(level >= 2)
        {
            Toggle(imgSpan.childNodes[0], ULnode);
        }
    });
}

// recursively walks up the tree of nested ULs until it reaches the leaf nodes
function IterateTree(LInode, level, nodeFunction)
{
    if(!LInode.childNodes) return;
    
    var imgSpan = LInode.childNodes[0];
    var ULnode = LInode.childNodes[4];
    
    if(!ULnode) return;
    
    nodeFunction(level, imgSpan, ULnode);

    level++;
    for(index in ULnode.childNodes)
    {
        var childLInode = ULnode.childNodes[index];
        IterateTree(childLInode, level, nodeFunction);
    }
}

// this function toggles the visibility of UL elements shown in a hierarchial tree
function toggleTreeNode(e)
{
    var img;   // the +/- IMG element
    
    if (!e) var e = window.event;  
    if (e.target) img = e.target;
    else if (e.srcElement) img = e.srcElement;
    
    // defeat safari bug
    if (img.nodeType == 3)
        img = img.parentNode;
    
    // find the UL node after the target LI node
    if (img.parentNode && img.parentNode.parentNode)
    {
        Toggle(img, img.parentNode.parentNode.childNodes[4]);
    }
}

function Toggle(img, ULnode)
{
    if (ULnode)
    {
        if (ULnode.nodeName == "UL")
        {
            if(img.src)
            {
                if(img.src.indexOf("minus.gif") != -1)
                {
                    ULnode.style.display="none"
                    img.src = "../images/plus.gif";
                }
                else
                {
                    ULnode.style.display="block";
                    img.src = "../images/minus.gif";
                }
            }
        }
    }
}

// debugging function (could be removed from production release)
function listChildNodes(node)
{
    var message = "";
    for(index in node.childNodes)
    {
        var childNode = node.childNodes[index];
        message = message + "\r\n" + index + " " + childNode.nodeName;
    }
    alert(message);
}

// better than window.onload = func 
// http://simonwillison.net/2004/May/26/addLoadEvent/
//
function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') 
    {
        window.onload = func;
    } 
    else 
    {
        window.onload = function() 
        {
            if (oldonload) 
            {
                oldonload();
            }
            func();
        }
    }
}
