// functions.js

$(window).load(function() {
    try {
        var $hMenuLeft  = $('#menu-left').height();
        var $hMain      = $('#main').height();

        if ($hMenuLeft < $hMain) {
            // Container size
            $('#menu-left').height($('#main').height());

            var $resizer = $('#box-login');
            $resizer.height($resizer.height() + ($hMain - $hMenuLeft));
        } else {
            // Container size
            $('#main').height($hMenuLeft);

            var $resizer = $('#main .box:last');
            $resizer.height($resizer.height() + ($hMenuLeft - $hMain));
        }
    } catch (e) {}
});

$(document).ready(function() {
    // Progress bar
    if (jQuery().progressBar) {
        $('.progress').progressBar({
            boxImage : 'images/progress/progressbar.gif',
            barImage : {
                0  : 'images/progress/progressbg_red.gif',
                30 : 'images/progress/progressbg_orange.gif',
                70 : 'images/progress/progressbg_green.gif'
            }
        });
    }
    // Colorbox
    if (jQuery().colorbox) {
        $('a.colorbox').colorbox({
            maxWidth: '100%',
            maxHeight: '100%',
            transition: 'none',
            slideshowStart: 'spustit slideshow',
            slideshowStop: 'zastavit slideshow',
            current: '{current} z {total}',
            previous: 'předchozí',
            next: 'další',
            close: 'zavřít',
            href: $(this).attr('href'),
            rel: $(this).attr('rel')
        });
        $('#cboxPhoto').live('click', function() {
            $.fn.colorbox.close();
        });
    }
});

/* Odeslani formulare klavesou enter */
function formSubmitOnEnter(event, form)
{
    var keynum = 0;

    if (window.event)
        keynum = event.keyCode;
    else
        keynum = event.which;
    if (keynum == 13)
        form.submit();
    else
        return true;
}

// Vyplneni aktualniho data a casu pokud je vstupni pole prazdne
function fillDate(id)
{
    var $elem = $('#' + id);
    if ($elem != null && $elem.val() == '') {
        var now = new Date();
        $elem.val(
            now.getDate() + '. ' +
           (now.getMonth() + 1) + '. ' +
            now.getFullYear() + ' ' +
            now.getHours() + ':' +
            now.getMinutes());
    }
    return true;
}

// Hlasovani
function vote(iteration, pid, id)
{
    var heslo = $('#votepass-' + iteration).val();
    if (heslo === '') {
        alert('Pro hlasování zadejte své hlasovací heslo');
        return false;
    }
    heslo = $.encoding.digests.hexSha1Str(heslo);

    $('#votetip-' + iteration).html('Probíhá zaznamenávání Vašeho hlasování...');
    $('#votetip-' + iteration).show();
    $.ajax({
        dataType: 'json',
        url: 'vote.php',
        data: { 'pid' : pid, 'id' : id, 'heslo' : heslo },
        success: function(response) {
            if (response.error) {
                alert(response.error);
            } else if (response.ok) {
                alert('Hlasování proběhlo úspěšně, děkujeme.');
                $('#votepass-' + iteration).val('');
                $('#vote-' + iteration).hide();
            } else {
                alert('Neznámá chyba.');
            }
            $('#votetip-' + iteration).hide();
        },
        error: function() {
            alert('Nastala chyba během odesílání požadavku na server, ' +
                  'zkuste to prosím později.');
            $('#votetip-' + iteration).hide();
        }
    });
    return false;
}

function voteToggle(iteration)
{
    $('#vote-' + iteration).toggle();
}

function voteSubmitOnEnter(event, iteration, id, pid)
{
    var keynum = 0;

    if (window.event)
        keynum = event.keyCode;
    else
        keynum = event.which;
    if (keynum == 13)
        return vote(iteration, id, pid);
    else
        return true;
}

