//v0.7

jQuery.fn.extend({
	ajaxify: function() {
		/*
		//capture the url
		var ajax_url = $(this).attr("href");
		//needs this check to keep the URL from compounding
		if( window.location.href.indexOf("#") == -1 ){
			var nice_url = window.location.href + "#" + $(this).attr("href").replace("http://"+window.location.host, "");
		}else{
			var nice_url = window.location.href.substring(0, window.location.href.indexOf("#")) +
				"#" + $(this).attr("href").replace("http://"+window.location.host, "");
		}
		
		*/
		
		//capture the url
		var ajax_url = $(this).attr("href");
		var nice_url = site_url + "#/" + $(this).attr("href").replace(site_url, "");
		
		//if these exist, kill them
		if($("#loading")) 	$("#loading").remove();
		if($("#holder")) 	$("#holder").remove();
		if($("#blocker")) 	$("#blocker").remove();
		
		//setup and run ajax page request
		$.ajax({
			type: "POST",
			url: ajax_url,
			dataType: "html",
			
			//before anything happens, lets set up some magic
			beforeSend: function(){
				//add the page blocking div after the body
				$("body").after('<div id="blocker"></div>');
				//set it up to cover the page
				$("#blocker").css({
					"position": "absolute",
					"width":      $(document.body).width(),
					"height":     $(document.body).height(),
					"top": 		0,
					"left": 	0,
					"opacity": 	0,
					"zIndex": 	999
				});
				/*capture clicks*/
				$("#blocker").click(function(){
					return false;
				});
				
				//grab the position of the #content div before we touch it
				var pos = $("#content").position();
				//add the #loading div to the page
				$("#content").after('<div id="loading"></div>');
				//set it up
				$("#loading").css({
					"position": "absolute",
					width: 		$("#content").outerWidth(true),
					height: 	$("#content").outerHeight(true),
					top: 		pos.top,
					left: 		pos.left,
					"background-color": "#E9158D",
					"text-align": "center",
					opacity: 0
				});
				//add the loading gif iside of it
				$("#loading").append('<img id="loading-gif" src="'+template_url+'images/loading.gif" />');
				//position the gif
				$("#loading-gif").css({
					"paddingTop": ($("#content").outerHeight(true)/2) - ($("#loading-gif").outerHeight(true)/2)
				});
				//finally fade in the #loading div and then fade out the #content div
				$("#loading").fadeTo(500, 0.70, function(){
					$("#content").fadeOut("slow");
				});
				
			},
			
			//when we know the ajax request has worked and returned
			success: function(responseText, textStatus){
				//replace title for sweetness
				var title = responseText.match(/<title[^>]*>(.*)<\/title>/i);
				if(title[1]){
					document.title = html_entity_decode(title[1]);
				}
				
				//rewrite the url
				window.location.href = nice_url;
				
				//add a div to hold the returned page
				$("body").after('<div id="holder"></div>');
				//hide it
				$("#holder").hide();
				//add the content of the #content div of the returned page to the holder 
				$("#holder").append( $(responseText).find("div#content").html() );
				
				//track this page in GA
				if(typeof pageTracker != "undefined"){
					pageTracker._trackPageview("/"+ajax_url);
					//console.log("/"+ajax_url);
				}				
				
				//55 is a totall arbitrary number because the full hight is not known?
				var content_height = $("#holder").outerHeight(true);				
				
				//move the loading gif to the middle of the content (or there about)
				$("#loading-gif").animate({
					"paddingTop": (content_height/2)
				}, "slow");
				
				//make the #loading div move to the full content height for style sake
				$("#loading").animate({
					"height": content_height +"px"
				},"slow", function(){
					
					//when that's done, fill the #content div with the ajaxed #holder content
					$("#content").html( $("#holder").html() );
					//slide down the #content
					$("#content").slideDown("slow", function(){
						//fade back out the #loading div
						$("#loading").fadeTo(500, 0, function(){
							//kill the divs that we added to free up the memory
							$("#loading").remove();
							$("#holder").remove();
							$("#blocker").remove();
						});
					});
				});
				
				
			},
			
			//this gets run after success and error no matter what the result.
			complete: function(){			
				//add pagetracker, pass ajax_url
				//track this page in GA
				/*if(typeof pageTracker != "undefined"){
					pageTracker._trackPageview("/"+ajax_url);
					//console.log("/"+ajax_url);
				}*/
			},
			
			//if the ajax request fails, fix it
			error: function(){
				$("#loading").remove();
				$("#holder").remove();
				$("#blocker").remove();
				$("#content").slideDown("slow");
			}
			
		});
			
	}
});

//Fix the page titles that have html entities in them
function html_entity_decode(str) {
	var ta=document.createElement("textarea");
	ta.innerHTML=str.replace(/</g,"&lt;").replace(/>/g,"&gt;");
	return ta.value;
}