/*!
 * Mailto Link Plugin for jQuery, version ALPHA
 *
 * Copyright 2010, Dell Sala
 * http://dellsala.com/
 * Dual licensed under the MIT or GPL Version 2 licenses.
 * http://jquery.org/license
 *
 * Date: 2010-03-15
 */
(function () {

    jQuery.fn.mailtolink = function (options) {
    
        var options = jQuery.extend(jQuery.mailtolink.defaultOptions, options);
    
        return this.each(function () {
            if (this.nodeName.toUpperCase() == 'A') {
                var email = jQuery.mailtolink.normalizeEmail(
                    this.title, options.atPattern, options.dotPattern
                );
                if (email == this.title) {
                    return;
                }
                this.href = 'mai'+
                    'lt'+
                    'o:'+email;
                this.title = email;
            } else {
                var elementContent = this.innerHTML;
                var email = jQuery.mailtolink.normalizeEmail(
                    elementContent, options.atPattern, options.dotPattern
                );
                if (email == elementContent) {
                    return;
                }
                var jElement = jQuery(this);
                jElement.replaceWith('<a class="email_link" href="ma'+
                    'ilt'+
                    'o:'+email+'">'+email+'</a>');
            }
        });
    
    };

    jQuery.mailtolink = {

        defaultOptions : {
            atPattern : / {AT} /g,
            dotPattern : / {DOT} /g
        },

        normalizeEmail : function (string, atPattern, dotPattern) {
            var email = string.replace(atPattern, '@');
            email = email.replace(dotPattern, '.');
            return email;
        }

    };


})();
