$(document).ready(function() {

    // make title (first td tag) into h1
    var h1 = $('td:first');
    $('table').before("<h1>"+h1.html()+"</h1>")
    h1.remove();
    
    // remove obsolete items
    $('tr:first').remove();
    $('colgroup').remove(); 
    
    // move first row of tbody into thead
    $('table tbody tr:first').parent().before("<thead><tr>"+$('table tr:first').html()+"</tr></thead>");
    $('table tbody tr:first').remove();
    

    // convert first TD in header row to TH
    $('thead td:has(b):first').each(function () {
        $(this).before("<th></th>").remove();
    });

    // convert first TD in each tbody row to TH
    $('tbody td:has(b)').each(function () {
        $(this).before("<th class='period'>"+$(this).html()+"</th>").remove();
    });


    // add % to header row
    $('table thead td').each(function () {
        $(this).html($(this).html()+'%');
    });

    // add css classes
    $('table thead tr:first').addClass('header');
    $('table tbody tr:odd').addClass('odd');
    $('table tbody tr:even').addClass('even');

    // calculate equal column widths
    var maxWidth=0;
    $('table tbody tr:first td').each(function () {
        maxWidth = $(this).width() > maxWidth ? $(this).width() : maxWidth;
    });

    // set width of table
    $('table').attr('width', (maxWidth+10) * $('table tbody tr:first td').size());
    
    // insert style for td with calculated width
    $('link:last').after('<style>td {min-width:'+(maxWidth)+'px;width:'+(maxWidth)+'px;}</style>');
    
    // insert header row every 20 rows
    $('table tbody tr:nth-child(20n)').after($('table thead tr:first'));

    // remove header row if it is the last row in table
    if ($('table tbody tr:last').attr('class') == 'header') {
        $('table tbody tr:last').remove();
    }

});
