$(document).ready(function () {
    $('.field-collection[data-prototype]').on('collection-add-item', function () {
        $(this).find('select').each(function (i, e) {
            SovWidgets.setSelect2($(e));
            setSubtheme($(e));
        })
    });

    $('.theme').each(function (i,select){
        setSubtheme($(select));
        $(select).trigger('change');
    })
});


function setSubtheme($select) {
    $select.on('change', function (e) {
        $(e.target).closest('.form-widget-compound').find('.subtheme option').prop('disabled', false);
        $(e.target).closest('.form-widget-compound').find('.subtheme option[data-class]').not("[data-class='" + $(e.target).find('option:selected').data('class') + "']").prop('disabled', true);
        SovWidgets.setSelect2($(this).closest('.form-widget-compound').find('.subtheme'));
    });
}