/*
	Plugin: yasp
	Autor: Björn M. Wibben (hi (at] wibben [punkt) de)
	Version: 1.3
	
	What's new:
	The plugin is now chainable e. g. $(".nameMeLikeYouWant").yasp().hide().fadeIn("slow");

	description:
	yasp - Yet Another Spam Protection
	Requires jQuery Version 1.2.6 or above (tested with 1.3.2 too)
	
	usage:
	
	jq:
	// set default value:
	$.fn.yasp.defaults.linktext = "Send me an eMail";

	$(".nameMeLikeYouWant").yasp();
	$(".nameMeLikeYouWant2").yasp({linktext: "contact me"});
	
	@return jQuery
	
	html:
	<span class="nameMeLikeYouWant some other classes goes here">info (at) domain [dot] de</span> or even
	<span class="nameMeLikeYouWant">info (bei) domain [punkt] de</span> or
	<span class="nameMeLikeYouWant2">name [dot] surname (at) domain [dot] de</span>
	<span class="nameMeLikeYouWant2"><img src="email.gif"></span>
	
	Special Thanks to Fredrik, who made some important extensions to this plugin in Version 1.0.
	(see: http://www.wibben.de/email-schutz-mit-jquery#comment-12)!
	
	This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License (http://creativecommons.org/licenses/by-nc-sa/3.0/)
*/

(function($) {

$.fn.yasp = function (options) {

	// set options
	var opts = $.extend({}, $.fn.yasp.defaults, options);
	var ret = [];
	this.each(function (i) {
		var $this = $(this);
		var $nc;
		// backup classes
		var classes = $this.attr("class");
		// get the rel attribute
		var rel = $this.attr("rel");
		// set emailtext to the value of rel or get the text from the span tag
		var et = rel || $this.text();
		// get the inner html from the span tag
		var inner = $this.html();
		// do some regex to make a valid email-adress
		var bei = et.replace(/\s\(\w*\)\s/, "@");
		var p = bei.replace(/\s\[\w*\]\s/g, ".");
		// check if inner equals to emailtext and replaces it with the valid email-adress
		if (!inner || inner === et) {inner = p;}
		// if ther was a linktext given to the options and there was no rel tag
		if (opts.linktext !== null && !rel) {
			// make the adress with the given linktext
			$nc = $('<a class="'+classes+'" href="mai'+'lto:'+p+'">'+opts.linktext+"</a>");
			$this.replaceWith($nc);
			ret.push($nc[0]);
		} else {
			// otherwise place the valid email-address or what has been the inner html inside the ancor tag
			$nc = $('<a class="'+classes+'" href="mai'+'lto:'+p+'">'+inner+"</a>");
			$this.replaceWith($nc);
			ret.push($nc[0]);
		}
	});
	return $(ret);
};

// public access
$.fn.yasp.defaults = {linktext: null};

})(jQuery);
