EStore.Fields = function(formDOM) {
    this.form = formDOM;
    this.setupFields();
    if (document.getElementById('basket-master-question-block'))
        this.__quetions = new EStore.Questions(document.getElementById('basket-master-question-block'));
}

EStore.Fields.prototype = {
    setupFields: function() {
        var self = this;
        for (var i=0; i<this.form.elements.length; i++) {
            switch (this.form.elements[i].name) {
                case 'country' :
                    this.defineCountry();
                    $(this.form.elements[i]).change(function() { self.defineCountry(); });
                    break;
                case 'city' :
                    this.setupCitySelectFields();
                    break;
            }
        }
    },
    defineCountry: function() {
        if (this.getElValue('country') != 'Россия') {
            this.formClass = 'foreign';
            $(this.form).addClass('set-foreign');
            $(this.form).find('.city input, .region input').unautocomplete();
        } else {
            this.formClass = 'russia';
            $(this.form).removeClass('set-foreign');
            this.setupCitySelectFields();
        }
    },

    setupCitySelectFields: function() {
        var self = this;
        $.getJSON('/misc/cities.js', function(data) {
            self.citiesData = data;
            self.attachCitiesAutocomplete();
        });
    },
    attachCitiesAutocomplete: function() {
        var $inputs = $(this.form).find('.city input, .region input');
        var self = this;
        if ($inputs.length) {
            var params = {
                resultsClass : 'ac_results ac_results-o',
                minChars : 1,
                matchContains : false,
                highlight : false
            }
            var $regionInput = $inputs.eq(0),
                $cityInput = $inputs.eq(1);
            function regionInputChangeHandler() {
                var region = $regionInput.val().toLowerCase();
                if ($cityInput) {
                    $cityInput.unautocomplete();
                    if (self.citiesData[region]) {
                        $cityInput
                            .autocomplete(self.citiesData[region], params)
                            .blur(function() {
                                $(this).search();
                            });
                        if ($cityInput.data('orig-change-handler')) {
                            $cityInput.data('orig-change-handler').apply($cityInput);
                        }
                    }
                }
            }
            $regionInput
                .autocomplete(self.citiesData.r, params)
                .result(function() {
                    $cityInput.val('');
                    regionInputChangeHandler.apply(this);
                })
                .blur(function() {
                    $(this).search();
                });
            if ($regionInput.data('orig-change-handler')) {
                $regionInput.data('orig-change-handler').apply($regionInput);
            }
            regionInputChangeHandler();
        }
    },
    getElValue: function(name) {
        for (var i = 0; i < this.form.elements.length; i++) {
            if (this.form.elements[i].name === name)
                return this.form.elements[i].value;
        }
    }
};

