/*
 * Very basic obsucation of 'mailto' links using progressive enhancement. Customised for CSTS.
 *
 * This script must be preceeded by a reference to jQuery
 * - Eg: <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
 *
 * Sample markup: 
 * - Raw html        : <a href="/mailer.php#herbertmould(at)btinternet.com&subject=CSTS%20Visits">contact Herbert Mould</a>
 * - is converted to : <a href="mailto:herbertmould@btinternet.com&amp;subject=CSTS%20Visits">contact Herbert Mould</a>
 *
 * Written by George Adamson of Software Unity Ltd.
 * - http://twitter.com/GeorgeAdamson
 * - http://www.SoftwareUnity.com
 *
 * Copyright Software Unity Ltd 2010.
 * Licensed for use on http://www.cirenscience.org.uk (CSTS) contact Richard Gunner.
 * 
 */ 

(function($) {

	// Call our custom mailto converter when the DOM has loaded:
	$(function($) {

		// Locate and convert all links that look like our obfuscated mailto links:
		$("A[href*='mailer.php'][href*='#'][href*='(at)']").mailto();

	});


	// Encapsulate mailto-converter functionality as a new jQuery method:
	$.fn.mailto = function(options) {

		var defaults = {
			at_symbol : '(at)'	// Can be a string or regex.
		}
		options = $.extend( {}, defaults, options );

		this.each(function() {

			// Get everything after the #hash, fix the 'at' symbol and remove 'to=' prefix if present:
			var email = $(this).attr('href').split('#').pop().replace( options.at_symbol, String.fromCharCode(64) ).replace( /^to=/, '' );

			$(this).attr({ href: 'mailto:' + email });

		});

	};

})(jQuery);