﻿/// <reference path="jquery-1.4.4-vsdoc.js" />
var autocompleteCities = function () {
    var cityCode;
    var cache = {};

    $(function () {

        var elems = {},
            lastXhr;

        elems = $('.autocompleteCities');
        $.each(elems, function (index, value) {
            $(value).autocomplete({
                autoFocus: true, 

                source: function (request, response) {
                    var term = request.term;
                    if (term in cache) {
                        response(cache[term]);
                        return;
                    }
                    lastXhr = $.getJSON("/handlers/Cities.ashx?amount=7", request, function (data, status, xhr) {

                        cache[term] = data;

                        if (xhr === lastXhr) {
                            response(data);
                        }
                    });
                }
            });

            $(value).autocomplete("option", "position", { my: "right top", at: "right bottom" });
        });
    });

    return {
        getCache: function () {
            return cache;
        }

    }
} ();



