var supplier_form = {
    settings: {
        city_autocomplete_url: null
    },
    init: function() {
        supplier_form.bindAutocomplete();
        supplier_form.bindElement();
        supplier_form.fileField_arrangeIndexes();
        supplier_form.fileField_bindRemove();
    },
    bindAutocomplete: function() {
        global.autocomplete.bind(supplier_form.city_autocomplete_url, '#supplier_form_city_description', '#supplier_form_city');
    },
    bindElement: function() {
        jQ('#href-new-file-field').unbind('click').bind('click', this.newFileField);
    },
    newFileField: function() {

        var div_ig = jQ('<div/>'),
            div_igb = jQ('<div/>'),
            btn = jQ('<button/>'),
            input_file = jQ('<input/>');

        div_ig.addClass('input-group').addClass('no-margin-bottom');
        div_igb.addClass('input-group-button');
        btn.attr('type', 'button').addClass('button secondary').addClass('file-field-remove').html('<i class="fa fa-remove"></i>');
        input_file.attr('type', 'file').addClass('input-group-field');

        div_igb.append(btn);
        div_ig.append(input_file).append(div_igb);

        jQ('#file-field-container').append(div_ig);

        supplier_form.fileField_arrangeIndexes();
        supplier_form.fileField_bindRemove();
    },
    fileField_arrangeIndexes: function() {
        jQ('#file-field-container .input-group').each(function(key){
            jQ(this).attr('id', 'file-field-input-group-' + key).data('key', key);
            jQ(this).find('input[type=file]').attr('name', 'files[' + key + ']');
            jQ(this).find('button').data('key', key);
        });
    },
    fileField_bindRemove: function() {
        jQ('.file-field-remove').unbind('click').bind('click', function(){
            jQ('#file-field-input-group-' + jQ(this).data('key')).remove();
            supplier_form.fileField_arrangeIndexes();
        });
    },
};