(function($) {
  var toByteArray = function(str) {
    var byteArray = [];
    for (var i = 0; i < str.length; i++)
      if (str.charCodeAt(i) <= 0x7F)
        byteArray.push(str.charCodeAt(i));
      else {
        var h = encodeURIComponent(str.charAt(i)).substr(1).split('%');
        for (var j = 0; j < h.length; j++)
          byteArray.push(parseInt(h[j], 16));
      }
    return byteArray;
  };

  var parseByteArray = function(byteArray) {
    var str = '';
    for (var i = 0; i < byteArray.length; i++)
      str +=  byteArray[i] <= 0x7F?
              byteArray[i] === 0x25 ? "%25" : // %
              String.fromCharCode(byteArray[i]) :
              "%" + byteArray[i].toString(16).toUpperCase();
    return decodeURIComponent(str);
  };

  $.fn.emailLink = function() {
    this.each(function() {
      var elem = $(this);
      elem.replaceWith('<a href="mailto:' + parseByteArray(elem.attr('data-stuff').split(',')) + '">' + elem.text() + '</a>');
    });
    return this;
  };

  $(function() {
    $('#contact .email').emailLink();
  });
})(jQuery);

