
var attachments = {

    batchLastIndex: 0,

    deleteFile: function(id, docType, docId) 
    {
        var self = this;
        $('#file_wait_'+id+', #file_delete_'+id).toggle();
        $.getJSON(BASE_URL+'/attachments/delete/doc_type/'+docType+'/doc_id/'+docId+'/file_id/'+id, {}, 
            function(resp)
            {
                if (resp.result) {      // ok
                    $('#file_'+id).fadeOut('slow', 
                        function()
                        {
                            var p = $(this).parent();
                            $(this).remove();
                            if (p.parent().attr('id') == 'batch_upload') {
                                self.syncBatchUploadLists();
                            } else {
                                if ($('div[id^=file_]', p).length == 0) {
                                    $('div.empty_placeholder', p).show();
                                }
                            }
                        }
                    );
                } else {    // fail
                    $('#file_wait_'+id+', #file_delete_'+id).toggle();
                    alert(resp.error_msg);
                }
            }
        );
    },

    addToBatch: function(el)
    {
        var p = $(el).parent();
        var inp_html = p.html();
        $(el).attr('name', 'file_N' + (++this.batchLastIndex)).attr('index', this.batchLastIndex).appendTo('#files_in_batch');
        this.updateFilesInBatchList();
        p.html(inp_html);
    },

    removeFromBatch: function(n)
    {
        $('input[type=file][index='+n+']', '#files_in_batch').remove();
        this.updateFilesInBatchList();
    },

    getFilesAsParams: function()
    {
        var files = {};
        $('input[type=file]', '#files_in_batch').each(
            function()
            {
                var id = parseInt(this.getAttribute('index'));
                if (!isNaN(id)) {
                    files['file['+id+']'] = this.value;
                }
            }
        );
        return files;
    },

    updateFilesInBatchList: function()
    {
        var self = this;
        $.post(BASE_URL+'/attachments/get_files_in_batch_html', this.getFilesAsParams(),
            function(resp)
            {
                if (resp.result) {     // ok
                    $('.file_list:last', '#batch_upload').html(resp.html);
                    self.syncBatchUploadLists();
                } else {    // fail
                    alert(resp.error_msg);
                } 
            }, 'json'
        );
    },

    syncBatchUploadLists: function()
    {
        var fl_1 = $('.file_list:first', '#batch_upload');
        var fl_2 = $('.file_list:last', '#batch_upload');
        
        var empty1 = $('div[id^=file_]', fl_1).length == 0;
        var empty2 = fl_2.html().length == 0;

        if (empty1 && empty2) {
            $('div.empty_placeholder', fl_1).show();
            $('.separator_line', '#batch_upload').hide();
        } else if (!empty1 && !empty2) {
            $('div.empty_placeholder', fl_1).hide();
            $('.separator_line', '#batch_upload').show();
        } else {            
            $('div.empty_placeholder', fl_1).hide();
            $('.separator_line', '#batch_upload').hide();
        }
    }

}

