var historyManager = new HistoryManager();
var theme = new Theme();
var loader = new Request.HTML({method:'get', 'nocache':true});

window.addEvent('domready', function()
{
	if(location.hash != '')
	{
		document.id(theme.contentDiv).set('html', '');
		loader.get('page.php?e=1&q=' + location.hash.substring(1));
	}
	
	historyManager.addEvent('onHistoryChange', function(hash)
	{
		if(hash == '') { hash = '/Home/'; }
		loader.get('page.php?e=1&q=' + hash);
	});
	
	loader.addEvent('onSuccess',function(tree,elements,html,js)
	{ 
		theme.LoadComplete(tree,elements,html,js);
		hashLoader(document.id(theme.contentDiv));
	});
	loader.addEvent('onRequest', theme.LoadStart);
	loader.addEvent('onFailure', theme.LoadFailure);
	
	hashLoader(document.id(document.body));
	
	theme.DomReady();
});

function hashLoader(base)
{
	base.getElements('a').each(function(a)
	{
		if(a.get('href').substring(0,1) == "/")
		{
			a.set('href','#' + a.get('href'));
		}
	});
	
	//fix local anchors if a js-enabled browser is on a non-js page
	var currentLoc = location.href.substring(7);
	var uriParts = currentLoc.split('/');

	if(uriParts.length > 2 && uriParts[1] != '#')
	{
		base.getElements('a').each(function(a)
		{
			if(a.get('href').substring(0,7) != 'http://')
			{
				a.set('href','http://matt.malensek.net/' + a.get('href'));
			}
		});
	}
}