//(function($) {

var pageTitle = "Spicy Bites Catering",
	hashTrigger = "#/",
	hashPollID = null,
	hashPollInterval = 500,
	lastHash = "",
	menuon = true;

function fixIEAA() {
	if ($.browser.msie)
		this.style.removeAttribute('filter');
}

function getHash() {
	return unescape(document.location.hash.substring(hashTrigger.length));
}

function showMenu(fn) {
	if (menuon) {
		if (typeof fn === "function")
			fn();
		return;
	}
	$("#content").stop(true).fadeTo(400, 0.0, function() {
		$(this).hide();
		$("#menu-toggler").stop(true).fadeTo(800, 0.0);
		$("#menu").stop(true).animate({width: $(window).width() - 230}, 400, "easeInOutQuart", function() {
			$(this).css("width", "100%");
			$("#main-nav").fadeTo(400, 1.0, fixIEAA);
			$("#lang-change").slideDown(400);
			$("#menu-toggler a").unbind("click").click(function(e) {
				e.preventDefault();
				hideMenu();
			});
			if (typeof fn === "function")
				fn();
			menuon = true;
		});
	});
}

function hideMenu(fn) {
	$("#main-nav").stop(true).fadeTo(200, 0.0, function() {
		$("#main-nav").hide();
		$("#lang-change").slideUp(200, function() {
			$("#menu").stop(true).animate({width: "165px"}, 1000, "easeInOutQuart", function() {
				$("#menu-toggler")
					.css("top", -50)
					.fadeTo(0, 1.0)
					.delay(800)
					.animate({top: 75}, 400, "easeInOutQuad", fixIEAA);
				$("#menu-toggler a").unbind("click").click(function(e) {
					e.preventDefault();
					showMenu();
				});
				if (typeof fn === "function")
					fn();
				menuon = false;
			});
		});
	});
}

function showHome() {
	if (lastHash == "")
		return;
	window.location.hash = hashTrigger.slice(1);
	lastHash = "";
	showMenu(function() {
		$("body").attr("id", "home");
		$("body").removeClass();
		document.title = pageTitle;
		$("#content").html("");
	});
}

function showPage(href) {
	if ((href == lastHash) && (href !== "contact/")) {
		hideMenu();
		return;
	}
	window.location.hash = hashTrigger + href;
	lastHash = href;
	hideMenu(function() {
		$("#content").load(href, function() {
			// page is set in the loaded page
			// as a global var
			$("body").attr("id", page.id);
			$("body").removeClass();
			$("body").addClass(page.bodyClass);
			document.title = page.title + " | " + pageTitle;
			$("#content").stop(true).delay(200).fadeTo(800, 1.0, fixIEAA);
			
			pageTracker._trackPageview(href);
		});
	});
}

jQuery(document).ready(function($) {
	$("#menu-toggler").fadeTo(0, 0.0);
	
	// attach handlers to main nav links
	$("#logo a").click(function(e) {
		e.preventDefault();
		showHome();
	});
	$("#main-nav a").click(function(e) {
		e.preventDefault();
		showPage($(this).attr("href")); // strip first slash
	});
	
	// setup a poller to support back/forward in history
	hashPollID = setInterval(function() {
		var hash = getHash();
		if (hash != lastHash && hash == "") // special case for homepage
			showHome();
		if (hash != lastHash)
			showPage(hash);
	}, hashPollInterval);
});

//})(jQuery);

