// This script is (c) copyright 2006 Jim Tucek under the
// GNU General Public License (http://www.gnu.org/licenses/gpl.html)
// For more information, visit www.jracademy.com/~jtucek/email/ 
// Leave the above comments alone!
var decryption_cache = new Array();
function decrypt_string(crypted_string,n,decryption_key,just_email_address) {
	var cache_index = "'"+crypted_string+","+just_email_address+"'";
	if(decryption_cache[cache_index])
		return decryption_cache[cache_index];
	if(addresses[crypted_string])
		var crypted_string = addresses[crypted_string];
	if(!crypted_string.length)
		return "Error, not a valid index.";
	if(n == 0 || decryption_key == 0) {
		var numbers = crypted_string.split(' ');
		n = numbers[0];	decryption_key = numbers[1];
		numbers[0] = ""; numbers[1] = "";
		crypted_string = numbers.join(" ").substr(2);
	}
	var decrypted_string = '';
	var crypted_characters = crypted_string.split(' ');
	for(var i in crypted_characters) {
		var current_character = crypted_characters[i];
		var decrypted_character = exponentialModulo(current_character,n,decryption_key);
		if(just_email_address && i < 7)	
			continue;
		if(just_email_address && decrypted_character == 63)
			break;
		decrypted_string += String.fromCharCode(decrypted_character);
	}	
	decryption_cache[cache_index] = decrypted_string;
	return decrypted_string;
}
function decrypt(crypted_string,n,decryption_key,plain) {
	if(!n || !decryption_key) { n = 0; decryption_key = 0; }
	if(!crypted_string) crypted_string = 0;
	var decrypted_string = decrypt_string(crypted_string,n,decryption_key,plain);
	return decrypted_string;
}
function exponentialModulo(base,exponent,y) {
	if (y % 2 == 0) {
		answer = 1;
		for(var i = 1; i <= y/2; i++) {
			temp = (base*base) % exponent;
			answer = (temp*answer) % exponent;
		}
	} else {
		answer = base;
		for(var i = 1; i <= y/2; i++) {
			temp = (base*base) % exponent;
			answer = (temp*answer) % exponent;
		}
	}
	return answer;
}
var addresses = new Array();
addresses.push("1339 245 772 1185 547 1271 493 323 392 293 493 277 677 277 402 514 1271 1063 514 547 293 104 547 327 103 493 1185 988 957 323 327 1318 674 1139 323 772 540 293 988 583 552 277 1139 493 627 448 277 583 293 547 493 277 192 277 327 1017 988 547 680 1063");
$(document).ready(function() {
	$('#header_right').append('<div id="contact"></div>');
	$('#contact').hide();
	$('#header_right h3#sh a').click(function() {	
		var rewriteEmail = function() {
			var txt = decrypt(0,null,null,true);
			var link = decrypt(0,null,null,false);
			$('#contact div > span').before('<a href=' + link + '>' + txt + '</a>').remove();
 		}
		rewriteEmail();

		$('#contact').load('/contact',function(){
   			rewriteEmail();
			$('#header_right img').hide();
			$(this).slideToggle();
 		});
		$('#header_right h3#sh').append('<img width="32" height="32" src="/images/loading.gif" alt="loading..." />');
		$('#header_right h3#sh').css("position","relative");
		$('#header_right h3#sh').css("z-index","10");
		//$('#contact h4').hide();  // need to avoid this with IE6 so use negative margin instead
		$('#contact').css("margin-top","-65px");
		return false;
	});
});