﻿var selecteditem = -1;
var x;

$(document).ready(function() {

    $("#searchBox_text").bind('keyup', function(event) {
        if (event.keyCode == 13) {
            if (selecteditem >= 0) {
                $('#filterSearchProductsList li').each(function(index) {
                    if (index == selecteditem) {
                        $('#searchBox_text').val($(this).text());
                    }
                });
            }

            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.");
            }
        }
        else if (event.keyCode == 38) {
            event.preventDefault();
        }
        else if (event.keyCode == 40) {
            event.preventDefault();
        }
    });

    $("#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();
        selecteditem = -1;
    });
}

function searchSuggest() {
//    $("#searchBox_text").keyup(function(event) {

//        if (x != null) {
//            x.abort();
//        }

//        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" + "'}";

//        x = $.ajax({
//            type: "POST",
//            contentType: "application/json; charset=utf-8",
//            url: "SearchService.asmx/Search",
//            data: json,
//            //async: false,
//            dataType: "json",
//            success: function(result) {

//                $("#filterSearchProducts").remove();

//                if (result != null) {
//                    if (result.d != null) {
//                        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 % 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();
//                        }

//                        var maxitems = -1;

//                        // this will be used as a 0-based index...so subtract 1
//                        maxitems = $("#filterSearchProductsList li").length - 1;

//                        if (maxitems > -1) {
//                            if (event.keyCode == 38) {            // up arrow
//                                selecteditem = selecteditem - 1;

//                                if (selecteditem < 0) { selecteditem = maxitems; }

//                            } else if (event.keyCode == 40) {     // down arrow
//                                selecteditem = selecteditem + 1;

//                                if (selecteditem > maxitems) { selectitem = 0; }

//                            }

//                            $('#filterSearchProductsList li').each(function(index) {
//                                var thisclass = $(this).attr('class');

//                                // reset the classes
//                                if (thisclass == 'even highlighted') { $(this).attr('class', 'even'); }
//                                if (thisclass == 'odd highlighted') { $(this).attr('class', 'odd'); }

//                                // set the class of the selected item
//                                if (index == selecteditem) {
//                                    thisclass = thisclass + ' highlighted';
//                                    $(this).attr('class', thisclass);
//                                }
//                            });
//                        }
//                    }
//                }
//            },
//            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();
//    });
};


