
var doFadeIn = false;
var swap = "";
var fadeSpeed = 400;
var beingClicked = false;
var newUrl;
var newTitle;

String.prototype.replaceAll = function(stringToFind,stringToReplace){
	var temp = this;
	var index = temp.indexOf(stringToFind);

	while(index != -1){
		temp = temp.replace(stringToFind,stringToReplace);
		index = temp.indexOf(stringToFind);
	}
	return temp;
}

String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

function iAmReady( )
{
	//When page has loaded, replace images and flash
	$("#main img").css("visibility", "visible");
	$("#main #flashPlayer").css("visibility", "visible");
}

function areYouReady(  )
{
	if( doFadeIn )
	{
		//data loaded and fadeout complete
		$("#main").html(swap);
		$x = $(".enc").text($.rotate13($(".enc").text()))
		$(".enc").attr("href", "mailto:" + $x.text());
		$("#main img").css("visibility", "hidden");
		$("#main #flashPlayer").css("visibility", "hidden");
		swap = "";
		$("#main").fadeIn( fadeSpeed, iAmReady );

		whenYoureReady();
		doFadeIn = false;
		beingClicked = false;
	}
	else
	{
		//what happens when the fade out completes completes before data loaded
		doFadeIn = true;
	}
}

function whenYoureReady()
{
	//onload..
	$("#innerbody a:not(.enc)").click( function (eventObj) {
		if( $(this).attr("href").indexOf("://") >= 0)
			return;

		if( beingClicked )
			return;
		beingClicked = true;

		window.location.hash = $(this).attr("href").substring( $(this).attr("href").indexOf("#"));
		loadUrlFromHash();
		return false;
	});

	(function($) {
		 $.rotate = function(s) {
			  return $.rotate13($.rotate5(s));
		 }

		 $.rotate5 = function(s) {
			  var b = [],c,i = s.length,a = '0'.charCodeAt(),z = a + 10;
			  while (i--) { 
					c = s.charCodeAt(i);
					if (c >= a && c < z) { b[i] = String.fromCharCode(((c - a + 5) % (10)) + a); }
					else { b[i] = s.charAt(i); }
			  }
			  return b.join('');
		 };

		 $.rotate13 = function(s) {
			  var b = [],c,i = s.length,a = 'a'.charCodeAt(),z = a + 26,A = 'A'.charCodeAt(),Z = A + 26;
			  while (i--) {
					c = s.charCodeAt(i);
					if (c >= a && c < z) { b[i] = String.fromCharCode(((c - a + 13) % (26)) + a); }
					else if (c >= A && c < Z) { b[i] = String.fromCharCode(((c - A + 13) % (26)) + A); }
					else { b[i] = s.charAt(i); }
			  }
			  return b.join('');
		 };
	})(jQuery)

	$("#main #flashPlayer").css("visibility", "hidden");
	doFadeIn = true;
	loadUrlFromHash();
	setInterval(loadUrlFromHash, 3000);
}

function loadUrlFromHash()
{
	//Called by polling, to reload when back button pressed
	var hash = window.location.hash.substring(1);
	if( newUrl == hash || !hash || hash.trim()=="" )
		return;	

	newUrl = window.location.hash.substring(1);
	myUrl = webroot + "\/Read\/Ajax\/" + newUrl;
	newTitle = newUrl.replaceAll("-", " ");
	document.title = newTitle + " - bradspreadsheet.net";

	$("#main img").css("visibility", "hidden");
	$("#main #flashPlayer").css("visibility", "hidden");
	doFadeIn = false;
	$("#main").fadeOut( fadeSpeed, areYouReady );
	$.get( myUrl, function(data) {
		swap = data;
		areYouReady();
	});
}


$("document").ready( function() {
	whenYoureReady();


});
