var $wb_search_top_form = $('#wb_search_top_frm');
$(document).ready(function(){

    $('.wb-selectbox').selectpicker({
        //style: 'btn-info',
        //size: 4
    });

   /* $('.wb-trigger-click', $wb_search_top_form).each(function(i){

        var rel_class = 'nothing-to-select';
        var is_all = $(this).hasClass('wb-all');

        $(this).on('click', function(ev){
            $.blockUI({
                message: '<h1>Updating your results...</h1>',
                css: {
                    border: '3px solid #333333',
                    padding: '15px',
                    backgroundColor: '#fff',
                    '-webkit-border-radius': '10px',
                    '-moz-border-radius': '10px',
                    opacity: .9,
                    color: '#333'
                }
            });
            if(is_all) {
                rel_class = $(this).attr('rel');
                if($(this).attr('checked')) {
                    $('.' + rel_class).attr('checked', false);
                    $(this).attr('checked', true);
                }
            }
            else {
                rel_id = $(this).attr('rel');
                $('#' + rel_id, $wb_search_top_form).attr('checked', false);
            }

            $('#rel_page', $wb_search_top_form).val(1);

            $wb_search_top_form.submit();
        });

    });*/

    /*if(wb_search_again == true) {
        $wb_search_top_form.append('<input type="hidden" name="_wb_search_results_ajax_call" value="true"/>');
        $($fm_uid_id, $wb_search_top_form).attr('checked', true);

        $.blockUI({
            message: '<h1>Updating your results...</h1>',
            css: {
                border: '3px solid #333333',
                padding: '15px',
                backgroundColor: '#fff',
                '-webkit-border-radius': '10px',
                '-moz-border-radius': '10px',
                opacity: .9,
                color: '#333'
            }
        });

        $wb_search_top_form.submit();
    }
    else {
        $wb_search_top_form.append('<input type="hidden" name="_wb_search_results_ajax_call" value="true"/>');
    }*/


    $('select.wb-trigger-change', $wb_search_top_form).on('change', function(e){
        var found_current = false;
        var id = $(this).attr('id');
        $('select', $wb_search_top_form).each(function(){
            var $el = $(this);

            if(found_current) {
                //$el.val('');
                //$el.selectpicker('deselectAll');
                $el.selectpicker('destroy');
                var $parent = $el.closest('.form-group');
                $el.remove();
                $parent.remove();
            }

            if($el.attr('id') == id) {
                found_current = true;
            }
        });
        $wb_search_top_form.submit();
    });

    /*$('#wb-dcode', $wb_search_top_form).on('change', function(ev){
        var post_data = {
            'district': $(this).val()
        };
        $.ajax({
            url: window.full_uri + 'amodules/wb_search_top/ajax/locations_by_district.php',
            data: post_data,
            type: 'post',
            dataType: 'json',
            success: function(r) {
                var $lcode = $('#wb-lcode');
                var opts = '<option value="" selected="selected">All</option>';
                _.each(r, function(v, i){
                    opts += '<option value="' + v.lcode + '">' + v.location + '</option>';
                });

                $('#wb-lcode').html(opts);
                $('#wb-lcode').selectpicker('refresh');
            }
        });
    });*/

    $('#wb-lcode').on('change',function(ev){

    });


    $('.wb-search-top-button').on('click', function(e) {
        $wb_search_top_form.submit();
    });


});
