jQuery( window ).ready(function($) {
    $('.compartivo_canais_busca #search_canais').on('click',function(){
        filter();
    });
    
    $('.compartivo_canais_busca #in-canal-type, .compartivo_canais_busca #in-canal-tag, .compartivo_canais_busca #in-canal-plano, .compartivo_canais_busca #in-canal-search').on('change blur search',function(){
        filter();
    });
    
    function filter(){
        var current_type = $('.compartivo_canais_busca #in-canal-type').val();
		var current_tag = $('.compartivo_canais_busca #in-canal-tag').val();
		var current_plano = $('.compartivo_canais_busca #in-canal-plano').val();
        var current_name = $('.compartivo_canais_busca #in-canal-search').val();
		
        $('.channel-list .channel').show();
        
        var mylist = $('.channel-list #dk_list_class');
        var listitems = mylist.find(".channel");
        
        listitems.sort(function(a, b) {
            var numberA = parseFloat($(a).attr('data-number'));
            var numberB = parseFloat($(b).attr('data-number'));
            return (numberA < numberB) ? -1 : (numberA > numberB) ? 1 : 0;
        })
        $(mylist).append(listitems);
        
        $('.channel-list .channel').each(function(index){
            var $el = $(this);
			
            if(current_plano && current_plano != ""){
                if($el.data('prods').search(current_plano) < 0){
					$el.hide();
                }
            }
			if(current_type && current_type != ""){
                if($el.data('cats').search(current_type) < 0){
					$el.hide();
                }
            }
			if(current_tag && current_tag != ""){
                if($el.data('tags').search(current_tag) < 0){
					$el.hide();
                }
            }
			

            if(current_name && current_name != ""){
                var search = new RegExp(current_name,'ig');
                if($el.data('name').search(search) < 0 && $el.attr('data-number').search(search) < 0) {
                    $el.hide();
                } 
            }
        })
    }

    setTimeout(function() {
        filter();    
    }, 500);
	
});