function api(object, action, params, callback) {
    var url = '';
    url = "/api/" + object + "/" + action;
    $.getJSON(url, params, callback);
}

function uploadFileStart(file)
{
    $('#nastaveni-foto').html('<div id="foto-progress"><p></p></div>');
}
function uploadProgress(file, bytesloaded, bytestotal) {
	var progress = document.getElementById("yourprogressid");
	var percent = Math.ceil((bytesloaded / bytestotal) * 160);
	$('#foto-progress p').width(percent);	
}
function uploadComplete()
{
    var stamp = new Date();
    if($('#student_id').val())
    {
        $('#nastaveni-foto').html('<img src="/firma/foto/?student_id=' + $('#student_id').val() + '&ident=' + stamp + '" />');
    }
    else
    {
        $('#nastaveni-foto').html('<img src="/firma/foto/?ucitel_id=' + $('#ucitel_id').val() + '&ident=' + stamp + '" />');
    }
}

$(document).ready(function()
{
    $('#test-form').submit(function() {
        return confirm('Opravdu chcete Váš test odevzdat?');
    });
    $('.multipages').each(function(i, el) {
        $('input[type=submit][name=submit]').remove();
        _this = $(this);
        var celkem = $('.multipages').length;
        var div = $(document.createElement('div'));
        if(i != 0)
        {
            var input = $(document.createElement('input'));
            input.val('<< Zpět');
            input.attr({'type': 'submit', 'class' : 'submit'});
            input.css({'float': 'left'});
            div.append(input);
            input.click(function() 
            {
                $('.multipages').each(function(ii, ell) 
                {
                    if(ii == i - 1)
                    {
                        $(this).show();
                    }
                });
                input.parent().parent().hide();
                input.blur();
                scroll(0,0);
                return false;
            });
        }
        if(i != celkem - 1)
        {
            var input = $(document.createElement('input'));
            input.attr({'type': 'submit', 'float': 'right', 'class' : 'submit'});
            input.css({'float': 'right'});
            input.val('Další >>');
            div.append(input);
            input.click(function() 
            {
                $('.multipages').each(function(ii, ell) 
                {
                    if(ii == i + 1)
                    {
                        $(this).show();
                    }
                });
                input.parent().parent().hide();
                input.blur();
                scroll(0,0);
                return false;
            });
        }
        else
        {
            var input = $(document.createElement('input'));
            input.attr({'type': 'submit', 'float': 'right', 'class' : 'submit', 'name' : 'submit'});
            input.css({'float': 'right'});
            input.val('Odeslat');
            div.append(input);
        }
        $(this).append(div);
        if(i != 0)
        {
            $(this).hide();
        }
        
    });
    $('.foto-remove').click(function () {
        if(confirm('Opravdu chcete fotku vymazat ?'))
        {
            if($('#student_id').val())
            {
                var data = 'student_id=' + $('#student_id').val();
            }
            else if($('#ucitel_id').val())
            {
                var data = 'ucitel_id=' + $('#ucitel_id').val();
            }
            var link = $(this);
            link.html('<img src="/images/progress.gif" />');
            $.ajax({type: "GET", url: "/nastaveni/smazat_fotku/", data: data, complete: function(request)            
            {
                if(request.responseText == 'ok')
                {
                    $('#nastaveni-foto').html('<img src="/images/foto.gif" />');
                }
                else
                {
                    alert('Fotku se nepodařilo vymazat.');
                }
                link.html('Vymazat fotku');
            }});
        }
    }); 
    if($('.nastaveni-student').length != 0 || $('.nastaveni-ucitel').length != 0)
    {
        var url = '';
        if($('#student_id').val())
        {
            var url = '/nastaveni/fotka/?student_id=' + $('#student_id').val();
        }
        if($('#ucitel_id').val())
        {
            var url = '/nastaveni/fotka/?ucitel_id=' + $('#ucitel_id').val();
        }
        swfu = new SWFUpload({
            upload_script : url,
            target : "foto-loader-panel",
            flash_path : "/flash/swfupload.swf",
            allowed_filesize : 30720,	// 30 MB
            allowed_filetypes : "*.jpg",
            allowed_filetypes_description : "Fotky...",
            browse_link_innerhtml : "Nahrát fotku",
            upload_link_innerhtml : "Nahrejte fotku",
            browse_link_class : "",
            upload_link_class : "",
            flash_loaded_callback : 'swfu.flashLoaded',
            //~ upload_file_queued_callback : "fileQueued",
            upload_file_start_callback : 'uploadFileStart',
            upload_progress_callback : 'uploadProgress',
            upload_file_complete_callback : 'uploadComplete',
            upload_error_callback : 'function () {alert("xxxx");}',
            upload_cancel_callback : 'function () {alert("xxxx");}',
            auto_upload : true
        });
    }
    $('a.show-zprava').click(function() {
        var id = $(this).attr('id').split('_')[2];
        $('#zprava_' + id).toggle('slow');
    });
    $('a.player').flash(
        { src: '/flash/singlemp3player.swf', height: 200, width: 300 },
        { version: 7 },
        function(htmlOptions) {
            $this = $(this);
            htmlOptions.flashvars.file = $this.attr('href');
            $this.html('');
            $this.before($.fn.flash.transform(htmlOptions));						
        }
    );
    $('a.videoplayer').flash(
        { src: '/flash/simpleflvplayer.swf',  bgcolor: "FFFFFF" },
        { version: 7 },
        function(htmlOptions) {
            $this = $(this);
            htmlOptions.flashvars.skin = '/flash/SteelExternalAll.swf';
            htmlOptions.flashvars.url = $this.attr('href');
            htmlOptions.flashvars.AutoPlay = 0;
            
            $this.html('');
            $this.before($.fn.flash.transform(htmlOptions));						
        }
    );
    
    $('#instrukce-nova').click(function() {
        $('#test_instrukce').toggle('slow');
        if($(this).parent().attr('class') == 'hint')
        {
            $(this).parent().hide();
        }
    });
    bindOtazkaOdpoved();
    bindHighlightable();
    if($('form.priradit_form').length != 0 || $('form.poslat_zpravu_form').length != 0)
    {    
        $('#trida_id').change(function() {
            var selected = new Array();
            var not_continue = false;
            $(this).find('option[selected]').each(function() {
                if(!not_continue)
                {
                    selected.push($(this).val())
                }
                if($(this).val() == 0)
                {
                    not_continue = true;
                }
            });
            if(not_continue)
            {
                $(this).find('option[selected]').each(function() {
                    if($(this).val() == 0)
                    {
                        this.selected = 1;
                    }
                    else
                    {
                        this.selected = 0;
                    }
                });
            }
            if(selected.length == 0 || (selected.length == 1 && selected[0] == 0))
            {
                $('#testy_priradit_student_id').hide();
            }
            else
            {
                var params = '';
                for(var i = 0; i < selected.length; i++)
                {
                    params += '&trida_id[]=' + selected[i];
                }
                $.getJSON("/testy/studenti_trid/?" + params, function(json) 
                {   
                    var select = $('#testy_priradit_student_id').find('select').get(0);
                    select.options.length = 0;
                    $.each(json, function(student_id, jmeno) {
                        var option = new Option(jmeno, student_id);
                        if(student_id == 0)
                        {
                            option.selected = 1;
                        }
                        select.options[select.options.length] = option;
                    });
                    $('#testy_priradit_student_id').show();
                });
            }
        });
    }
    $('#table_checkall').change(function() {
        var table = $(this).parents('table');
        if($(this).is('[checked]'))
        {
            $('input.table_checkallable', table).attr('checked', 'checked');
        }
        else
        {
            $('input.table_checkallable', table).removeAttr('checked');
        }
    });
    if($('#testy_priradit').length != 0)
    {    
        $('#department_id').change(function() 
        {
            var selected = new Array();
            var not_continue = false;
            $('option[selected]', $(this)).each(function() 
            {
                if($(this).val() != '')
                {
                    selected.push($(this).val())
                }
            });
            var params = '';
            for(var i = 0; i < selected.length; i++)
            {
                params += '&oddeleni_id[]=' + selected[i];
            }
            $.getJSON("/testy/studenti_oddeleni/?" + params, function(json) 
            {   
                var select = $('#class_id').get(0);
                select.options.length = 0;
                $.each(json, function(trida_id, nazev) {
                    var option = new Option(nazev, trida_id);
                    if(trida_id == '')
                    {
                        option.selected = 1;
                    }
                    select.options[select.options.length] = option;
                });
                $('#class_id').show();
            });
        });
    }
    if($('form.priradit_form').length != 0)
    {
        $('#student_id').change(function() {
            var not_continue = false;
            $(this).find('option[selected]').each(function() {
                if($(this).val() == 0)
                {
                    not_continue = true;
                }
            });
            if(not_continue)
            {
                $(this).find('option[selected]').each(function() {
                    if($(this).val() == 0)
                    {
                        this.selected = 1;
                    }
                    else
                    {
                        this.selected = 0;
                    }
                });
            }
        });
    }
    
    if($('div.test-timer').length == 1)
    {
        $(window).scroll(function() {
            var el = $('div.test-timer');
            var offset = el.offset();
            var scrolltop = $(window).scrollTop();
            if(scrolltop > 300)
            {
                var x = offset.left;
                var width = el.width();
                $(el).css({'position': 'fixed', 'top': '30px', 'left': x + 'px', 'width': width + 'px'});
            }
            else
            {
                $(el).css({'position': 'static'});
            }
        });
        test_timer  = setTimeout("updateTestTimer()", 1000);
    }
    
    if($('#test_novy, #test_zmena, #test_banka_novy_kategorie').length != 0)
    {
        $('#test_kategorie_id').change(function() {
            if($(this).val() == 9999)
            {
                $('#el_test_kategorie_nazev').show();
            }
            else
            {
                $('#el_test_kategorie_nazev').hide();
            }
        });
    }
    if($('form.oddeleni_nove').length != 0)
    {
        $('#ucitel_id').change(function() {
            $(this).val() == 0 ? $('#ucitel').show() : $('#ucitel').hide();
        });
    }
    if($('form.tridy_nova').length != 0)
    {
        $('#ucitel_id').change(function() {
            $(this).val() == 0 ? $('#ucitel').show() : $('#ucitel').hide();
        });
    }
    if($('form.oddeleni_novy_vedouci').length != 0)
    {
        $('#ucitel_id').change(function() {
            $(this).val() == 0 ? $('#ucitel').show() : $('#ucitel').hide();
        });
    }
    if($('form.test_banka_novy').length != 0)
    {
        $('#banka_id').change(function() {
            if($(this).val() != '')
            {
                $.getJSON("/test/testy_banky/?banka_id=" + $(this).val(), function(json) 
                {
                    loadOptions(json, $('#test_id').get(0));
                    $('#el_test_id').show();
                });
            }
            else
            {
                $('#el_test_id').hide();
            }
        });
    }
    var marker = false;
    if($('#firma-zmena-gmap').length != 0)
    {
        gmap = new GMap2(document.getElementById("firma-zmena-gmap"));
        gmap.addControl(new GSmallMapControl());
        gmap.addControl(new GMapTypeControl());
        if($('input[name=lat]').val() != '' && $('input[name=lng]').val() != '')
        {
            var lat = $('input[name=lat]').val();
            var lng = $('input[name=lng]').val()
            var point = new GLatLng(lat, lng);
            gmap.setCenter(point, 13);
            var text = $('input[name=nazev]').val() + "<br />" + 
                       $('input[name=adresa]').val() + ' ' + $('input[name=mesto]').val() + ' ' + $('input[name=psc]').val() + '<br />Email: ' + 
                       $('input[name=email]').val() + '<br />WWW: <a href="' + $('input[name=www]').val() + '" target="_blank">' + 
                       $('input[name=www]').val() + '</a><br />Telefon: ' + 
                       $('input[name=telefon]').val() + '<br />';
            gmap.openInfoWindow(point, text);
        }
        else
        {
            gmap.setCenter(new GLatLng(49.191128, 16.617851), 14);
        }
        
        GEvent.addListener(gmap, "click", function(marker, point) 
        {
            $('input[name=lat]').val(point.lat());
            $('input[name=lng]').val(point.lng());
            gmap.openInfoWindow(point, document.createTextNode($('input[name=nazev]').val()));
        });
    }
    if($('#firma-zmena-gmap').length != 0)
    {
        gmap = new GMap2(document.getElementById("firma-zmena-gmap"));
        gmap.addControl(new GSmallMapControl());
        gmap.addControl(new GMapTypeControl());
        if($('input[name=lat]').val() != '' && $('input[name=lng]').val() != '')
        {
            var lat = $('#lat').val();
            var lng = $('#lng').val()
            var point = new GLatLng(lat, lng);
            gmap.setCenter(point, 13);
            var text = $('input[name=nazev]').val() + "<br />" + 
                       $('input[name=adresa]').val() + ' ' + $('input[name=mesto]').val() + ' ' + $('input[name=psc]').val() + '<br />Email: ' + 
                       $('input[name=email]').val() + '<br />WWW: <a href="' + $('input[name=www]').val() + '" target="_blank">' + 
                       $('input[name=www]').val() + '</a><br />Telefon: ' + 
                       $('input[name=telefon]').val() + '<br />';
            gmap.openInfoWindow(point, text);
        }
        else
        {
            gmap.setCenter(new GLatLng(49.191128, 16.617851), 14);
        }
        
        GEvent.addListener(gmap, "click", function(marker, point) 
        {
            $('#lat').val(point.lat());
            $('#lng').val(point.lng());
            var text = $('input[name=nazev]').val() + "<br />" + 
                       $('input[name=adresa]').val() + ' ' + $('input[name=mesto]').val() + ' ' + $('input[name=psc]').val() + '<br />Email: ' + 
                       $('input[name=email]').val() + '<br />WWW: <a href="' + $('input[name=www]').val() + '" target="_blank">' + 
                       $('input[name=www]').val() + '</a><br />Telefon: ' + 
                       $('input[name=telefon]').val() + '<br />';
            gmap.openInfoWindow(point, text);
        });
    }
    if($('#firma-gmap').length != 0)
    {
        firma_gmap = new GMap2(document.getElementById("firma-gmap"));
        firma_gmap.addControl(new GSmallMapControl());
        if($('input[name=_lat]').val() != '' && $('input[name=_lng]').val() != '')
        {
            var lat = $('input[name=_lat]').val();
            var lng = $('input[name=_lng]').val()
            var point = new GLatLng(lat, lng);
            firma_gmap.setCenter(point, 13);
            firma_gmap.addOverlay(new GMarker(point));
        }
        else
        {
            gmap.setCenter(new GLatLng(49.191128, 16.617851), 13);
        }
    }
    var reftimer = 0;
    if($('#ajaxrefresher').length != 0)
    {
        reftimer  = setTimeout('doRefresh();', 60000);
    }
});


function doRefresh()
{
    $.ajax({type: "GET", url: '/test/refresh/', data: {}, complete: function()
    {
    }});
    reftimer  = setTimeout('doRefresh();', 60000);
}

function loadOptions(data, element)
{
    element.options.length = 0;
    $.each(data, function(id, nazev) {
        var option = new Option(nazev, id);
        element.options[element.options.length] = option;
    });
}

function updateTestTimer()
{
    var element = $('div.test-timer span');
    if(element.html() == 'neomezeno')
    {
        return;
    }
    var tmp = element.html().split(':');
    var minut = tmp[0];
    var sekund = tmp[1];
    if(sekund == 0)
    {
        sekund = '59';
        minut--;
    }
    else
    {
        sekund -= 1;
    }
    if(minut == -1 && sekund == 59)
    {
        $('#test-form').unbind();
        $('#test-form #test-done').click();
        return true;
    }
    if(sekund >= 0 && sekund <= 9)
    {
        sekund = '0' + sekund;
    }
    element.html(minut + ':' + sekund);
    test_timer  = setTimeout("updateTestTimer()", 1000);
}

var test_timer = 0;

function bindHighlightable(table)
{
    $('table.highlightable tr').unbind();
    $('table.highlightable tr').mouseover(function() {
        $(this).addClass('highlighted');
    });
    $('table.highlightable tr').mouseout(function() {
        $(this).removeClass('highlighted');
    });
}

function bindOtazkaOdpoved()
{
    if($('form.test_otazka_nova').length != 0 || $('form.test_otazka_zmena').length != 0)
    {
        $('#hint-' + $('#typ').val()).fadeIn('slow');
        $('#test_otazka_kategorie_id').change(function() 
        {
            if($(this).val() == 0)
            {
                $('#el_test_otazka_kategorie_nazev').show();
            }
            else
            {
                $('#el_test_otazka_kategorie_nazev').hide();
            }
        });
        $('#typ').change(function() {
            if($(this).val() == 1 || $(this).val() == 10 || $(this).val() == 4 || $(this).val() == 5)
            {
                $('#el_test_otazka_text').show();
            }
            else
            {
                $('#el_test_otazka_text').hide();
            }
            if($(this).val() == 4 || $(this).val() == 5)
            {
                $('#el_test_otazka_odpoved').hide();
            }
            else
            {
                $('#el_test_otazka_odpoved').show();
            }
            $('.hint').hide();
            $('#hint-' + $(this).val()).fadeIn('slow');
        });
    }
}
