function $(id) {return document.getElementById(id);}

(function() {

	var animateInterval = 5;
	var hashPrefix = "#";
	
	var currentPage;
	var body;
	var currentHash;
	var pageHistory = [];
	var pages = {};
	
	addEventListener("load", function(event) {
	    body = document.getElementsByTagName("body")[0];
		currentHash = location.hash;
	    // load from hash if given and valid
	    if (currentHash && currentHash.length>hashPrefix.length) {
			var page = $(currentHash.substr(hashPrefix.length));
			if (page) currentPage = page}
	    // else load home
	    if (!currentPage) {
			currentPage = $('home');
			location.href = currentHash = hashPrefix + currentPage.id;
			pageHistory.push(currentPage.id);}
		currentPage.style.display = '';
		if (!pages[currentPage.id]) pages[currentPage.id] = currentPage;
	    setTimeout(function(){window.scrollTo(0, 1)}, 500); // pan to the bottom, hides the location bar
	    setInterval(checkLocation, 300);
	}, false);
	
	addEventListener("click", function(event) {
	    var obj = event.target;
		if (obj && obj.hash) {
			if (obj.hash.length>hashPrefix.length) { // go to page
			    var id = obj.hash.substr(hashPrefix.length);
				var back = (obj.className=='breadcrumb' || obj.className=='back');
			    showPage(id,back);}
			else if (obj.hash==hashPrefix) { // go to top of page
				scrollTo(0,1);
			}
			event.preventDefault();
		}
	}, true);
	
	function checkLocation() {
	    if (location.hash != currentHash) {
			currentHash = location.hash;
			var id = currentHash.substr(hashPrefix.length);
			var page = $(id);
			if (page) {
			    var index = pageHistory.indexOf(id);
			    var backwards = index>=0;
			    if (backwards) pageHistory.splice(index, pageHistory.length);
			    showPage(id, backwards)}}
	}
	
	function showPage(pageId, backwards) {
		var page = pages[pageId];
		if (page) body.appendChild(page);
		else page = $(pageId);
	    if (!page) return;
	    location.href = currentHash = hashPrefix + page.id;
	    pageHistory.push(page.id);
	    var fromPage = currentPage;
	    currentPage = page;
	    if (fromPage) setTimeout(swipePage, 0, fromPage, page, backwards);
	}
	
	function swipePage(fromPage, toPage, backwards) {
		
		var w = fromPage.offsetWidth;
		var x = (backwards) ? w : 0;
		var xStep = parseInt(w/16,10);
		
	    toPage.style.left = ((backwards) ? 0 : w) + 'px';
		toPage.style.display = '';
		fromPage.style.left = x + 'px';
		
		var timer = setInterval(function() {
			x += (backwards) ? -xStep : xStep;
			if ((backwards) ? x<=0 : x>=w) { // has finished
				x =  (backwards) ? 0 : w;
				clearInterval(timer);
				fromPage.style.display = 'none';
				if (!backwards) {
		            var page2 = toPage.cloneNode(true);
		            page2.style.left = "0";
		            body.appendChild(page2);
		            setTimeout(function() {
		                scrollTo(0, 1);
		                toPage.style.left = "0";
		                document.body.removeChild(page2);
		            }, 10);
		        }
			}
			scrollTo(x,1);
	    }, animateInterval);
	}

})();