{% load i18n %}
{% include 'snippets/list_commit_detail.html' %}

function reqEvents(start) {
    $('#events-loading').removeClass('hide');
    $.ajax({
        url:'{{SITE_ROOT}}events/?start=' + start{% if org %} + '&org_id={{ org.org_id }}'{% endif %},
        dataType: 'json',
        cache: false,
        success: function(data) {
            // for data fetched by clicking 'more'
            var original_last_date = $('.event-group-hd:last'),
                original_last_events,
                new_first_date, new_first_events;

            $('#events-loading').addClass('hide');
            $('#events-body').append(data['html']);

            new_first_date = original_last_date.next().next();
            if (original_last_date.text() == new_first_date.text()) {
                original_last_events = original_last_date.next();
                new_first_events = new_first_date.next();
                original_last_events.append(new_first_events.html());
                new_first_date.remove();
                new_first_events.remove();
            }

            $('.updated-repo').each(function() {
                $(this).next().css({'margin-right':$(this).width() + 10});
            });

            if (data['events_more']) {
                var new_start = data['new_start'];
                $('#events-more').data('start', new_start).removeClass('hide');
            } else {
                $('#events-more').addClass('hide');
            }

            if ($.browser.mozilla || $.browser.msie) {
                $('#events a').focus(function() {
                    $(this).blur();
                });
            }
            $('.lsch').click(function() {
                listCommitDetails($(this).data('url'), $(this).data('time'));
                return false;
            });
            $('.lsch-encrypted').click(function() {
                showDetailForEncrypRepo($(this));
                return false;
            });
        },
        error: function(jqXHR, textStatus, errorThrown) {
            $('#events-loading').addClass('hide');
            if (!jqXHR.responseText && textStatus != 'abort') {
                $('#events-error').html("{% trans "Failed. Please check the network." %}").removeClass('hide');  
            }   
        }
    });
}
//reqEvents(0);

function showDetailForEncrypRepo(obj) {
    if (obj.data('passwordset')) {
        listCommitDetails(obj.data('url'), obj.data('time'));
    } else {
        var form = $('#repo-decrypt-form');
        form.find('input[name="repo_id"]').val(obj.data('repoid'));
        form.find('.op-target').html(obj.data('reponame'));
        form.data({'url':obj.data('url'), 'time':obj.data('time')}).modal({appendTo:'#main'});
        $('#simplemodal-container').css({'width':'auto'});
        $(window).resize();
    }
}

$('#events-more').click(function() {
    $(this).addClass('hide');
    reqEvents($(this).data('start'));
});

{% include 'snippets/repo_decrypt_js.html' %}
