function tabme_show(event) {
    event.preventDefault();
    $(".tabme div.rounded-tab a").each(function() {
        // Hide all divs
        $(this.hash).hide();
        // Remove flags
        $(this).parent().removeClass("selected");
        $(this).parent().removeClass("onleft");
    });
    var tab = $(this).parent()
    // Show selected div
    $(this.hash).show('fast');
    // Add flag
    tab.addClass("selected");
    find_and_mark_left_selected()
}

function find_and_mark_left_selected() {
    /* TODO rewrite  (awful) */
    var last = null;
    $(".tabme div.rounded-tab").each(function() {
        if((last != null) && $(this).hasClass("selected")) {
            last.addClass("onleft");
        }
        last = $(this);
    });
}

function tabme() {
    // Find a tab menu and hook it
    var tabs = $(".tabme div.rounded-tab");
    if(tabs.length) {
      tabs.eq(0).addClass("first-tab");
      tabs.eq(tabs.length-1).addClass("last");
    }

    var tabsa = $(".tabme div.rounded-tab a");
    if (tabsa.length) {
        // Hide all divs at start
        tabsa.each(function() { $(this.hash).hide(); });
        // But show a default one, the one in the URL first
        var hash = window.location.hash ? window.location.hash : tabsa.eq(0).attr("hash");
        $(hash).show();
        // Select the matching tab
        var link = $("a[hash=" + hash + "]");
        var tab = link.parent()
        tab.addClass("selected")
        find_and_mark_left_selected()
        // Hook the onclick event
        tabsa.click(tabme_show);
    }
}

function gen_notes() {
    var notes = $("td span.note-value")
    if (notes.length) {
        notes.each(function() {
            // Append the table used to draw the bar
            $(this).after('\
                <table class=\"note-bar\">\
                  <tr>                    \
                    <td></td>             \
                    <td></td>             \
                    <td></td>             \
                    <td></td>             \
                    <td></td>             \
                    <td></td>             \
                    <td></td>             \
                    <td></td>             \
                    <td></td>             \
                    <td></td>             \
                  </tr>                   \
                </table>');
            // The table
            var note_bar = $(this).next("table");
            // The note value
            var note = $(this).html() - 1;
            // The nth cell of the table (n == note)
            var cursor = note_bar.find("td")[note];
            // We select this cell
            $(cursor).addClass("selected");
            // We remove the value
            $(this).remove();
        })
    }
}
