/* Function to check hash of page and trigger a change event on window.location */
/* http://www.bennadel.com/blog/1520-Binding-Events-To-Non-DOM-Objects-With-jQuery.html */
(
	function( $ ){
		// Default variables to the current location
		var str_prev_href = "";
		var str_prev_hash = "";
		var str_loc_href = window.location.href;
		var str_loc_hash = window.location.hash;
		// Set timer for polling location changes
		var polling_timer = 250;
		// Function to remove # from hash
		var clean_hash = function(str_hash){return(str_hash.replace("#",""));}

		// Check for changes in the window location
		var check_location = function(){
			if((str_loc_href != window.location.href) || ((str_loc_hash.length > 0) && (str_prev_href.length == 0))){
				str_prev_href = str_loc_href;
				str_prev_hash = str_loc_hash;
				str_loc_href = window.location.href;
				str_loc_hash = window.location.hash;

				$(window.location).trigger(
					"change",
					{
						current_href: str_loc_href,
						current_hash: clean_hash(str_loc_hash),
						current_link: $(str_loc_hash),
						previous_href: str_prev_href,
						previous_hash: clean_hash(str_prev_hash)
					}
				); // $(window.location).trigger

			} // if(str_prev_href != window.location.href)
		} // var check_location function()

		// Set interval to check on location changes
		setInterval(check_location, polling_timer);

	} // function ( $ )
)( jQuery );

/* Check for a token in the window.location  */
function check_token(){
	if(window.location.hash){
		return true;
	}else{
		return false;
	}
} // function check_token

/* Get the current token from window.location */
function get_token(){
	var token = check_token();
	if(token){
		var hash = window.location.hash;
		hash = hash.replace("#","");
		return hash;
	}else{
		return false;
	}
} // function get_token

/* Set a token to window.location.hash */
function set_token(id){	
	window.location.hash = "#" + id;
	return true;
} // function set_token

/* Determine the url to be loaded from the link id */
function url_from_token(id){
	/* Will replace the following codes (precede hex codes [1-24] with a slash) */
	var char_replace = new Array(25);
	for(var i = 0; i < char_replace.length; i++){char_replace[i] = new Array(2);} // resize array
		char_replace[0][0] = "abs-";
			char_replace[0][1] = "/";
		char_replace[1][0] = "2F";
			char_replace[1][1] = "/";
		char_replace[2][0] = "2E";
			char_replace[2][1] = ".";
		char_replace[3][0] = "3F";
			char_replace[3][1] = "?";
		char_replace[4][0] = "3D";
			char_replace[4][1] = "=";
		char_replace[5][0] = "26";
			char_replace[5][1] = "&";
		char_replace[6][0] = "5B";
			char_replace[6][1] = "[";
		char_replace[7][0] = "5D";
			char_replace[7][1] = "]";
		char_replace[8][0] = "28";
			char_replace[8][1] = "(";
		char_replace[9][0] = "29";
			char_replace[9][1] = ")";
		char_replace[10][0] = "30";
			char_replace[10][1] = "0";
		char_replace[11][0] = "31";
			char_replace[11][1] = "1";
		char_replace[12][0] = "32";
			char_replace[12][1] = "2";
		char_replace[13][0] = "33";
			char_replace[13][1] = "3";
		char_replace[14][0] = "34";
			char_replace[14][1] = "4";
		char_replace[15][0] = "35";
			char_replace[15][1] = "5";
		char_replace[16][0] = "36";
			char_replace[16][1] = "6";
		char_replace[17][0] = "37";
			char_replace[17][1] = "7";
		char_replace[18][0] = "38";
			char_replace[18][1] = "8";
		char_replace[19][0] = "39";
			char_replace[19][1] = "9";
		char_replace[20][0] = "2D";
			char_replace[20][1] = "-";
		char_replace[21][0] = "5F";
			char_replace[21][1] = "_";
		char_replace[22][0] = "23";
			char_replace[22][1] = "#";
		char_replace[23][0] = "2C";
			char_replace[23][1] = ",";
		char_replace[24][0] = "2C";
			char_replace[24][1] = ",";
	if(id != false){
		url_to_load = id;
			for(var i = 0; i < char_replace.length; i++){
				var stringtoreplace = char_replace[i][0];
				var re = new RegExp("\\\\" + char_replace[i][0], "g");
				url_to_load = url_to_load.replace(re,char_replace[i][1]);
			}
		return url_to_load;
	}
	else{return false;}
} // function url_from_token

/* Load a page into a target id */
function load_page(id,target){
	if(arguments.length == 0){var id = get_token();}
	var token_set = set_token(id);
	if(token_set){
		var url_to_load = url_from_token(id);
		$(target).load(url_to_load); //function(responseText, textStatus, XMLHttpRequest){this;}
		$(target).attr("class",id);
		return true;
	}else{
		return false;
	}
} // function load_page

/* Change menu item states */
function menu_states(id){
	/* Add active class */
	$("a").filter(function(index){
		if($(this).attr("id") == id){
			return index;
		}else{
			return false;
		}
	}).addClass("active");

	$("a").filter(function(index){
		if($(this).attr("id") == id){
			return false;
		}else{
			return index;
		}
	}).removeClass("active");
} // function menu_states

// Bind a change handler to the window location
$(window.location).bind(
	"change",
	function(event,data){
		target = $("#content");
		load_page(data.current_hash,target);
		menu_states(data.current_hash);
	} // function(event,data)
); // $(window.location).bind

/*
Loads content, back button works, forward buton works, direct entry works, bookmarks work
Safari (for Windows) 3.2.2 (525.28.1)
Google Chrome 2.0.172.31

Loads content, back button works, forward buton works, direct entry works, bookmarks work, swaps \ in href to /
Firefox 3.0.10

Loads content, back button works, direct entry works, bookmarks work
Forward button does not work, must confirm scripts before function
Internet Explorer 7.0.5730.13

Loads content, back button works, forward button works, direct entry does not work, bookmarks do not work
Opera 9.64
*/
