﻿$(document).ready(function() {

    $("#searchBox_text").bind('keyup', function(event) {
        if (event.keyCode == 13) {
            if ($('#searchBox_text').val().length > 2) {
                window.location.href = 'vsearch.aspx?SearchTerm=' + $('#searchBox_text').val()
            } else {
                alert("Please enter at least 3 characters in the Search For field.");
            }
        }
    });

    $("#searchBox_button").click(function() {
        if ($('#searchBox_text').val().length > 2) {
            window.location.href = 'vsearch.aspx?SearchTerm=' + $('#searchBox_text').val()
        } else {
            alert("Please enter at least 3 characters in the Search For field.");
        }
    })
    searchSuggest();
});

function hideSearch() {
    $("#filterSearchProducts").hover(function() {

    }, function() {
        $(this).hide();
    });
}

function searchSuggest() {
    $("#searchBox_text").keyup(function () {
        var search = $(this);
        var offset = search.offset();
        var term = search.val();

        $("#searchBox").val(term);
        if (term.length <= 1) {
            $("#filterSearchProducts").remove();
            return false;
        }

        "{'origin':'value'}"
        var entityName = $("#thisEntityName").val();
        if (entityName == null) entityName = '';

        var entityID = $("#thisEntityID").val();
        if (entityID == null) entityID = 0;

        var json = "{'SearchTerm':'" + term + "'," +
			"'EntiyID':'" + 0 + "'," +
			"'EntityName':'" + "category" + "'}";

        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "SearchService.asmx/Search",
            data: json,
            dataType: "json",
            success: function (result) {

                $("#filterSearchProducts").remove();

                if (result.d.length > 0) {

                    var list = $('<ul id="filterSearchProductsList" />');
                    //list.append("<li><a id=\"seeResults\" href=\"vsearch.aspx?SearchTerm=" + term + "\">See All Results</a></li>");
                    $.each(result.d, function (index, product) {
                        if (index < 11) {
                            if (index % 2 == 0) {
                                list.append('<li class="odd"><a href="' + product.Url + '">' + product.Name + '</a></li>');
                            } else {
                                list.append('<li class="even"><a href="' + product.Url + '">' + product.Name + '</a></li>');
                            }
                        }
                    });
                    $("#thisBaseURL").val()
                    //Vibe_AjaxRequest('" + BaseURL + Separator + "Filter=[pagenum=" + NumPages.ToString() + otherParam + "]');
                    list.append("<li><a id=\"seeResults\" href=\"vsearch.aspx?SearchTerm=" + term + "\">See All Results</a></li>");
                    $('<div id="filterSearchProducts" />').css({ "position": "absolute", "left": offset.left + 1, "top": offset.top + 20 }).prepend(list).prependTo("body");
                    hideSearch();
                }
            },
            error: function (xhr, msg) {

                $("#filterSearchProducts").remove();
                //alert(xhr.responseText);
            }
        });
    }).blur(function () {
        setTimeout("hideSearch()", 300);
    }).focus(function () {
        var offset = $(this).offset();
        $("#filterSearchProducts").css({ "position": "absolute", "left": offset.left + 1, "top": offset.top + 20 }).show();
    });
};

