var load_fade = function(obj, data) {
	var fade = 70;

	obj.fadeOut(fade, function() {
		obj.html(data);
		obj.fadeIn(fade);
	});
};

var load = function(page) {
	// check if content exists in cache
	if (cache[page]) {
		// load cache
		load_fade($('#content'), cache[page]);
	}
	// if not, get it with ajax
	else {
		$.get(page, function(data) {
			// print page
			load_fade($('#content'), (data));

			// cache data
			cache[page] = data;
		});
	}
};

// check if 'onhashchange' is available
if ("onhashchange" in window) {
		window.onhashchange = function(e) {
				if (location.hash.substr(0, 2) == "##") {
					load(location.hash.substr("##".length));
				}
				else {
					load('content/'+location.hash.substr("#".length));
				}

				/*
				// check if the history api is available
				if (typeof history.pushState !== 'undefined') {
						// make the url prettier
						history.pushState(page, page, page);
						location.hash = '';
				}
				*/
		};
}
// emulate 'onhashchange'
else {
		var hash = location.hash;
		setInterval(function() {
				if (location.hash != hash) {
						if (location.hash.substr(0, 2) == "##") {
							load(location.hash.substr("##".length));
						}
						else {
							load('content/'+location.hash.substr("#".length));
						}
						hash = location.hash;
				}
		}, 200);
}

var cache = {};

/*
if (typeof location.pathname !== 'undefined' && 
			location.pathname != '/') {
	load(location.pathname.substr('/'.length)); 
}
else */if (location.hash != "") {
	if (location.hash.substr(0, 2) == "##") load(location.hash.substr("##".length));
	else load('content/'+location.hash.substr("#".length));
}
else {
	load('content/experiments');
}

// some logic to add a secret extra tab
if (location.hash == '#tu') {
	$('#nav > ul').append('<li><a href="#tu" class="tu">School</a></li>');
}

$('body').noisy({
    'intensity' : 1, 
    'size' : 200, 
    'opacity' : 0.025, 
    'monochrome' : true
});
