//  Plugin Name: Obfuscate
//	Description: jQuery plugin for obfuscating email addresses
//	
//	Author: T. Hahn
//	Version: 1.0
//	Author URI: http://www.HahnCreativeGroup.com/

// Obfuscate 1.0
(function($) {
	$.obfuscate = function(element, options) {
		this.options = {};
		element.data('obfuscate', this);
		this.init = function(element, options) {
			this.options = $.extend({}, $.obfuscate.settings, options);
			$.obfuscate.Obfuscator.stopAll(element, this.options);
		};
		
		this.init(element, options);
	};
	
	$.fn.obfuscate = function(options) {		
		return this.each(function() {
			(new $.obfuscate($(this), options));			
		});	
	};
	
	$.obfuscate.settings = {
		cssClass: 'cloaked'	
	}	
	
	$.obfuscate.Obfuscator = {
		p: "f='ma",
		t: String.fromCharCode(97),
		E: "/",
		A: String.fromCharCode(105, 108, 116),
		N: ">",
		o: " hre",
		s: "<",
		LL: "o:",
		D: "'",
		styleClass: "cloaked",
		seperatorOne: String.fromCharCode(64),
		seperatorTwo: String.fromCharCode(46),
	
		encoder: function(txt, ext, domain, prefix) {
			var encode;
			if (txt == undefined) {
				txt = prefix + this.seperatorOne + domain + this.seperatorTwo + ext;
			}
			encode = this.start() + prefix + this.seperatorOne + domain + this.seperatorTwo + ext + this.D + this.style() + this.N + txt + this.end();
	
			return encode;
		},
		
		style: function() {
			return(" class='" + this.styleClass + "'");
		},
	
		start: function() {
			return (this.s + this.t + this.o + this.p + this.A + this.LL);
		},
	
		end: function() {
			return (this.s + this.E + this.t + this.N);
		},
		
		stopAll: function(el, opt) {		
			this.styleClass = opt.cssClass;
			var active;
			var viewText;
			
			var title = $(el).attr("title");
			var params = title.split("::");
			
			var settings = params[1].split("|");
			var data = params[0].split("|");
			
			if(settings != -1)
			{
				active = settings[0];
				viewText = settings[1];
			}
			else
			{
				active = settings;
			}
			
			var address = data[2]+"@"+data[1]+"."+data[0];
			
			if(active == "true")
			{
				$(el).replaceWith(this.encoder(viewText,data[0],data[1],data[2]));
			}
			else
			{
				$(el).text(address).removeClass($(this).attr('class')).addClass(this.styleClass);
			}	
		}
	};
})(jQuery);
