eat.main = {
    timers: [],
    xhr: null,

    data: {
        city:null
    },
    
    dom: {},
    
    init: function() {        
        var self = this;

        eat.core.domCache(this.dom, ['sections','city','cityPreview','cityPreviewData','cityControl','cityMessage','cityForm','cityInput','citySubmit']);
                
        this.dom.cityInput.on('focus', function(e) {
            if(this.className.indexOf('dimmed')!=-1) {
                this.value = '';
                this.className = '';
            }
            
            self.dom.citySubmit.el.disabled = false;
        });
                
        this.dom.cityForm.on('submit', function(e) {
            e.preventDefault();
        
            if(self.dom.cityInput.el.className == '') {
                self.search();
            }
                
            return false;
        });
        
        this.render();
        
        this.dom.sections.show();
    },
    
    render: function() {
        var data = eat.main.data;

        if(data.city) {
        
            // set preview data
            this.dom.cityPreviewData.html(data.city.name);
            
            // hide control
            this.dom.cityControl.hide();
            
            // set state
            this.dom.city.el.className = 'sectionClosed';
            
        }
        else {

            // set preview data
            this.dom.cityPreviewData.html(__('city.help'));
            
            // show control
            this.dom.cityControl.show();

            // set form inputs    
            this.dom.cityInput.el.value = __('city.form.input.default');
            this.dom.cityInput.el.blur();
            this.dom.cityInput.addClass('dimmed');
            this.dom.citySubmit.el.disabled = true;

            // set state            
            this.dom.city.el.className = 'sectionOpen';
            
        }
    },
    

    search: function() {
        if(!this.dom.citySubmit.el.disabled) {
            var self = this;
            var data = eat.main.data;
            var xhrId = new Date().getTime();
            
            this.dom.citySubmit.el.disabled = true;

            eat.core.message(self.dom.cityMessage, __('city.progress.message'), { time:-1 });
            
            this.xhr = xhrId;

            window.setTimeout(function() {
                eat.core.ajax(settings.citySearchJsonUrl, { city: self.dom.cityInput.el.value }, function(json) {
                    if(self.xhr == xhrId) {
                        window.clearTimeout(self.timers[xhrId]);

                        if(json.status == 'success') {
                            eat.core.message(self.dom.cityMessage, __('city.success'), {
                                style: 'success',
                                callback: function() {
                                    self.setCity(json.city);                                      
                                },
                                delay: 500
                            });
                        }
                        else {
                            eat.core.message(self.dom.cityMessage, json.message, {
                                style: 'error',
                                delay: 500,
                                callback: function() {
                                    self.dom.citySubmit.el.disabled = false;
                                },
                                time:3000
                            });
                        }
                    };
                });
            }, ((eat.core.browser.symbian)?500:0));
                
            this.timers[xhrId] = window.setTimeout(function() {
                self.xhr = null;
                
                self.dom.citySubmit.el.disabled = false;
                
                eat.core.message(self.dom.cityMessage, __('city.timeout'), {
                    style: 'error'
                });
            }, 30000);
        }
    },
    
    setCity: function(city) {
        eat.core.scrollTop();
                
        eat.core.message(false, __('city.loading'), { time:-1 });
                
        window.setTimeout(function() {
            document.location = settings.cityIndexUrl+'?city='+city.normalizedname;
        }, 750);
    }
};
