var acListTotal = 0;
var acListCurrent = -1;
var acDelay = 800;
var acURL = null;
var acSearchId = null;
var acResultsId = null;
var acSearchField = null;
var acResultsDiv = null;
$(document).click(function () {
    clearAutoComplete();
    return;
});
function setAutoComplete(field_id, results_id, get_url) {
    acSearchId = "#" + field_id;
    acResultsId = "#" + results_id;
    acURL = get_url;
    $("body").append('<div id="' + results_id + '"></div>');
    acSearchField = $(acSearchId);
    acResultsDiv = $(acResultsId);
    repositionResultsDiv();
    acSearchField.keyup(function (e) {
        var keyCode = e.keyCode || window.event.keyCode;
        var lastVal = acSearchField.val();
        if (lastVal.length < 2) {
            clearAutoComplete();
            return;
        }
        $(acSearchId).addClass("textloading");
        if (updownArrow(keyCode)) {
            return;
        }
        if (keyCode == 13 || keyCode == 27) {
            clearAutoComplete();
            return;
        }
        setTimeout(function () {
            autoComplete(lastVal)
        },
        acDelay);
    });
}
function autoComplete(lastValue) {
    var checkVar = acResultsDiv.css("display");
    if (checkVar == 'block') {
        clearAutoComplete();
    }
    var part = acSearchField.val();
    if (part == '') {
        clearAutoComplete();
        return;
    }
    if (part.length < 2) {
        clearAutoComplete();
        return;
    }
    if (lastValue != part) {
        return;
    }
    $.getJSON(acURL + part, function (json) {
        var ansLength = acListTotal = json.length;
        if (ansLength > 0) {
            var newData = '';
            for (i = 0; i < ansLength; i++) {
                newData += '<div class="unselected">' + json[i] + '</div>';
            }
            $('.dropdown').hide();
            acResultsDiv.html(newData);
            acResultsDiv.css("display", "block");
            var divs = $(acResultsId + " > div");
            divs.mouseover(function () {
                divs.each(function () {
                    this.className = "unselected";
                });
                this.className = "selected";
            })
            $(acSearchId).removeClass("textloading");
        } else {
            $(acSearchId).removeClass("textloading");
        }
    });
}
function clearAutoComplete() {
    acResultsDiv.html('');
    acResultsDiv.css("display", "none");
    $(acSearchId).removeClass("textloading");
    $('.dropdown').show();
}
function repositionResultsDiv() {
    var sf_pos = acSearchField.offset();
    var sf_top = sf_pos.top;
    var sf_left = sf_pos.left;
    var sf_height = acSearchField.height();
    var sf_width = acSearchField.width();
    acResultsDiv.css("position", "absolute");
    acResultsDiv.css("left", sf_left - 0);
    acResultsDiv.css("top", sf_top + sf_height + 5);
    acResultsDiv.css("width", sf_width + 5);
}
function updownArrow(keyCode) {
    if (keyCode == 40 || keyCode == 38) {
        if (keyCode == 38) {
            if (acListCurrent == 0 || acListCurrent == -1) {
                acListCurrent = acListTotal - 1;
            } else {
                acListCurrent--;
            }
        } else { if (acListCurrent == acListTotal - 1) {
                acListCurrent = 0;
            } else {
                acListCurrent++;
            }
        }
        acResultsDiv.children().each(function (i) {
            if (i == acListCurrent) {
                acSearchField.val(this.childNodes[0].nodeValue);
                this.className = "selected";
            } else {
                this.className = "unselected";
            }
        });
        return true;
    } else {
        acListCurrent = -1;
        return false;
    }
}
