$(document).ready(function() {
	setInterval(loadFromHash, 200);
	var recentHash = '';
	function loadFromHash() {
		// hash was not changed, abort
		if(window.location.hash == recentHash) return;
		recentHash = window.location.hash;
		
		// read url from hash
		if(window.location.hash) {
			var url = window.location.hash.replace('#!', '');
		} else {
			var url = window.location.href;
			url = url.replace(/http:\/\/[^\/]+/i, '');
		}
		
		// load page title
		loadPageTitle(url);
		
		// load page contents
		$('div#content').load(url+'?nolayout=true', function() {
			$('ul#navigation li').removeClass('current');
			$('ul#navigation li a[href='+url+']').parent().addClass('current');
			enableContentLinks();
		});
	}
	
	function loadPageTitle(url) {
		$.ajax({
			type: 'GET',
			url: url+'?justtitle=true',
			success: function(title) {
				document.title = title;
			}
		});
	}
	
	// main navigation
	$('ul#navigation').find('a').each(function() {
		$(this).click(function() {
			var link = $(this);
			window.location.hash = '!'+link.attr('href');
			recentHash = window.location.hash;
			$('div#content').load(link.attr('href')+'?nolayout=true', function() {
				loadPageTitle(link.attr('href'));
				$('ul#navigation li').removeClass('current');
				link.parent().addClass('current');
				enableContentLinks();
			});
			return false;
		});
	});
	
	// infobar links
	$('div#infoBar p').find('a').each(function() {
		$(this).click(function() {
			var link = $(this);
			window.location.hash = '!'+link.attr('href');
			recentHash = window.location.hash;
			$('div#content').load(link.attr('href')+'?nolayout=true', function() {
				loadPageTitle(link.attr('href'));
				$('ul#navigation li').removeClass('current');
				enableContentLinks();
			});
			return false;
		});
	});
	
	enableContentLinks();
	
	// links in content itself
	function enableContentLinks() {
		$('div#content').find('a').each(function() {
			var link = $(this);
			// only make ajax link if it's no external link, no file, no ancor and no image
			if(!link.attr('href').match(/(\#|\.[\w\d]{1,5})/i)) {
				$(this).click(function() {
					window.location.hash = '!'+link.attr('href');
					recentHash = window.location.hash;
					$('div#content').load(link.attr('href')+'?nolayout=true', function() {
						loadPageTitle(link.attr('href'));
						enableContentLinks();
					});
					return false;
				});
			}
		});
		
		// enable fancybox for loaded content
		$('.fancybox').fancybox();
	}
});

//enable links in fancybox content
function enableFancyboxLinks() {
	return {
		// make ajax links in fancybox content
		onComplete: function() {
			$('#fancybox-inner').find('a').each(function() {
				var link = $(this);
				// only make ajax link if it's no external link, no file, no ancor and no image
				if(!link.attr('href').match(/(\#|\.[\w\d]{1,5})/i)) {
					$(this).click(function() {
						$.fancybox.close();
						window.location.hash = '!'+link.attr('href');
						recentHash = window.location.hash;
						$('div#content').load(link.attr('href')+'?nolayout=true', function() {
							loadPageTitle(link.attr('href'));
							enableContentLinks();
						});
						return false;
					});
				}
			});
		}
	}
}
