jQuery.fn.pending_request = false;

// by alastair weakley and greg turner, interaction consortium
// depends on jquery.livequery

jQuery.fn.ajaxify_contents = function(settings){
		
		settings = jQuery.extend({
			//defaults
		},settings);

		//takes all links and makes it a bookmark version of itself.
		var ajaxify_links = function(){
			
			
			$("a.ajaxify").livequery(function() {
				var $this = $(this);
				$this.attr('href', '#' + $this.attr('pathname').replace(/(^[^/])/,'/$1'));
				$this.click(function() {
					jQuery.fn.ajax_request($(this).attr('href'));
				});
			});
			
			
		}
		
		//ON LOAD
		jQuery.fn.check_for_hash(true);
		ajaxify_links();
		setInterval("jQuery.fn.check_hash_changed()", 200);
};

jQuery.fn.check_for_hash = function(initial) {
		// load any page specified in a direct url call.
		hash = document.location.hash; // eg. "#/contact/"
		if(hash && hash !== "#") jQuery.fn.ajax_request(hash, initial);
		jQuery.fn.old_hash = hash;
}

jQuery.fn.ajax_request = function(loc, initial) {
	if (jQuery.fn.pending_request){ //pending_request is global
		// cancel any pending requests
		jQuery.fn.pending_request.abort();
		jQuery.fn.pending_request = false;
	}
	if (initial) var sf = jQuery.fn.ajaxify_contents.initial_pre_replace; else var sf = jQuery.fn.ajaxify_contents.pre_replace;
	jQuery.fn.pending_request = $.ajax({
		type: "GET",
		url: loc.substr(1),
		success: sf
	});
}

jQuery.fn.check_hash_changed = function(){
	if (!jQuery.fn.pending_request) { //don't do anything if there is a request pending - we're already on it ay.
		hash = document.location.hash;
		if (hash != jQuery.fn.old_hash) {
			jQuery.fn.ajax_request(hash);
		}
		jQuery.fn.old_hash = hash;
	}
}


//Overridable, so you can specify different treatments for different incoming data.
//The default behaviour is simply to replace each element in the current page with the corresponding element in the incoming data.
//Afterwards, the ajax activate functions will be run

jQuery.fn.ajaxify_contents.initial_pre_replace = function(incoming_data){
	jQuery.fn.ajaxify_contents.replace(incoming_data, true);
}


jQuery.fn.ajaxify_contents.pre_replace = function(incoming_data){
	jQuery.fn.ajaxify_contents.replace(incoming_data, false);
}

jQuery.fn.ajaxify_contents.replace_element = function(incoming_element, incoming_element_id, initial) {
	var outgoing_element = $("#" + incoming_element_id); //only replace if the incoming content is different
	outgoing_element.replaceWith(incoming_element);
}

jQuery.fn.ajaxify_contents.replace = function(incoming_data, initial) {
	$(incoming_data).each(function() {
		var incoming_element = $(this);
		var myid = incoming_element.attr("id");
		if (myid) {
			switch (myid) {
				case "ajax_title":
					document.title = incoming_element.html();
					break;
				default:
					jQuery.fn.ajaxify_contents.replace_element(incoming_element, myid, initial);
			}
		}
	});
	//finally, run any init code
	jQuery.fn.ajaxify_contents.post_replace(incoming_data, initial);
}

jQuery.fn.ajaxify_contents.post_replace = function(incoming_data, initial){
	
}



