/*--------------------------------------------------------------------------------
	emailDecoder.js
	
	Description: The DecodeEmail class unobtrusively decodes email addresses in 
	an HTML document. Email addresses are encoded in a form like this: <span 
	class="email">myname [at] mycompany [dot] com</span>. They are replaced with 
	valid email links: <a href="myname@mycompany.com">myname@mycompany.com</a>. 
	The strings representing the at-sign, period, and dash can all be changed by 
	passing parameters to the constructor.
	
	Author: Clifton Karnes (the idea comes from Jeremy Keith)
	
	Optional Constructor Arguments:
		at:		the regular expression text to use for the @ symbol: 
				" [at] " is the default
		dot:  	the regular expression text to use for the . symbol: 
				" [dot] " is the default
		dash: 	the regular expression text to use for the - symbol: 
				" [dash] " is the default
	                                                               
	Requires: prototype.js
	
	Usage: Instantiate this class once (no need to save a reference) to decode 
	all the document's email addresses. Using prototype you could use 
	document.observe("dom:loaded", function(){ new DecodeEmail() });;

	Notes: To override any of defaults, pass an object literal with regular 
	expression strings. For example, to use " (at) " for the @ symbol, since 
	both "(" and ")" are special characters in regular expressions, they need to 
	be escaped with "\". Plus, since "\" is a special character in JavaScript 
	stings, it needs to be escaped for JavaScript, so you need two \\'s escaped 
	twice. twice. To use " (at) " instead of " [at] ", you'd instantiate the 
	class like this: new DecodeEmail({ at: " \\(at\\) "

--------------------------------------------------------------------------------*/

var EmailDecoder = Class.create
({
	// Get any parameters that override the defaults and call fixEmail().
	initialize: function(options)
	{
		this.options = Object.extend({ at: " \\[at] ", dot: " \\[dot] ", dash: " \\[dash] " }, options || {});
		this.fixEmail();					
	},

	fixEmail: function()
	{
		// The strings we're replacing need to be converted to regular expressions for gsub().
		var at = new RegExp(this.options.at);
		var dot = new RegExp(this.options.dot);
		var dash = new RegExp(this.options.dash);
		
		// Get all the <span class="email" elements.
		$$("span.email").each
		(
		    function(el)
		    {
			    // Replace the at-text, dot-text, and dash-text with the real characters
		        txt = el.innerHTML;
		        txt = txt.gsub(at, "@");
		        txt = txt.gsub(dot, ".");
		        txt = txt.gsub(dash, "-");
				// Create a mailto anchor with the newly formatted email address.
		        txt = '<a href="mailto:' + txt + '">' + txt + "</a";
		        // Write the new email address to the document, replacing  the <span>.
		        el.replace(txt);
		    }
		);
	}
});
