﻿function initSearchBar() {
    var barra = $('#search');
    var busquedaAnterior = "";
    $('body').append("<div id='panelBusquedaInterno' style='position:fixed;'><div id='resultadoBusqueda'></div></div>");
    barra.focus(function () {
        if (barra.val() != "") {
            mostrarPanelBusqueda();
        }
    }).blur(function () {
        setTimeout(function () {
            $("#panelBusquedaInterno").hide();
        }, 200);
    }).keyup($.debounce(750, function () {
        if (barra.val() != '') {
            urlBusqueda = "/handlers/search/?noRegistros=6&buscar=" + barra.val();
            $.ajax({
                type: "GET",
                url: urlBusqueda,
                dataType: "xml",
                success: function (xml) {
                    mostrarPanelBusqueda();
                    $("#resultadoBusqueda").html("");
                    $(xml).find('Anuncio').each(function () {
                        var anuncio = $(this);
                        if (anuncio.attr("idTipoAnuncio") == 2) {
                            $("#resultadoBusqueda").append(
                                "<div class='searchItem'>" +
                                    "<a href='" + $(this).attr("FullURL") + "'>" +
                                        "<div class='imageSearch'>" +
                                            "<img src='" + $(this).find("Imagen").attr("URLImagen").replace('files/','050/files/') + "'/>" +
                                        "</div>" +
                                        "<div class='nombreNegocio'>" + $(this).attr("nombre") + "</div>" +
                                        "<div class='descripcion'>" + $(this).find("descripcionMini").text() + "</div>" +
                                    "</a>" +
                                "</div>");
                        }
                        else {
                            $("#resultadoBusqueda").append(
                                "<div class='searchItem'>" +
                                    "<a href='" + $(this).attr("FullURL") + "'>" +

                                        "<div class='nombreNegocio'>" + $(this).attr("nombre") + "</div>" +
                                        "<div class='descripcion'>" + $(this).attr("direccion") + "<br/>" + $(this).attr("telefono") + "</div>" +
                                    "</a>" +
                                "</div>");
                        }
                    });
                    //searchItemsChange();
                    $("#resultadoBusqueda").append(
                                "<div class='searchItem bottom'><a href='/?buscar="+barra.val()+"'>" +
                                "<div class='nombreNegocio'>Ver todos los resultados para \""+barra.val()+"\"</div>"+
                                "</a></div>"
                                );
                }
            });
        }
        else {
            $("#panelBusquedaInterno").hide();
        }
    }));
}

function mostrarPanelBusqueda() {
    var barra = $('#search');
    var panel = $("#panelBusquedaInterno");
    panel.show();
    var posicion = barra.offset();
    panel.css("top", "130px"); //posicion.top + barra.height() + 12+"px");
    panel.css("left", posicion.left+"px");
    
}

function searchItemsChange() {
    $("#resultadoBusqueda .searchItem").hover(
        function () {
            $(this).addClass('selected');
        },
        function () {
            $(this).removeClass('selected');
        }
        );
    }

    function initScrollTop() {
        $(window).scroll(function () {
            var scrollVal = $(this).scrollTop();
            if (scrollVal > 140 && $('#MainHeader').css('position') != 'fixed') {
                $('#MainHeader').css('top', '-160px').css('z-index', '90').css('position', 'fixed').animate({
                    top: '0',
                    opacity: 0.95
                }, 300, function () { });
            }
            else {
//                if (scrollVal < 140)
//                $('#MainHeader').css('top', '0').css('z-index', '90').css('position', 'relative');
            }
        });
    }
