function getNoteId(url) {
    var regex = new RegExp("/note/(\\d+)/");
    var m = url.match(regex);
    var noteid = m[1];
    return noteid;
}

$(document).ready(function () {
    $("#menu a").each(function () {
        //var addr = $(this).attr("href");
        //var regex = new RegExp("/note/(\\d+)/");
        //var m = addr.match(regex);
        //var noteid = m[1];
        //$(this).attr("href", "/note/1/#" + noteid); 

        $(this).click(function (event) {
            // .url
            var addr = $(this).attr("href");
            var noteid = getNoteId(addr);
            //var noteid = $.url(addr).attr('fragment')
            $.getJSON("/content/" + noteid + "/", function(msg) {
                var content = $("#content");
                window.history.pushState({"html": msg.content, "pageTitle":msg.title}, "Fastwiki.net - " + msg.title, "/note/"+noteid);
                //window.location.hash = noteid;
                //var urlPath = "/note/" + noteid + "/";
                //window.history.pushState({"html":a.html(),"pageTitle":response.pageTitle},"", urlPath);
                content.html(msg.content);
                $("#title").html(msg.title);

            });
            event.preventDefault();
            return false;
        });
    });
    window.onpopstate = function(e) {
        console.log(e);
        if (e.state) {
            $("#content").html(e.state.html);
            $("#title").html(e.state.pageTitle);
        }
    }
});

