﻿(function ($) {
    $.fn.autoSuggest = function (targetUrl, targetDiv) {
        return this.each(function () {
            var currentIndex = 1;
            var maxLength = $(targetDiv).find("li").length;

            $(targetDiv).hide();
            $(this).bind('keyup focus', function (e) {
                if (e.keyCode == 38) {
                } else {
                    // execute the autosuggest functionallity
                    var tBox = this;
                    $.ajax({
                        type: 'POST',
                        url: targetUrl,
                        data: { 'input': $(this).val() },
                        dataType: 'json',
                        contentType: 'application/x-www-form-urlencoded',
                        global: false,
                        complete: function (data) {
                            processResults(data, tBox, targetDiv);
                        }
                    });
                }
            });

            
            $(this).bind('keyup', function (e) {
                //                alert(e.keyCode);
                switch (e.keyCode) {
                    case 38: // up
                    case 40: // down                        
                };
            });

            $(this).bind('blur', function () {
                $(targetDiv).fadeOut();
            });
        });

        function processResults(data, textBox, targetDiv) {
            if (data.status == 200) {
                $(targetDiv).fadeIn();
                $(targetDiv).html(data.responseText);
            } else {
                $(targetDiv).fadeOut();
            }
        };
    };
})(jQuery);
