/**
 * Created by IOAN on 11/26/2015.
 */
function enableCkSelect() {
    $('input.ws_categs').click(function(ev){
        var uid = $(this).val();
        if($(this).prop('checked')) {
            var title = $(this).attr('rel');
            var li = '<li id="s_' + uid + '"><input type="checkbox" id="' + uid + '" name="module_bl_categs['+uid+']" value="'+title+'" class="yp_categs" checked="checked"> '+title+'</li>';
            $('#categs_ul').append(li);
        }
        else {
            if($('#s_' + uid).length > 0) {
                $('#s_' + uid).remove();
            }
        }
    });
}

$(document).ready(function(){
    var $p = $('#bl_middle');
    var $ws = $('#ws_categs');
    var $yp = $('#categs');

    $('#ws_search_form', $p).submit(function(ev){
        ev.preventDefault();
        $('#search_yp_categs', $p).click();
        return false;
    });

    $('#search_yp_categs', $p).click(function(ev){
        ev.preventDefault();
        $.ajax({
            url: full_uri + '/amodules/bl_middle/ajax/get_ws_categories.php',
            data: 'term=' + $('#ws_categ_term').val() + '&ref=init',
            cache: false,
            type: 'post',
            dataType: 'json',
            success: function(msg) {
                //console.log(msg);
                $('#ws_categs_ul').empty();
                if(msg.result == 1) {
                    var data = msg.categs;
                    $.each(data['data'], function(uid, cat){
                        var checked = '';
                        if($('#s_' + uid).length > 0) {
                            checked = 'checked="checked"';
                        }
                        var li = '<li>';
                        li = li + '<input type="checkbox" rel="' + cat.cy_admin_title_en + '" id="ws_'+uid+'" value="' + uid + '" '+checked+' class="ws_categs"> ';
                        li = li + '' + cat.cy_admin_title_en + ' (' + cat.num_rec + ')';
                        li = li + '</li>'

                        $('#ws_categs_ul').append(li);
                    });
                    enableCkSelect();
                }
            }
        });

        /*

         */

        return false;
    });



});