function filtrovendas () {
    
    // Filtro
    filtrandovendas("todos");
    filtrandoalugueis("todos");
    
    $('.filtros a').click(function (data, handler) {
        $('.filtros a').removeClass('ativo');
        filtro = $(this).attr('class');
        $(this).addClass(filtro + ' ativo');
        filtrandovendas(filtro);
        filtrandoalugueis(filtro);
    });
    
    $('#carregar a').click(function (data, handler) {
        
        filtro = $('.filtros a[class *= "ativo"]').attr('class').replace(' ativo', '');
        
        page = Math.ceil($("#espacoVendas #box").length / 12);
        
        filtrandovendas (filtro, page);
        
    });
    
}

function filtrandovendas (filtro, page) {
    
    // Busca Imoveis
    $.ajax({
        type: "POST",
        url: "/index/filtrar/tipo/V",
        data: ({rp:        "12",
                page:      ((page != undefined)?page+1:"1"),
                filtro:    filtro}),
        dataType: "text",
        beforeSend: function() {
            $("#carregar a").html("Carregando...");
        },
        success: function(data){
            if(page != undefined) 
                if(data != '') $("#espacoVendas").append(data); else alert("Não foram encontrados novos registros.");
            else
                $("#espacoVendas").html(data);

        },
        complete: function() {
            $("#carregar a").html("Carregar novos imóveis a venda");
        }

    });
    
}

function filtrandoalugueis (filtro, page) {
    
    // Busca Imoveis
    $.ajax({
        type: "POST",
        url: "/index/filtrar/tipo/A",
        data: ({rp:        "12",
                page:      ((page != undefined)?page+1:"1"),
                filtro:    filtro}),
        dataType: "text",
        success: function(data){
            if(page != undefined) 
                if(data != '') $("#espacoAlugueis").append(data); else alert("Não foram encontrados novos registros para alugueis.");
            else
                $("#espacoAlugueis").html(data);

        }

    });
    
}
