
$(function() {
    var trs = $(".year_overview tbody tr:not(.year_sum)");
    trs.css('cursor', 'pointer');

    trs.each(function() {
        var tr = $(this);
        var a = tr.find("a");
        tr.click(function() {window.location.replace(a.attr('href'));});
    });

    var tables = $(".year_overview table");
    tables.each(function() {
        var table = $(this);
        var caption = table.find('caption');
        var overview = table.find('tbody .year_sum');

        caption.css('cursor', 'pointer');
        overview.css('cursor', 'pointer');
        caption.click(function() {tableToggle(table);});
        overview.click(function() {tableToggle(table);});

        caption.append(' <span>(click to show months)</span>');
        tableToggle(table);
    });
});

function tableToggle(table) {
    var caption = table.find('caption');
    var rows = table.find('tbody tr:not(.year_sum)');
    var overview = table.find('tbody .year_sum');
    var first_td = table.find('td:first-child, th:first-child');

    caption.find('span').remove();

    if(rows.css('display') == 'none') {
        first_td.show();
        rows.show();
        caption.append('<span>(click to hide months)</span>');
    } else {
        first_td.hide();
        rows.hide();
        caption.append('<span>(click for details)</span>');
    }
}

