{% load seahub_tags avatar_tags i18n %}
    //repo-share-form share-list autocomplete
    var share_list = [], contact_email, group_name, group_name_py;
    {% for contact in contacts %}
    contact_email = '{{ contact.contact_email }}';    
    share_list.push({value:contact_email, label:contact_email});
    {% endfor %}
    
    {% for group in groups %}
    group_name = '{{ group.group_name }}';
    group_name_py = '{{ group.group_name|char2pinyin }}'
    share_list.push({value:group_name + group_name_py, label:group_name});
    {% endfor %}

    $(".repo-share-btn").click(function() {
        var form = $("#repo-share-form"),
            hd = $('.hd', form),
            btn_ct = $(this).parents('td'),
            repo_id = btn_ct.data('id'),
            repo_name = btn_ct.attr('data-name');
        form.modal({appendTo: "#main", focus:false, containerCss:{"padding": 0}});
        $('[name="repo_id"]', form).val(repo_id);
        hd.html(hd.html().replace('%(lib_name)s', '<span class="op-target">' + repo_name + '</span>'));
        $('#repo-share-tabs').tabs();
        $('#simplemodal-container').css('height', 'auto');
        addAutocomplete('#email_or_group', '#repo-share-form', share_list);
    });

    //check before post
    $('#share-submit-btn').click(function() {
        var form = $("#repo-share-form"),
            cur_tab_id = $('.ui-tabs-selected a', form).attr('href'),
            post_data = '',
            input = $('[name="email_or_group"]', form);
        switch(cur_tab_id) {
            case '#share-enter':
                post_data = input.val();
                break;
            case '#share-grp-options':
            case '#share-contact-options':
                $(cur_tab_id + ' .checkbox-checked .checkbox-orig').each(function() {
                    post_data += $(this).val() + ',';
                });
                input.val(post_data);
        }
        if (!post_data) {
            apply_form_error(form.attr('id'), "{% trans "Please enter emails or groups, or select some." %}");
            return false;
        }
        form.submit();
        disable($(this));
    });

    $('.unshare-btn').click(function() {
        location.href = $(this).data('url');
    });

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

    $(".download-btn").click(function() {
        var repo_id = $(this).parents('td').data('id');
        window.open('{{ SITE_ROOT }}seafile_access_check/?repo_id=' + e(repo_id));
    });

function repoCreateSuccessCallback(data) {
    $.modal.close();
    var new_repo_item = $('#new-repo .repo-item').clone(true); 
    var tds = $('td', new_repo_item);
    var repo_id = data['repo_id'], repo_name = data['repo_name'];
    $(tds[1]).html('<a href="{{ SITE_ROOT }}repo/' + repo_id + '/">' + repo_name + '</a>');
    $(tds[2]).html(data['repo_desc']);
    $(tds[4]).attr('data-id', repo_id).attr('data-name', repo_name);
    
    var my_own_repos = $('#my-own-repos');
    var empty_tip = $('.empty-tips', my_own_repos);
    if (empty_tip.length == 1) {
        var repo_table = $('#new-repo').clone().removeAttr('id').removeClass('hide');
        $('.repo-item', repo_table).replaceWith(new_repo_item);
        empty_tip.replaceWith(repo_table);
    } else {
        $('tr:first', my_own_repos).after(new_repo_item);
    }
}
