/*
Obsluha tabulky s udalostmi.
*/
$(document).ready(function(){
    // hover nad radkem
    $('table.events').find('tr:not(.quiet)').hover(function(){
        prev_row = $(this).prev();
        if (!prev_row.hasClass('quiet')) {
            prev_row.css({background:'#EEE'});
        }
        next_row = $(this).css({background:'#EEE'}).next();
        if (!next_row.hasClass('quiet')) {
            next_row.css({background:'#EEE'});
            next_row = next_row.next();
        }
        next_row.css({background:'#EEE'}).show();
        $(this).parents('table.events').find('tr:last').hide();
    }, function() {
        prev_row = $(this).prev();
        if (!prev_row.hasClass('quiet')) {
            prev_row.css({background:'#FFF'});
        }
        next_row = $(this).css({background:'#FFF'}).next();
        if (!next_row.hasClass('quiet')) {
            next_row.css({background:'#FFF'});
            next_row = next_row.next();
        }
        next_row.css({background:'#FFF'}).hide();
        $(this).parents('table.events').find('tr:last').show();
    });

    // click nad kteroukoliv bunkou v tabulce zpusobi skok na detail 
    // (je to pohodlnejsi nez se strefovat na titulek)
    // zajimaji nas ale jen ty radky, ktere obsahuji odkaz na detail udalosti
    $('table.events').find('tr:not(.quiet)').find('td:first').each(function(){
        $a = $(this).find('a');
        if ($a.length == 0) {
            $prev_row = $(this).parents('tr:first').prev();
            if ($prev_row.length && !$prev_row.hasClass('quiet')) {
                $a = $prev_row.find('a:first');
            }
        }
        if ($a.length) {
            $(this).parents('tr:first').find('td').css({cursor:'pointer'}).bind('click', {a: $a}, function(event){
                url = event.data.a.attr('href');
                if (url.length > 0) {
                    window.location = url;
                }
                return false;
            });
        }
    });

    // najdeme nejvyssi radek
    total_max = 0;
    $('table.events').find('tr').each(function(){
        h = $(this).height();
        if (h > total_max) {
            total_max = h;
        }
    });
    title_max = 0;
    $('table.events').find('tr:not(.quiet)').each(function(){
        h = $(this).height();
        if (h > title_max) {
            title_max = h;
        }
    });

    // pridame prazdny radek na konec tabulky
    count = Array();
    $('table.events').each(function(){
        count.push($(this).find('tr:first').find('td').length);
    });
    count.reverse();
    $('table.events').each(function(){
        $(this).find('tr:last').after('<tr><td colspan="' + count.pop() + '"> </td></tr>');
    });

    // nastavime explicitne vsechny radky na idealni vysku
    $('table.events').find('tr:not(.quiet)').find('td:first').each(function(){$(this).height(title_max);});
    $('table.events').find('tr.quiet').find('td:first').each(function(){$(this).height(total_max);});
    $('table.events').find('tr:last').find('td:first').each(function(){$(this).height(total_max);});

    // schovame radky s popisem
    $('table.events').find('tr.quiet').hide();
});

// vim: set et si ts=4 sw=4 enc=utf-8 syn=javascript:
