$(function() {
    $('#searcher_result').on('click', '.upsl, .downsl', function() {
        $('[name=l]').val($(this).attr('id').replace('sl', ''));
        $('#search_form').submit();
    });
    $('#search_field').on('change keyup', function() {
        $('#searcher_result').prepend('&nbsp;').children('span').fadeOut(500, function() {
            $('#searcher_result span').remove();
        });
    });

    searcher_check_sl();
    function searcher_check_sl() {
        setTimeout(function() {
            if($('#sc[name=searcher]').length & $('#l[name=searcher]').length) {
                var searcher_sc = parseInt($('#sc[name=searcher]').val());
                var searcher_l = parseInt($('#l[name=searcher]').val());
                var searcher_result = '&nbsp;';
                if(searcher_sc) {
                    $.ajax({
                        type    :   'POST',
                        url     :   '/callback/searcher.php',
                        data    :   'scsl=' + searcher_sc,
                        success :   function(asl) {
                            if(asl == 0) searcher_check_sl();
                            else if(asl > 1) {
                                if(searcher_l  == 1) searcher_result = '<span class="upsl" id="sl' + (searcher_l + 1) + '">расширить результаты поиска</span>';
                                else if(searcher_l < asl) searcher_result = '<span class="downsl" id="sl' + (searcher_l - 1) + '">сузить результаты поиска</span><span class="upsl" id="sl' + (searcher_l + 1) + '">расширить результаты поиска</span>';
                                else searcher_result = '<span class="downsl" id="sl' + (searcher_l - 1) + '">сузить результаты поиска</span>';
                            }
                            $('#searcher_result').html(searcher_result);
                            if(searcher_result != '&nbsp;') {
                                effect_el('highlight', {color: '#FF0000'}, 4000, 1);
                            }
                        }
                    });
                }
            }
        }, 2000);
    }
    function effect_el(e, o, t, r) {
        if(!r) $('#searcher_result > span').effect(e, o, t);
        else {
            $('#searcher_result > span').effect(e, o, t, function() {
                effect_el(e, o, t, r);
            });
        }
    }
});
