﻿$(document).ready(function() {
    var CurIndex = -1;
    $("#ctl00_txtTopSearch").keyup(function(e) {
        var searchVal = $("#ctl00_txtTopSearch").val();
        if (searchVal.length > 1) {

            var parameters = "{'prefixText':'" + searchVal + "','count':'" + 15 + "'}"

            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "/webservices/wsautocomplete.asmx/GetPartNumber",
                data: parameters,
                dataType: "json",
                success: function(msg) {

                    var list = ParseResults(msg.d);
                    //set list content and show 
                    if (list != "") {
                        if (e.which != 13) {
                            $("#divRes").show();
                        }
                        $("#divRes").attr("innerHTML", list);
                        $("#divRes").addClass("SuggestionBox");
                    }
                    else {
                        $("#divRes").hide();
                    }
                    //arrow down and up
                    if (e.which == 38) {
                        CurIndex--;
                        $("#li" + CurIndex).attr("class", "highlight");
                    }
                    else if (e.which == 40) {
                        CurIndex++;
                        $("#li" + CurIndex).attr("class", "highlight");
                    }
                    
                    //list item hover handler
                    $("#divRes li").hover(function() {
                        $(this).attr("class", "highlight");
                    }, function() { $(this).attr("class", ""); });

                    //list item click handler
                    $("#divRes li").click(function() {
                        var curText = $(this).text();
                        if (curText != '') {
                            $("#ctl00_txtTopSearch").val(curText);
                        }

                        e.preventDefault();
                        $("#divRes").hide();

                        //this function can be found in utilities.js
                        Search('ctl00_txtTopSearch');
                        return false;
                    });
                },
                error: function(e) {
                }
            });
        }
        else {
            $("#divRes").attr("innerHTML", "");
            $("#divRes").removeClass("SuggestionBox");
        }
    });

    //handle enter button
    $(document).keypress(function(e) {
        if (e.keyCode == 13) {

            var curText = $("#li" + CurIndex).text();
            if (curText != '') {
                $("#ctl00_txtTopSearch").val(curText);
            }

            e.preventDefault();
            $("#divRes").hide();

            //this function can be found in utilities.js
            Search('ctl00_txtTopSearch');
            return false;
        }
    });
});

//create results list
function ParseResults(res) {
    var text = "";
    if (res.length > 0) {
        text = "<ul>";
        for (var i = 0; i < res.length; i++) {
            if (i % 2 == 0) {
                text += "<li style=\"cursor:pointer\" id=\"li" + i + "\">" + res[i] + "<br/></li>";
            }
            else {
                text += "<li style=\"cursor:pointer\" class=\"alt\" id=\"li" + i + "\">" + res[i] + "<br/></li>";
            }
        }
        text += "</ul>";
    }
    return text;
}
       