var imgNodeClassName = "asm-tree-node-icon";
var imgDocName = "node_bl.gif";
var imgPlusName = "node_pl.gif";
var imgMinusName = "node_mi.gif";


var baseStr = "";
var relativeURL = null;
var rootNode = null;
var matchNode = null;

function adjustVisibility(listName, ibaseStr) {
    baseStr = ibaseStr;
    rootNode = document.getElementById(listName);

    //figure out the relative link of the url we are dealing with
    relativeURL = getRelativeURL(document.URL);
    var spos = relativeURL.indexOf(baseStr);
    if (spos>-1) relativeURL = relativeURL.substring(spos+baseStr.length);

    //now lets walk the toc dom
    traverse(rootNode.childNodes);

    //now let's set our visibility
    if (matchNode!=null) {
        //we will always set the child Nodes visible
        var gotSome = setVisible(matchNode.childNodes, 0);
        if (gotSome) {
            var imgNode = getChildByClass(matchNode, imgNodeClassName);
            if (imgNode!=null) {
                imgNode.src = imgNode.src.replace(imgPlusName, imgMinusName);
            }
        }

        //now we need to find the containing list element
        var depth = -1;
        var ulNode = getULParent(matchNode);
        while (ulNode!=null && ulNode!=rootNode) {
            depth = depth + 1;
            setVisible(ulNode.childNodes, depth);
            var liParent = getLIParent(ulNode);
            if (liParent!=null) {
                var imgNode = getChildByClass(liParent, imgNodeClassName);
                if (imgNode!=null) {
                    imgNode.src = imgNode.src.replace(imgPlusName, imgMinusName);
                }
            }
            ulNode = getULParent(ulNode);
        }

        //finally, set the style on the match node
        matchNode.className += " asm-tree-node-match";
    }
}

function traverse(children) {
    if (children!=null) for (var i=0; i<children.length; i++) {
        var child = children[i];
        var tag = (""+child.tagName).toLowerCase();

        if (tag=="a" && child.hasChildNodes) {
            traverseAnchor(child.childNodes);
        }

        if (tag=="ul" || tag=="li") {
            if (tag=="li") {
                var href = getHref(child);

                //check for matchNode
                if (matchNode==null && href==relativeURL) matchNode = child;

                //set everything except for the topmost level to be invisible
                if (child.parentNode!=rootNode) {
                    child.style.display = "none";
                }
            }

            if (child.hasChildNodes) {
                traverse(child.childNodes);
            }
        }
    }
}

function traverseAnchor(children) {
    if (children!=null) for (var i=0; i<children.length; i++) {
        var child = children[i];
        var tag = (""+child.tagName).toLowerCase();

        if (tag=="img" && child.className==imgNodeClassName) {
            var liparent = getLIParent(child);
            if (liparent!=null && hasULChild(liparent)) {
                child.src = child.src.replace(imgDocName, imgPlusName);
            }
        } else if (child.hasChildNodes) {
            traverseAnchor(child.childNodes);
        }
    }
}


function getHref(parent) {
    var children = parent.childNodes;
    if (children!=null) for (var i=0; i<children.length; i++) {
        var child = children[i];
        var tag = (""+child.tagName).toLowerCase();
        if (tag=="a") {
            return getRelativeURL(child.href);
        } else if (child.hasChildNodes) {
            var href = getHref(child.childNodes);
            if (href!=null) return getRelativeURL(href);
        }
    }
    return null;
}

function getRelativeURL(href) {
    var spos = href.indexOf(baseStr);
    if (spos>-1) href = href.substring(spos+baseStr.length);
    return href;
}

function setVisible(children, depth) {
    var gotSome = false;
    if (children!=null) for (var i=0; i<children.length; i++) {
        var child = children[i];
        var tag = (""+child.tagName).toLowerCase();

        if (tag=="ul" && !gotSome) {
            gotSome = setVisible(child.childNodes, depth);
        }

        if (tag=="li") {
            child.style.display = "list-item";
            if (child!=matchNode) {
                child.className += " asm-tree-node-visible";
            }
            gotSome = true;
        }
    }
    return gotSome;
}

function getULParent(child) {
    var parent = child.parentNode;
    var tag = (""+parent.tagName).toLowerCase();
    while (parent!=null && !(tag=="ul")) {
        parent = parent.parentNode;
        tag = (""+parent.tagName).toLowerCase();
    }
    return parent;
}

function getLIParent(child) {
    var parent = child.parentNode;
    var tag = (""+parent.tagName).toLowerCase();
    while (parent!=null && !(tag=="li")) {
        parent = parent.parentNode;
        tag = (""+parent.tagName).toLowerCase();
    }
    return parent;
}

function hasULChild(parent) {
    if (parent==null || !parent.hasChildNodes) return false;
    var children = parent.childNodes;
    for (var i=0; i<children.length; i++) {
        var child = children[i];
        var tag = (""+child.tagName).toLowerCase();
        if (tag=="ul") return true;
    }
    return false;
}

function getChildByClass(parent, targetClass) {
    if (parent==null || !parent.hasChildNodes) return null;
    var children = parent.childNodes;
    for (var i=0; i<children.length; i++) {
        var child = children[i];
        if (child.className==targetClass) {
            return child;
        }
    }
    for (var i=0; i<children.length; i++) {
        var child = getChildByClass(children[i], targetClass);
        if (child!=null) return child;
    }
    return null;
}
