/**
 *	DZ Email Decryptormatic
 *	@version 	1.00
 *	@author		Ryan Sexton
 *	
 *	@note	Remove this entire comment block before posting it live
 *
 *	Decrypt encrypted emails
 *
 *	How to use:
 *		Will can all A tags
 *		$('a').DZEmailDecryptomatic();
 *
 *		Will scan all A tags with the class name (this can be whatever you want)
 *		$('a.dzemails').DZEmailDecryptomatic();
 *
 *	How it works:
 *		Looks for an href attribute, if it has one it then
 *		checks to see if there's a mailto: text in that href tag
 *		If the information after the mailto: tab is NOT an email address
 *		then it processes it.
 *
 *		Example that does not get decrypted
 *		<a href="mailto:test@test.com">test@test.com</a>
 *
 *		Example that does get decrypted
 *		<a href="mailto:asdlfjaasdfljasdf">asdlfjaasdfljasdf</a>
 *
 *		Another words, it tries to find encrypted mailtos and decrypt them
 *
 *	@downside 	this degrades poorly, if someone doesn't have javascript
 *
 *	Real world example
 *
 *	
 	<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
	<html>
	<head>
	 	<script type="text/javascript" src="js/jquery.js"></script>
		<script type="text/javascript" src="js/jquery.dz.js"></script>
		<script>
		//<!--
			$(document).ready(function(){
			    $('a').DZEmailDecryptomatic();
			});
		//-->
		</script>
	</head>
	<body>
		<a href="mailto:dGVzdEB0ZXN0LmNvbQ==">dGVzdEB0ZXN0LmNvbQ==</a>
		<p>The above should say test@test.com</p>
	</body>
	</html>
 */
(function($)
{
	$.fn.DZEmailDecryptomatic = function(a) 
	{	
		return this.each(function() {
			if ($(this).attr('href') != undefined) {
				if ($(this).attr('href').substr(0, 7) == 'mailto:') {
					if ($.__isEmail($(this).attr('href').substr(7)) == false) {
						$(this).attr('href', 'mailto:'+$.__b64decode($(this).attr('href').substr(6)));
		    			$(this).text($.__b64decode($(this).text()));
					}
				}
			}
		});
	}

	$.__isEmail = function(str) 
    {
	    var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
	    return regex.test(str);
    }
	    
	$.__b64decode = function(input) 
	{   
		var output = "";
        var chr1, chr2, chr3;
        var enc1, enc2, enc3, enc4;
        var i = 0;
        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
		_keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
		
        while (i < input.length) {
            enc1 = _keyStr.indexOf(input.charAt(i++));
            enc2 = _keyStr.indexOf(input.charAt(i++));
            enc3 = _keyStr.indexOf(input.charAt(i++));
            enc4 = _keyStr.indexOf(input.charAt(i++));

            chr1 = (enc1 << 2) | (enc2 >> 4);
            chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
            chr3 = ((enc3 & 3) << 6) | enc4;

            output = output + String.fromCharCode(chr1);

            if (enc3 != 64) {
                output = output + String.fromCharCode(chr2);
            }
            if (enc4 != 64) {
                output = output + String.fromCharCode(chr3);
            }
        }

        output = $.__utf8_decode(output);
    return output;
    }
    
    $.__utf8_decode = function(utftext) 
    {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {
            c = utftext.charCodeAt(i);
            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            } else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            } else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }
        }
        return string;
    }

})(jQuery);