var currentMenu = "";
var showSubMenus = true;

function buildMenusFromSiteMap() {
    $("#menuBar").append("<a id='HomeMenu' class='menuItem' title='There's no place like home' href='/index.html'>Home</a><br />");

    var sitemap = $.get("/sitemap.xml", null, function (data, textStatus, XMLHttpRequest) {
        var xml = $(data);
        var uris = [];
        xml.find("loc:contains('/index.html')").each(function (index) {
            uris.push(parseUri(this.textContent || this.text));
        });
        // Google's sitemap creator returns the subdirs before the parent dirs - I want them in "natural" dir order.
        uris.sort(function(a, b) {return a.directory.localeCompare(b.directory);});
        $.each(uris, function(index, uri) {
            var dir = uri.directory;
            var dirParts = dir.slice(1, -1).split("/");
            var menuId = dirParts[0] + "Menu";
            var menuCollectionId = menuId + "Collection";
            var subMenuId = dirParts.length > 1 && dirParts[1].length > 0 ? dirParts[1] + "Menu" : "";

            if (dirParts.length === 1 && dirParts[0].length > 0 && $("#" + menuId).length === 0) {
                $("#menuBar").append("<a id='" + menuId + "' class='menuItem' title='" + dirParts[0] + "' href='#'>" + dirParts[0] + " Home</a><br />" +
                                     "<div id='" + menuCollectionId + "' class='menuCollection hidden'/>");
            }
            else if ($("#" + menuCollectionId).length > 0 && $("#" + subMenuId).length === 0) {
                $("#" + menuCollectionId).append("<a id='" + subMenuId + "' class='menuItem subMenuItem' href='" + uri.relative + "'>" + dirParts[1] + "</a>");
            }
        });
    }, "xml");
}

function menuClicked() {
    var menuCollection = "#" + this.id + "Collection";
    $(".menuCollection").addClass("hidden");

    if (currentMenu != menuCollection) {
        toggleSubMenu(menuCollection);
        currentMenu = menuCollection;
    }
    
    return false;
}

// parseUri 1.2.2
// (c) Steven Levithan <stevenlevithan.com>
// MIT License

function parseUri(str) {
	var	o   = parseUri.options,
		m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
		uri = {},
		i   = 14;

	while (i--) uri[o.key[i]] = m[i] || "";

	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
		if ($1) uri[o.q.name][$1] = $2;
	});

	return uri;
};

parseUri.options = {
	strictMode: false,
	key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
	q:   {
		name:   "queryKey",
		parser: /(?:^|&)([^&=]*)=?([^&]*)/g
	},
	parser: {
		strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
		loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};

function toggleSubMenu(menuCollection) {
    $(menuCollection).toggleClass("hidden");
    showSubMenus = showSubMenus ? false : true;
}

$(document).ready(function () {   
    $(".footer").toggleClass("hidden");

    if ($(".footer").offset().top < 880) {
        $(".footer").css("position", "absolute");
    }

    $(".menuItem").click(menuClicked);

    //buildMenusFromSiteMap();
});


