$(document).ready(function() {

	// Array of possible pages
	// First is initial page
	var pages = ["home", "about"];
	
	function setPage(sender) {
		var validPage = validatePageName(sender);
		
    	var btnID = validPage + "-button";
		var pageID = validPage + "-page";
		
		if ($(btnID).hasClass("active")) {
			// Do nothing
		} else {
			$(".active").removeClass("active");
			$(btnID).addClass("active");
			$('#viewer').empty().append($('<div></div>').html($(pageID).html())).jScrollPane();
			window.location.hash = validPage;
		}
	};
    
	function validatePageName(sentPage) {
    	// Loop through pages array checking against sentPage
		for (var i=0; i<pages.length; i++) {
			if (sentPage=="#" + pages[i]) {
				// Set currentPage
				var sentPage = "#" + pages[i];
				return sentPage;
			}
		}
		
		// If hash does not equal an array value set to initial page
		var sentPage = "#" + pages[0];
		return sentPage;

	};
	
	function pollHash() {
		recentHash = "";
     	if (window.location.hash==recentHash) {
     		return;
     	} else {
     		setPage(window.location.hash);
     	}
     	recentHash = window.location.hash;
  	};
  		
  	function initializePage() {	
  		// Initialize page with hash value
		setPage(window.location.hash);
	};
	
	initializePage();
	setInterval(pollHash, 500);
	
	// Any a with class new-content will set page
    $("a.new-content").click(function () {
    	setPage($(this).attr("href"));
    });
});