$(function() {
    var update_elements = $('.hook-update');
    $.fanart.update = function() {};
    if(update_elements.length) {
        var timeout_set = false;
        var request_running = false;
        var the_timeout = 0;
        var update_func;
        var update_numbers = {};
        update_elements.each(function(i) {
            update_numbers[i] = 0;
        });
        update_func = function() {
            var data = {};
            var data_len_total = 0;
            update_elements.each(function(i) {
                var a = $(this).find('a.update-params');
                if(a.length === 1) {
                    data['up_'+i] = a.attr('href');
                    data_len_total += a.length;
                }
            });
            //console.log('update', data);
            timeout_set = false;
            request_running = true;
            $.fanart.ajax({
                spintype: 'global',
                data: data,
                url: '/actions/update.php',
                error: function() {
                    //console.log("Něco je špatně!");
                },
                success: function(data, textStatus) {
                    //console.log(data);
                    var updates = $(data);
                    update_elements.each(function(i) {
                        var update = updates.find('.update.no-'+i);
                        if(update.length) {
                            var content = update.find('.content>*');
                             // weird - if I do content.hide().insertAfter(..., the element order gets mixed up
                            var ins = $(this);
                            content.each(function(){
                                $(this).hide().insertAfter(ins).slideDown(1000);
                                ins = $(this);
                            });
                            var next = update.find('a.update-params').attr('href');
                            var a = $(this).find('a.update-params');
                            a.attr('href', next);
                            update_numbers[i] = update_numbers[i] + content.length;
                            //console.log(update_numbers[i]);
                            if(update_numbers[i] > 100) {
                                a.remove();
                                $(this).after($('<div class="last-update">Obnov si prosím stránku, Porygonovi se udělalo špatně.</div>'));
                            }
                        }
                    });
                },
                complete: function() {
                    request_running = false;
                    if(data_len_total) {
                        $.fanart.update.schedule();
                    }
                }
            });
        };
        $.fanart.update = function() {
            if(timeout_set) {
                window.clearTimeout(the_timeout);
                timeout_set = false;
            }
            update_func();
        };
        $.fanart.update.schedule = function(timeout) {
            if(typeof timeout === 'undefined') { timeout = 30000; }
            //console.log('schedule', timeout/1000.);
            if(timeout_set) { return; }
            if(request_running) { return; }
            the_timeout = window.setTimeout(update_func, timeout);
            timeout_set = true;
            //console.log('to', the_timeout);
        };
        $.fanart.update.schedule(40000);
    }
});
