/*



Anti-Spam Email Links 1.6

=========================

by Andrew Gregory

http://www.scss.com.au/family/andrew/webdesign/emaillinks/



This work is licensed under the Creative Commons Attribution License. To view a

copy of this license, visit http://creativecommons.org/licenses/by/1.0/ or send

a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305,

USA.



IMPORTANT NOTE:

Variables and functions with names starting with an underscore (_) are

'internal' and not to be used.



*/



// search for (with subject "subject") in the element text and use the text

// inside the double quotes as the email subject

function emaillinks_subject(ele, link) {

  var subject = emaillinks_config.subj.exec(ele.childNodes[0].nodeValue);

  if (subject) {

    link.setAttribute('href', link.getAttribute('href') + '?subject=' + subject[1]);

  }

}



// Process the element

function _emaillinks_process(ele) {

  var cfg = emaillinks_config;

  // get recipient name and obfuscated email address

  var text = ele.childNodes[0].nodeValue;

  var name = (cfg.name) ? cfg.name.exec(text) : null;

  var addr = (cfg.addr) ? cfg.addr.exec(text) : null;

  if (addr) {

    var title = ele.getAttribute('title');

    // un-obfuscate email address

    addr = addr[1];

    if (cfg.unobs) {

      for (var i = 0; i < cfg.unobs.length; i++) {

        var r = cfg.unobs[i];

        addr = addr.replace(r.re, r.txt);

      }

    }

    // create link element

    var ns = document.getElementsByTagName('html')[0].namespaceURI;

    var link = ns ? document.createElementNS(ns, 'a') : document.createElement('a');

    // copy attributes

    var attrs = ele.attributes;

    for (var i = 0; i < attrs.length; i++) {

      var attr = attrs.item(i);

      if (attr.specified) {

        link.setAttribute(attr.name, attr.value);

      }

    }

    // set link text

    text = (name) ? name[1] : (title != null && title != '') ? title : addr;

    link.appendChild(document.createTextNode(text));

    // set link address

    link.setAttribute('href', 'mailto:' + addr);

    // last minute processing

    if (cfg.process) {

      for (var i = 0; i < cfg.process.length; i++) {

        cfg.process[i](ele, link);

      }

    }

    // insert link into document

    ele.parentNode.insertBefore(link, ele);

    // and remove the obfuscated element

    ele.parentNode.removeChild(ele);

    return true;

  }

  return false;

}



// Process selected elements with the configured class name

function _emaillinks_processAll() {

  var className = (emaillinks_config.className) ? emaillinks_config.className : 'email';

  var eles = document.getElementsByTagName('a');

  for (var i = 0; i < eles.length; i++) {

    var ele = eles[i];

    if (ele.className && ele.className.indexOf(className) != -1) {

      if (_emaillinks_process(ele)) {

        i--;

      }

    }

  }

}



// default configuration

var emaillinks_config = {

  className:'email',

  addr:/<([^>]*)>/,

  name:/"([^"]*)"/,

  subj:/with subject "([^"]*)"/,

  process:[emaillinks_subject],

  unobs:[

    {re:/\s+at\s+/ig , txt:'@'},

    {re:/\s+dot\s+/ig, txt:'.'},

    {re:/\s+-at-\s+/ig , txt:'@'},

    {re:/\s+-dot-\s+/ig, txt:'.'},

    {re:/\s+\(at\)\s+/ig , txt:'@'},

    {re:/\s+\(dot\)\s+/ig, txt:'.'},

    {re:/[\.]?invalid$/i, txt:''},

    {re:/\s+/g, txt:''}

  ]

};



// process emails when the document finishes loading

addEvent(window, 'load', _emaillinks_processAll, false);
