Well, that was *almost* right ;-) ... replace that while loop with this:
while (tgt && tgt.nodeName.toLowerCase()!="ul" &&
tgt.className!="foldheader") {
tgt = tgt.parentNode;
// this loop bubbles up through the content model to find either
// a <ul> or a <li class="foldheader">.
}
if (!tgt) return; // quit if neither of the above items.
