/**
 * Creates the domelement named name.
 *
 * @see  document.createElement(String)
**/
function ce(name) {
    return document.createElement(name);
}

/**
 * Dynamically renders a dropown list
 * for a in-page anchor navigation.
 *
 * @param jumpnodes the nodes who's name to jump to (&lt; a name=...)
 * @param textnodes the corresponding textnodes whos inner TextNodes will be used
 *        to create the selection texts.
 * @param parentOfDropdown the element to create the form in.
 * @param id the unique id of the selection.
 * @param labelText the text for the selection's label tag.
**/
function beaviour_renderDropdown(jumpnodes, textnodes, parentOfDropdown, id, labelText) {
    if (jumpnodes != null &&
        textnodes != null &&
        parentOfDropdown != null &&
        jumpnodes.length == textnodes.length) {

        // Create form.
        var form = ce("form")
        // Create fieldset
        var fieldset = ce("fieldset")
        var legend = ce("legend");
        // Create selection.
        var selection = ce("select");
        // Create label.
        var label = ce("label")

        form.appendChild(fieldset);
        fieldset.appendChild(legend);
        legend.style.visibility = 'hidden';
        legend.appendChild(document.createTextNode("Anchor navigation"))
        label.setAttribute("for", id)
        selection.id = id;
        label.appendChild(document.createTextNode(labelText));
        fieldset.appendChild(label);
        fieldset.appendChild(selection);
        for (var i = 0; i < textnodes.length; ++i) {
            var option = ce("option");
            var text = document.createTextNode(textnodes[i].firstChild.nodeValue);
            option.appendChild(text);
            option.setAttribute("value", jumpnodes[i].name);
            selection.appendChild(option);
        }

         selection.onchange = function() {
             try {
                 window.location.hash = selection.options[selection.selectedIndex].value;
             } catch (e) {
                 // ignore.
             }
         }
         parentOfDropdown.appendChild(form);
    }
}
