{% load i18n %}
var g_token;
var g_loading = false;
// "setToken" is called in mobile app to pass in the auth token
function setToken(token) {
    g_token = token;
}
function reqEvents(start) {
    var loading_icon = $('#loading-icon');
    if (g_loading) return;
    g_loading = true;
    $.ajax({
        url:'{% url 'more_events' %}?start=' + start,
        dataType: 'json',
        cache: false,
        headers:{Authorization:'Token '+g_token},
        success: function(data) {
            $('#events-body').append(data['html']);

            setCmtWidth();
            $('.event-item').unbind().click(itemClick);

            if (data['events_more']) {
                $('#events').data('more', true).data('start', data['new_start']);
            } else {
                $('#events').data('more', false);
                loading_icon.hide();
            }
            g_loading = false;
        },
        error: function(jqXHR, textStatus, errorThrown) {
            loading_icon.hide();
            if (!jqXHR.responseText) {
                $('#events-error').html("{% trans "Failed. Please check the network." %}").removeClass('hide');
            }
            g_loading = false;
        }
    });
}

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

function itemClick() {
    var item = $(this);
    var url = item.data('url');

    // 'delete repo'
    if (!url) {
        return;
    }

    // modified more than 1 file
    if ($('.cmt-desc', item).data('more')) {

        // if 'event-details' already exist
        if ($('.event-details', item).length > 0) {
            $('.event-details', item).toggleClass('hide');
            return;
        }

        $.ajax({
            url: url,
            dataType: 'json',
            cache: false,
            headers:{Authorization:'Token '+g_token},
            success: function(data) {
                $('.txt', item).append(data['html']);
                $('.event-details li').click(function(e) {
                    if ($('a', $(this)).length > 0) {
                        location.href = $('a', $(this)).attr('href');
                    }
                    e.stopPropagation();
                });
            },
            error: function(jqXHR, textStatus, errorThrown) {
                if (jqXHR.responseText) {
                    $('.txt', item).append('<p class="error">' + $.parseJSON(jqXHR.responseText).err + '</p>');
                } else {
                    $('.txt', item).append('<p class="error">' + "{% trans "Please check the network." %}" + '</p>');
                }
            }
        });
    } else {
        location.href = url;
    }
}

setCmtWidth();
$('.event-item').click(itemClick);

{% if events_more %}
    $('#events').data('more', true).data('start', {{events_more_offset}});
{% else %}
    $('#events').data('more', false);
{% endif %}
// get more events when scroll to the bottom
$(document).scroll(function() {

    if ($('#events').data('more') && $(window).height() + $(window).scrollTop() == $(document).height()) {
        reqEvents($('#events').data('start'));
    }
});
