var $wb_search_form = $('#wb_search_frm');
$(document).ready(function(){

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

    /*$('#fal_dcode').on('changed.bs.select', function(e, clickedIndex, newValue, oldValue){
        console.log(clickedIndex);
        console.log(newValue);
        console.log(oldValue);
    });*/

    $('#fal_dcode').on('change', function(ev){
        var dcode = $(this).val();

        var all_loc = 0;
        var opts = '<option value="" selected="selected">All Locations (x)</option>';
        $.each(json_dl[dcode]['locs'], function(lcode, l){
            if(typeof l.title != "undefined") {
                all_loc += l.num;
                opts += '<option value="' + lcode + '">' + l.title + ' ('+l.num+')</option>';
            }
        });

        opts = opts.replace('(x)', '(' + all_loc + ')');

        $('#fal_lcode').html(opts);
        $('#fal_lcode').selectpicker('refresh');

        var ck_selected = "";

        $('#fal_type option').each(function(){
            var tfm = $(this).val();
            var tfm_selected = $(this).selected == true;
            if(tfm_selected) ck_selected = tfm;
        });

        var all = 0;

        var l_opts = '<option value="" selected="selected">All Types (x)</option>';
        $.each(json_dl[dcode]['types'], function(fm_uid, num){
            var checked = '';
            if(ck_selected == fm_uid) checked = 'selected="selected"';
            l_opts += '<option value="' + fm_uid + '" '+checked+'>' + wb_types_js[fm_uid] + ' ('+num+')</option>';
            all = all + (num * 1);
        });

        l_opts = l_opts.replace('(x)', '(' + all + ')');
        $('#fal_type').html(l_opts);
        $('#fal_type').selectpicker('refresh');

    });

    $('#fal_lcode').on('change', function(ev){
        var lcode = $(this).val();
        var dcode = $('#fal_dcode').val();

        if(lcode == '') {

            $('#fal_dcode').change();
        }
        else {

            var ck_selected = "";

            $('#fal_type option').each(function(){
                var tfm = $(this).val();
                var tfm_selected = $(this).selected == true;
                if(tfm_selected) ck_selected = tfm;
            });

            var all = 0;

            var l_opts = '<option value="" selected="selected">All Types (x)</option>';
            $.each(json_dl[dcode]['locs'][lcode]['types'], function(fm_uid, num){
                var checked = '';
                if(ck_selected == fm_uid) checked = 'selected="selected"';
                l_opts += '<option value="' + fm_uid + '" '+checked+'>' + wb_types_js[fm_uid] + ' ('+num+')</option>';
                all = all + (num * 1);
            });

            l_opts = l_opts.replace('(x)', '(' + all + ')');
            $('#fal_type').html(l_opts);
            $('#fal_type').selectpicker('refresh');

        }

    });

    $('.wb-trigger-click', $wb_search_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).prop('checked')) {
                    $('.' + rel_class).prop('checked', false);
                    $(this).prop('checked', true);
                }
            }
            else {
                rel_id = $(this).attr('rel');
                $('#' + rel_id, $wb_search_form).prop('checked', false);
            }

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

            $wb_search_form.submit();
        });

    });
});
