/**
* Copyright (c) 2009, 2010 Adam Schmalhofer
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/

/** Unobscure email adresses in page */
function unobscured()
{
    var obscured_span = document.getElementsByClassName('obscured');
    while (obscured_span.length > 0)
        unobscure_node(obscured_span[0]);
}


/** unobscure email adress in node and put it into a mailto-link. */
function unobscure_node(obscured_node)
{
    del_non_text_children(obscured_node);
    var text = obscured_node.innerHTML;
    obscured_node.innerHTML = wrap_in_mailto_link_html(text);
    _unobscure_node__flip_obscure_class(obscured_node)
}


function wrap_in_mailto_link_html(address)
{
    return '<a href="mailto:'.concat(address).concat('">'
            ).concat(address).concat('</a>');
}


/** Change the class='obscured' to class='unobscured' of given node.
 *
 *  Other classes of node remain unchanged.
 *
 * */
function _unobscure_node__flip_obscure_class(obscured_node)
{
    var class_attr = obscured_node.getAttribute('class');
    if (class_attr == null)
        return;
    class_attr = class_attr.replace(/^(.* )?obscured( .*)?$/, '$1unobscured$2');
    obscured_node.setAttribute('class', class_attr);
}


/** Delete all non-text children of a given node. */
function del_non_text_children(node)
{
    var child = node.firstChild;
    while (child != null) {
        if (child.nodeType == 1)
            // nodeType is <node>
            node.removeChild(child);
        child = child.nextSibling;
    }
}
