/**
 * MCrypt (MailCrypt)
 * @author: marco.pegoraro@gmail.com
 * http://www.consulenza-web.com
 * 
 * Very simple mail obfuscator to prevent spam from web sites.
 * Every mail link is published as a clean A tag with a mailto: directive.
 * We write a simple crypted mail address booth on href and text: 
 * "the first and last character of the string is exchanged".
 *
 * This version on e-mail does not extist!
 * Then we assign ad identifiel (class, id) to call our plugin:
 *
 * $('.mailto').mcrypt();
 * 
 */

(function($) {

	$.mcrypt = {};
	
	$.extend($.mcrypt,{ foo:null
		,defaults: {}
		
		// Very very simple decryptation function.
		// the first and last character of the string is exchanged.
		,decrypt: function( str ) {
			
			var arr 		= str.split("");
			var newArr 		= Array();
			var newStr		= '';
			
			newArr[0] = arr[arr.length-1];
			for ( i=1;i<(arr.length-1);i++ ) newArr[i] = arr[i];
			newArr[newArr.length] = arr[0];
			
			
			for ( i=0; i<newArr.length; i++ ) newStr += newArr[i];
			
			return newStr;
			
		}
		
	});
	
	$.fn.extend({ foo:null
		
		,mcrypt: function(settings) {
			
			if ( !this.length ) return this;
			
			// setup configuration
			settings = $.extend({}, $.mcrypt.defaults, {	
			}, settings);
			
			// work on mail string.
			var mail 	= $(this).attr('href').substr(7,255);
			var newMail = $.mcrypt.decrypt(mail);
			
			// Replace old mail address with decrypted version.
			$(this).attr('href','mailto:'+newMail);
			
			// Replace old mail address in tag content.
			var cnt = $(this).html();
			cnt = cnt.replace(mail,newMail);
			$(this).html(cnt);
			
			// return object.
			return this;
		}
		
		,mycryptGetMail: function( settings ) {
			
			if ( !this.length ) return null;
			
			// setup configuration
			settings = $.extend({}, $.mcrypt.defaults, {	
			}, settings);
			
			// work on mail string.
			var mail 	= $(this).attr('href').substr(7,255);
			
			return $.mcrypt.decrypt(mail);
			
		}
		
	});

})(jQuery);
