var site = {
	rootUrl: "http://pncil.com/",
	currentPage: "",
	
	bootstrap: function()
	{
		window.onpopstate = site.onPopState;
		$('#content').wrap('<div id="container" />');
		$('<div id="fade_out" />').insertBefore('#container');
		this.bindAjaxLinks();
		this.currentPage = this.extractPageNameFromUrl(document.URL);
		this.onPageIn();
	},
	navigateTo: function(page)
	{
		if (page != site.currentPage)
		{
			this.load(page);
			history.pushState(site.currentPage, "pncil.com&rsaquo;", site.getContentUrl());
		}
	},
	navigateToUrl: function(url)
	{
		pageName = this.extractPageNameFromUrl(url);
		this.navigateTo(pageName);
	},
	load: function(page)
	{
		this.onPageOut();
		this.currentPage = page;
		
		$('#container').load(site.getContentUrl() + " #content", this.onPageIn);
	},
	onPopState: function(e)
	{
		if (e.state)
			site.load(e.state);
	},
	onPageIn: function()
	{
		site.bindAjaxLinks();
		if (site.currentPage == 'index')
			setTimeout("$('#header').removeClass('small')", 600);
//			$('#header').removeClass('small');
		else
			setTimeout("$('#header').addClass('small')", 600);
//			$('#header').addClass('small');
		
		$('#container').removeClass('out');
	},
	onPageOut: function()
	{
		$('#content').appendTo('#fade_out');
		$('#fade_out').addClass('out');
		setTimeout('$("#fade_out").empty(); $("#fade_out").removeClass("out");', 150);
		
		$('#container').remove();
		$('<div id="container" class="out" />').insertAfter('#fade_out');
	},
	extractPageNameFromUrl: function(url)
	{
		pageName = url.replace(this.rootUrl, "").replace(".html", "");
		return pageName != "" ? pageName : "index";
	},
	getContentUrl: function() { return this.rootUrl + this.currentPage + ".html"; },
	bindAjaxLinks: function()
	{
		$("a.ajax").click(function (e) {
			e.preventDefault();
			site.navigateToUrl($(this).attr("href"));
		});
	},
}



