/* 
 * Creuna Anti Spam (1.0)
 *
 * Copyright (c) 2010 Creuna (www.creuna.com)
 *
 * Requires:
 * - jQuery
 * - jQuery Cryptography Plug-in
 *
 * CHANGELOG:
 * v 1.0   - Initial release by Ayhan Binici
 *
 */

jQuery.fn.antispam = function(settings) {
	settings = jQuery.extend({
		base64: true,			// optional, whether or not the input is base64-encoded
		replaceText: false, 	// optional, whether or not the text-value to the a-element
		filterLevel: 'normal' 	// optional, accepts 'low' or 'normal'
	}, settings);
	
	return this.each(function(){
		e = null;
		if(settings.filterLevel == 'low')
		{
			// Fetch raw value
			if($(this).is('a[rel]'))
			{
				e = $(this).attr('rel');
			} else {
				e = $(this).text();
			}
			if(e)
			{
			    // Decoding
			    if(settings.base64)
			    {
				    e = $().crypt({method:"b64dec",source:e});
			    }
			    // Replacing
			    e = e.replace('//', '@').replace(/\//g, '.');
			}
		}
		else // 'normal'
		{
			// Fetch raw value
			if($(this).is('a[rel]'))
			{
				e = $(this).attr('rel');
			} else {
				e = $(this).text();
			}
			if(e)
			{
			    // Decoding
			    if(settings.base64)
			    {
				    e = $().crypt({method:"b64dec",source:e});
			    }
			    // Replacing
			    e = e.split('').reverse().join('').replace('//', '@').replace(/\//g, '.');
			}
		}
		if(e)
		{
			if($(this).is('a[rel]'))
			{
				$(this).attr('href', 'mailto:' + e);
				if(settings.replaceText)
				{
					$(this).text(e);
				}
			}
			else
			{
				$(this).text(e);
			}
		}
	});
};

