function modulesFiltering() {
    $.expr[":"].contains = $.expr.createPseudo(function (arg) {
        return function (elem) {
            return $(elem).text().toUpperCase().indexOf(arg.toUpperCase()) >= 0;
        };
    });
    $('#modules-filter').keyup(function () {
        var filter = $(this).val();
        if (filter.length > 0) {
            $('.replacement-modules > .form-group').hide();
            $('.replacement-modules > .form-group:contains(Wszystkie)').show();
            $('.replacement-modules > .form-group:contains(' + filter + ')').show();
        } else {
            $('.replacement-modules > .form-group').show();
        }
    });
}

var selectedModules = [];
function modulesCheckboxes() {
    $('.replacement-modules > .form-group').each(function (index) {
        var isChecked = false;
        $checkbox = $(this).find('.icheck input[type=checkbox]');
        if ($checkbox.is(':checked')) {
            isChecked = true;
        }
        selectedModules[index] = isChecked;
        $checkbox.on('ifClicked', function () {
            selectedModules[index] = !$(this).is(':checked');
        });
    });
}
function checkAll(parent_class_name) {
    // $(parent_class_name + ' .icheck:visible').each(function () {
    //     $(this).find('input[type="checkbox"]').iCheck('check');
    // });
    $(parent_class_name).each(function () {
        $(this).find('input[type="checkbox"]').attr('checked', true);
    });

    // $(parent_class_name).each(function (index) {
    //     $checkbox = $(this).find('.icheck input[type=checkbox]');
    //     $checkbox.iCheck('check');
    // });
}
function uncheckAll(parent_class_name){
    // $(parent_class_name + ' .icheck:visible').each(function () {
    //     $(this).find('input[type="checkbox"]').iCheck('uncheck');
    // });
    $(parent_class_name).each(function () {
        $(this).find('input[type="checkbox"]').attr('checked', false);
    });

    // $(parent_class_name).each(function (index) {
    //     $checkbox = $(this).find('.icheck input[type=checkbox]');
    //     if(selectedModules[index] == false){
    //         $checkbox.iCheck('uncheck');
    //     }
    // });
}
function checkAllModules() {
    var checkAllCheckbox = $('#check-all');
    if (checkAllCheckbox.is(":checked")) {
        checkAll('.replacement-modules > .form-group', true);
    } else {
        uncheckAll('.replacement-modules > .form-group', false);
    }

    // checkAllCheckbox.on('ifChecked', function () {
    //     checkAll('.replacement-modules > .form-group', true);
    // });
    // checkAllCheckbox.on('ifUnchecked', function () {
    //     uncheckAll('.replacement-modules > .form-group', false);
    // });
}

function findReplacements(type1, type2) {
    var input = $('#replacement-search' + type1);
    var trigger = $('#replacement-search-trigger');
    var loader = input.data('loader');
    var options = {
        source: '/daj_adresy_ac',
        appendTo: '#replacement-search-holder',
        disabled: false,
        select: function (event, ui) {
            selectReplacement(
                ui.item.bNazwa,
                ui.item.bEmail,
                ui.item.bGid,
                ui.item.bCzyMozeZaakceptowacZaPozostalych,
                ui.item.bCzyMusiZaakceptowac,
                ui.item.bNazwaWyswietlana,
				type1,
				type2
            );
        },
        search: function () {
            // showLoader(loader);
        },
        response: function () {
            // hideLoader(loader);
        },
        open: function () {
            // hideLoader(loader);
        }
    };
    input.autocomplete(options);
    // trigger.click(function (e) {
    //     e.preventDefault();
    //     var value = input.val();
    //     input.autocomplete('enable');
    //     input.autocomplete('search', value);
    // });
}
function selectReplacement(name, email, gid, accept_for_all, must_accept, displayName, type1, type2) {
    $('#email-' + type2).val(email);
    $('#replacement-search' + type1).val(displayName);
    $('.gid-' + type2).val(gid);
    $('#akceptuje-za-pozostalych-' + type2).val(accept_for_all);
    $('#musi-zaakceptowac-' + type2).val(must_accept);
}
$(document).ready(function () {
    modulesFiltering();
    modulesCheckboxes();
    checkAllModules();
	findReplacements('1', 'zastepowanego');
    findReplacements('2', 'zastepujacego');
});
