/* Plugin: safe_mail
 * Author: Michael D. Risser <MDRISSER AT the domain called GMAIL which is a dot COM>
 * Date: October 2007
 * Version 0.2
 * Version Update:
 *      The arguments have been replaced with a single argument in the form of an object and two
 *          additional parameters have been added.
 * Description:
 *		With the proliferation of spammers out there, scrapping email addresses off of web sites,
 *		this plugin for jQuery makes it easy to hide email address from spam bots.
 *		
 *		The plugin is far from perfect, but it works, although I plan to expand on it as time permits.
 *		Usage is pretty straight forward, as shown below. The only caveat is that you need an element to
 *		append the mailto link to. You can simply place an empty div or span where you want the email link to
 *		appear, and access it using the id you have provided. For example:
 *			<p>Here is a bunch of text in a paragraph, where I want an <span id='email_link'></span> to
 *			appear. Since the plugin appends to an element, I've included an empty span tag where I want
 *			the email address to appear.</p>
 *
 *
 * Usage:
 *		To use the email address as the link text:
 *			$('#email_link').safe_mail({
 * 								        username: "john.doe",
 *                                      domain_name: "domain",
 *                                      domain_ext: "com",
 *                                      link_txt: 'link'
 *           });
 *			Ouput: <a href="mailto:john.doe@domain.com" class="safeMail">john.doe@domain.com</a>
 *
 *		To use some other text as the link text:
 *			$('#email_link').safe_mail({
 * 								        username: "john.doe",
 *                                      domain_name: "domain",
 *                                      domain_ext: "com",
 *                                      link_txt: "This is my email link text"
 *           });
 *			Ouput: <a href="mailto:john.doe@domain.com" class="safeMail">This is my email link text</a>
 *
 *      In addition, a class of your own choosing can be assigned to the link by passing class: 'myCustomClass'
 *      in the call to safe_mail:
 *			$('#email_link').safe_mail({
 * 								        username: "john.doe",
 *                                      domain_name: "domain",
 *                                      domain_ext: "com",
 *                                      class: "myCustomClass",
 *                                      link_txt: 'link'
 *           });
 *			Ouput: <a href="mailto:john.doe@domain.com" class="myCustomClass">john.doe@domain.com</a>
 *
 *       It may not always be desirable to have the email address be a mailto link, so there is another value
 *       that can be passed to the safe_mail call that wraps the email address in a div:
 *			$('#email_link').safe_mail({
 * 								        username: "john.doe",
 *                                      domain_name: "domain",
 *                                      domain_ext: "com",
 *                                      link_txt: 'link',
 *                                      class: 'myCustomClass',
 *                                      is_link: false
 *           });
 *			Ouput: <div class="safeMail">john.doe@domain.com</div>
 */

jQuery.fn.safe_mail = function(options) {
	// Setup defaults for our options
	var defaults = {
		username: 'nospam',
		domain_name: 'domain',
		domain_ext: 'com',
		link_txt: 'link',
		theClass: 'safeMail',
		is_link: true
	}
	
	var opts = jQuery.extend(defaults, options);
	
	// First we want to know if this is an actual link
	if(opts.is_link == true) {
		// If no link text(link_txt) is specified, or if the user explicitally sets link_txt to "link"
		if(opts.link_txt == "link") {
			// Use the link its self as the link text
			mail_link = "<a " + "href" + "=" + "'mail" + "to" + ":" + 
						opts.username + "@" + opts.domain_name + "." + opts.domain_ext + 
						"' class='" + opts.theClass + "'>" + 
						opts.username + "@" + opts.domain_name + "." + opts.domain_ext + 
						"</a>";
	} else {
			// Otherwise use the user provided link text
			mail_link = "<a " + "href" + "=" + "'mail" + "to" + ":" +  
						opts.username + "@" + opts.domain_name + "." + opts.domain_ext + 
						"' class='" + opts.theClass + "'>" + opts.link_txt + 
						"</a>";
		}
	// If its not an actual link
	} else {
		// This is identical to the above, but we're replacing the anchor tag with a div so we can do something else with it.
		if(opts.link_txt == "link") {
			// Use the link its self as the link text
			mail_link = "<div class='" + opts.theClass + "'>" + 
						opts.username + "@" + opts.domain_name + "." + opts.domain_ext + 
						"</div>";
		} else {
			// Otherwise use the user provided link text
			mail_link = "<div class='" + opts.theClass + "'>" + opts.link_txt +"</a>";
		}
	}
	
	jQuery(this).append(mail_link);
};
