var EventosPort = {
    init: function() {
        $('#input_base_ports').multiselect({ disableIfEmpty: true});

        $('input.toggle_multi_ed').click(function () {
            Cfgedit.toggleMultiedicion();
        });

        $('input.quitar_base_port').click(function () {
            var num = $(this).data('num');
            Cfgedit.quitarBaseport(this, num);
        });

        $('.add_port').change(function () {
            var num_last_port = $(this).data('num-last-port');
            Cfgedit.setPortNom(this.value, num_last_port);
        });

        $('.button_add_port').click(function () {
            Cfgedit.agregarPortada();
            return false;
        });

        $('#listados_portadas .sel_multiples_plantillas').each(function() {
            var this_selector = this;
            $(this_selector).multiselect({
                disableIfEmpty: true,
                onChange: function(element, checked) {
                    if (checked === true) { // seleccionan un elemento
                        $('#listados_portadas select.sel_multiples_plantillas').not(this_selector).each(function(index, other_selector) {
                            $(other_selector).children('option[value="'+element.val()+'"]').remove();
                            $(other_selector).multiselect('rebuild');
                        });
                        $(this_selector).multiselect('select', element.val());
                    } else if (checked === false) { // deseleccionan un elemento.
                        $('#listados_portadas select.sel_multiples_plantillas').not(this_selector).each(function(index, other_selector) {
                            $(other_selector).append($('<option>', {value:element.val(), text:element.val()}));
                            $(other_selector).multiselect('rebuild');
                        });
                        $(this_selector).multiselect('deselect', element.val());
                    }
                }
            });
        });

        $('.del_port').click(function () {
            var num_port = $(this).data('num-port');
            Cfgedit.quitarPortada(this, num_port);
        });
    }
};

$(document).ready(function () {
    EventosPort.init();
});
