/****************************************************************
 * Converts obfuscated email addresses into normal, working email addresses.
 *
 * @name defuscate
 * @param Boolean link If true, all defuscated email addresses will be turned into links, defaults to true (optional)
 * @descr Converts obfuscated email addresses into normal email addresses
 *
 * Enter email address in plain text with @ symbol obfuscated: <span class="eml">foo('at' sign here)bar.com</span>
 * Run the defuscate function on the container: $(".eml").defuscate();
 ****************************************************************/

jQuery.fn.defuscate = function( settings ) {
    settings = jQuery.extend({
        link: true
    }, settings);
    var regex = /\b([A-Z0-9._%-]+)\([^)]+\)((?:[A-Z0-9-]+\.)+[A-Z]{2,6})\b/gi;
    return this.each(function() {
        if ( $(this).is('a[@href]') ) {
            // If it's an <a> element, defuscate the href attribute
            $(this).attr('href', $(this).attr('href').replace(regex, '$1@$2'));
            // Make sure that the element's contents is not made into a link
            var is_link = true;
            //alert($(this).attr('href'));
        }
        // Defuscate the element's contents
        $(this).html($(this).html().replace(regex, (settings.link && !is_link ? '<a href="mailto:$1@$2">$1@$2</a>' : '$1@$2')));
  });
}

jQuery.fn.defuscate2 = function( settings ) {
    settings = jQuery.extend({
        link: true
    }, settings);
    return this.each(function() {
		if ($(this).attr('rel')) {
			var user = $(this).attr('rel');
			if ($(this).attr('rel2')) {
				var domain = $(this).attr('rel2');
			} else {
				var domain = 'northwestdoor.com';
			}
			var address = user + '@' + domain;
			$(this).html('<a href="mailto:' + address + '">' + address + '</a>');
		}
	});
}

$(document).ready(function(){
	$(".eml").defuscate2();
});

