function findFollowingDiv(input, targetClassName, levelsUp) {
    var div = input;
    while (levelsUp > 0) {
        div = div.parentNode;
        levelsUp--;
    }
    do {
        div = div.nextSibling;
    } while (div != null && (div.tagName != "DIV" || div.className != targetClassName));
    return div;
}

var emaliaRules = {
    "A.help-button" : function(e) {
        e.onclick = function() {
            var div = findFollowingDiv(this, "help-area", parseInt(this.getAttribute("levelsUp")));
            if (div.style.display != "block") {
                div.style.display = "block";
                // make it visible
                new Ajax.Request(this.getAttribute("helpURL"), {
                    method : 'get',
                    onSuccess : function(x) {
                        div.innerHTML = x.responseText;
                    },
                    onFailure : function(x) {
                        div.innerHTML = "<b>ERROR</b>: Failed to load help file: " + x.statusText;
                    }
                });
            } else {
                div.style.display = "none";
            }
            return false;
        }
        e = null; // avoid memory leak
    },
    "A.new-help-button" : function(e) {
        e.onclick = function() {
            var div = findFollowingDiv(this, "new-help-area", parseInt(this.getAttribute("levelsUp")));
            if (div.style.display != "block") {
                div.style.display = "block";
            } else {
                div.style.display = "none";
            }
            return false;
        }
        e = null; // avoid memory leak
    }
};
Behaviour.register(emaliaRules);

