var RequestController = new Class({
    Implements: [Options, Events],
    
    _setRequest: function(){
        this.request = new Request({
            'url': '/',
            'method': 'post',
            'onComplete': function(response){
                this.response_object = JSON.decode(response);
                
                switch (this.response_object.status) {
                    case 'success':
                        this.processSuccess();
                        break;
                    
                    case 'error':
                        this.processError();
                        break;
                    
                    default:
                        this.statusWindow.alert('There was an error processing your request');
                }
                
            }.bind(this)
        });
        
        return this;
    }
});

RequestController.Form = new Class({
    Implements: [Options, Events],
    
    Extends: RequestController,
    
    options: {
        start_message: 'Your request is being processed'
    },
    
    initialize: function(form, options){
        this.form = $(form);
        
        this.statusWindow = new Status();
        
        if(!this.form) return;
        
        this.setOptions(options);
        
        this._setRequest()._addEvent();
    },
    
    processSuccess: function(){
        var button_info;
        
        switch (this.response_object.button_type) {
            case 'location':
                button_info = {
                    'Finished': {
                        'type': 'location',
                        'href': this.response_object.button_location
                    }
                };
                break;
            
            case 'refresh':
                button_info = {
                    'close': {
                        'type': 'close'
                    }
                };
                break;
            
            default:
                button_info = {
                    'close': {
                        'type': 'close'
                    }
                };
        }
        
        this.statusWindow.title('Success').message(this.response_object.message).buttons(button_info).show();
        if (this.response_object.button_type == 'refresh') {
            this.form.reset();
        }
    },
    
    processError: function(){
        var error_hash  = new Hash(this.response_object.error_fields);
        error_message   = error_hash.toErrorFields();        
        
        this.statusWindow.title('Errors').message(error_message).buttons({
            'close' : {
                'type' : 'close'
            }
        }).show();
    },
    
    sendRequest: function(){
        this.request.setOptions({
            'url': this.form.get('action'),
            'method': 'post'
        });
        this.request.send(this.form.toQueryString());
    },
    
    _addEvent: function(){
        this.form.addEvent('submit', function(e){
            e.stop();
            this.statusWindow.title('Processing....please wait').message(this.options.start_message).buttons({
                'close': {
                    'type': 'close'
                }
            }).show();
            this.sendRequest();
        }.bind(this));        
        
        return this;
    }
});
