/* emobfus - email address obfuscation
 * By Justin Force <justin.force@gmail.com>
 * Modified Jul 14, 2009
 * Requires Prototype >= v1.6 <http://prototypejs.org>
 *
 * Email addresses should appear in the document as follows
 * <span class="emobfus">domain.tld joe</span>
 *
 * They will be reassembled as
 * <span class="emobfus">joe@domain.tld</span>
 *
 * The script isn't picky about whitespace. <span ...>     company.com
 * joe</span> is ok.
 *
 * If you instead set the class to emobfusl, a mailto: link will be inserted as
 * well, i.e.
 * <span class="emobfusl"><a href="mailto:joe@company.com">joe@company.com</a></span>.
 */

emobfus = {
    spans:null,               // spans that will be linked (mailto) email addresses
    re:/\s*(\S+)\s+(\S+)\s*/, // the regular expression to match
    replace:'$2@$1',          // the replacement text
    init:function() {
        emobfus.spans = $$('.emobfus, .emobfusl');
        for (var i=0;emobfus.spans[i];i++) {
            var span = $(emobfus.spans[i]);
            var email = span.innerHTML.replace(emobfus.re,emobfus.replace);
            var node = document.createTextNode(email); 
            if (span.hasClassName('emobfusl')) {
                var a = new Element('a', { href: 'mailto:'+email });
                a.appendChild(node);
                node = a;
            }
            span.update();
            span.removeClassName('emobfus');  // necessary to keep IE from doing it twice
            span.removeClassName('emobfusl'); //
            span.appendChild(node);
        }
    }
}
Event.observe(window,'load',emobfus.init);
