/*
	Plugin: yasp
	Autor: Björn M. Wibben (hi (at] wibben [punkt) de)
	Version: 1.2

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

	$(".nameMeLikeYouWant").yasp();
	$(".nameMeLikeYouWant2").yasp({linktext: "contact me"});
	
	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
	(see: http://www.wibben.de/email-schutz-mit-jquery#comment-12)!
*/

(function($) {

$.fn.yasp = function (options) {

	// set options
	var opts = $.extend({}, $.fn.yasp.defaults, options);
	$(this).each(function () {
		// 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
		!inner || inner === et ? inner = p : null;
		// 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
			$(this).replaceWith('<a class="'+classes+'" href="mai'+'lto:'+p+'">'+opts.linktext+"</a>");
		} else {
			// ohterwise place the valid email-address or what has been the inner html inside the ancor tag
			$(this).replaceWith('<a class="'+classes+'" href="mai'+'lto:'+p+'">'+inner+"</a>");	
		}
	});

};

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

})(jQuery);