// ROT13 routine nicked from Scott Yang, http://scott.yang.id.au/

Rot = {
	map: null,
	cur: null,

	convert: function(a) {
		var s = "";
		for (i=0; i < a.length; i++) {
			var b = a.charAt(i);
			s += ((b>='A' && b<='Z') || (b>='a' && b<='z') ? Rot.map[b] : b);
		}
		return s;
	},

	init: function(n) {
		if (Rot.cur == n) return;

		var map = new Array();
		var s   = "abcdefghijklmnopqrstuvwxyz";

		for (i=0; i<s.length; i++)
			map[s.charAt(i)] = s.charAt((i+n+26)%26);
		for (i=0; i<s.length; i++)
			map[s.charAt(i).toUpperCase()] = s.charAt((i+n+26)%26).toUpperCase();

		Rot.map = map;
		Rot.cur = n;
	},

	decryptMailto: function() {
		// Decode all email addresses taking the form encrypted_mailto:$rot_by$str$
		var objs = document.getElementsByTagName('A');
		var s, es, i, enc, n, dec;

		for (i = 0; i < objs.length; i++) {
			es = "encryptedmailto://";
			s = new String(objs[i].href);
			if (s.indexOf(es) > -1) {
				enc = s.substring(s.indexOf(es) + es.length + 1);
				n = enc.substring(0, enc.indexOf('$'));
				enc = enc.substring(enc.indexOf('$') + 1);
				if (enc.indexOf('$') == -1) {
					//alert('The e-mail address could not be decrypted due to a bug in your Javascript engine.  (Google Chrome?)\n\nSorry!');
					//
					// Seems that Google Chrome URL-encodes the string
					// *after* the first 2 rounds through substring()
					// (scanning for the first $-sign works fine).
					//
					// Also, the debugger "print enc" command shows
					// the string in unencoded form?  Anyway, the hack
					// below seems to set things straight for Chrome.
					enc = unescape(enc);
				}
				enc = enc.substring(0, enc.indexOf('$'));
				Rot.init(-n);
				dec = Rot.convert(enc);
				objs[i].href = 'mailto:' + dec;
				objs[i].innerHTML = dec;
			}
		}
	}
}

signal("rot_loaded");

