﻿$(document).ready(function () {
    //alert('entrou');

    $('.classe_busca_ajax').keyup(function (e) {

        var texto = $(this).val();
        if (texto.length > 3) {
            $.ajax({
                type: "POST",
                url: "Busca.aspx/retornoBusca",
                data: JSON.stringify({ texto: texto }),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    var html = '';
                    var tipo = '';  
                    $.each(msg.d, function (key, val) {
                        if (val.tipo == '1')
                            val.link = 'noticias/' + val.link;
                        if (val.tipo == '2')
                            val.link = 'promocoes/' + val.link;
                        if (val.tipo == '3')
                            val.link = 'eventos/' + val.link;
                        if (tipo != val.titulo_tipo) {
                            html += '<dt>' + val.titulo_tipo + '</dt>';
                            tipo = val.titulo_tipo;
                        }
                        html += '<dd>' +
                                                '<a href="' + val.link + '" title="' + val.titulo_conteudo + '"><em>' + val.titulo_conteudo + '</em> <span>' + val.resumo + '</span></a>' +
                                           '</dd>';
                    });
                    if (html != '') {
                        $('#result-ajax').empty().html(html);
                        $('.results').slideDown();
                    }
                    else {
                        $('#result-ajax').empty();
                        $('.results').slideUp();
                    }
                }
            });
        }
        else {
            $('#result-ajax').empty();
            $('.results').slideUp();
        }
        e.preventDefault();
    });
});
