// New, awesome code!  :D
var page = {
	current: false,
	next: false,
	content_open: false
}

var debugging = false;

function debug(msg) {
	if (debugging) {
		console.log(msg);
	}
}

function showContent(content) {
	// Set static constant
	page.content_open = true;
	// Re-populate inner content element
	$("#inner-content").html(content);
	// Slide down content pane
	$("#content").slideDown("slow");
}

function imagesLoaded() {
	$("#loader").fadeOut("slow", function(){
		$("#wrapper").fadeIn("slow");
		$("#preload").css("display","none");
	})
}

$(document).ready(function(){
	// Add jQuery Address functionality to the links in the navbar
	$("#nav a").address();
	// Our event responder that triggers whenever the address is changed (including on first load!)
	$.address.change(function(event) {
		debug("address change called");
		// Set shortcut to URI value
		var uri = event.value;
		// Don't run ajax call for index page
		if (uri === "/") {
			if (page.content_open) {
				document.title = "MiracleBlue - Home";
				$("#nav a.navlink_selected").attr("class","navlink");
				$("#nav a.navlink_selected").blur();
				$("#content").slideUp("slow");
			}
			return;
		}
		// Run ajax call to get JSON return data
		$.getJSON(uri, function(data){
			// Set document title
			document.title = data.title;
			// Toggle navigation bar display
			$("#nav a.navlink_selected").attr("class","navlink");
			$("#nav a[href='"+uri+"']").attr("class","navlink_selected");
			// Slide up content pane if it's already open
			if (page.content_open) {
				$("#content").slideUp("slow", function(){
					// Slide down new content
					showContent(data.content);
				});
			}
			// Slide down new content
			else {
				showContent(data.content);
			}
		});
	});
	// Initialise qTip logic
	$("a[tip]").qtip({
		content: {
			attr: "tip"
		},
		position: {
			my: "bottom center",
			at: "top center",
			adjust: { x: 0, y: -10 }
		},
		show: {
			delay: 100
		},
		style: {
			classes: 'ui-tooltip-rounded ui-tooltip-shadow'
		}
	});
})
