/**
 * Treeview functions
 *
 * Handles expanding and collapsing of treeview sections
 *
 * Copyright Hyfinity 2008
 */


function treeItemClicked(container, animate)
{
    var entryId = container.getAttribute('id');

    if ((animate == null) || (typeof(animate) == 'undefined') || animate)
        toggleComponent(entryId + "_content", null, true);
    else
        toggleComponent(entryId + "_content", null, false);

    if (dojo.hasClass(container, 'node-collapsible'))
    {
        dojo.removeClass(container, 'node-collapsible');
        dojo.addClass(container, 'node-expandable');
    }
    else
    {
        dojo.removeClass(container, 'node-expandable');
        dojo.addClass(container, 'node-collapsible');
    }
}

/**
 * Expands the specified tree enry
 ** @param container The SPAN element representing the tree entry to expand
 */
function expandTreeEntry(container)
{
    var entryId = container.getAttribute('id');

    if (dojo.hasClass(container, 'node-expandable'))
    {
        toggleComponent(entryId + "_content", 'show', true);
        dojo.removeClass(container, 'node-expandable');
        dojo.addClass(container, 'node-collapsible');
    }
}

/**
 * Expands all parent entries in the tree so that the provided entry is visible
 * The povided entry itself will not be expanded.
 * @param elem The SPAN element representing the tree entry to be visible
 */
function expandTree(elem)
{
    var parents = new Array();
    var tempElem = elem;

    while (!dojo.hasClass(tempElem, 'treeviewTop'))
    {
        if (tempElem.tagName.toUpperCase() == 'UL')
        {
            parents[parents.length] = tempElem;
        }
        tempElem = tempElem.parentNode;
    }

    for(var i = parents.length; i > 0; --i)
    {
        var list = parents[i-1];
        var sp = getPreviousSibling(list);
        expandTreeEntry(sp);
    }
}

function getPreviousSibling(elem)
{
    var prev = elem.previousSibling;
    //make sure we return an element type node, not a text node
    while (prev && prev.nodeType != 1)
    {
        prev = prev.previousSibling;
    }

    return prev;
}