var RequestController = new Class({
    sendable: true,
    
    data: {},
    
    Implements: [Options, Events],
    
    options: {
        /*onBeforeSend: $empty,
        onResponseFailure: $empty,
        onResponseSuccess: $empty,
        onResponseError: $empty,
        onComplete:$empty,
        onPopHide: $empty,*/
        'title': 'Your request is being processed',
        'message': 'Message here',
        'action': false,
        'method': false
    },
    
    initialize: function(options){
        this.setOptions(options);
        this._build();
    },
    
    _fixAction: function(action){
        var url = action || this.options.action,
            check_host = window.append_hosts || ['dealersite', '192.168.10.224', 'crm.dealercrm.com', 'crm.imagiclab', '192.168.10.224'];
            
        if(check_host.contains(window.location.hostname)){
            url = '/common/members/control/mycarlink' + url;
        }

        return url;
    },
    
    _build: function(){        
        this.request = new Request({
            'url': this._fixAction(),
            'method': this.options.method,
            'onComplete': function(response){
                var resp = JSON.decode(response);
                
                this.fireEvent('complete', [resp]);

                switch(resp.status){
                    case 'success':
                        this.fireEvent('responseSuccess', [resp]);
                        break;
                    
                    case 'error':
                        this.fireEvent('responseError', [resp]);
    			        var m = resp.message || '';
                        var message = m + '<ul>';
                        
                        for(var i in resp.error_fields){
                            message += '<li>'+ resp.error_fields[i] + '</li>';
                        }
                        
                        resp.message = message + '</ul>';
                        break;
                    
                    case 'failure':
                        this.fireEvent('responseFailure', [resp]);
                        break;                        
                }
                
                this._updatePopup(resp.title || resp.status, resp.message);
                
            }.bind(this)
        });
        
        return this;
    },

    _updatePopup: function(title, message){
        if(window.popup.shown){
            window.popup.title(title).content(message);
        }
        
        return this;
    },
    
    setAction: function(url){
        if(this.request){
            this.request.options.url = this._fixAction(url);
        }
        
        return this;
    },
    
    send: function(){
        this.fireEvent('beforeSend');

        window.popup.show(this.options.title, this.options.message);

        this.request.send(this.data);
    }
});

RequestController.Form = new Class({    
    Extends: RequestController,
    
    initialize: function(form, options){
        this.form = $(form);
        
        if(!this.form) return;
        
        this.parent(options);
        this._setUp()
            ._actions()
    },
    
    _setUp: function(){
        if(!this.options.action){
            this.request.options.url = this._fixAction(this.form.get('action'));
        }
        
        if(!this.options.method){
            this.request.options.method = this.form.get('method');
        }
        
        return this;
    },
    
    _actions: function(){
        this.form.addEvent('submit', function(e){
            e.stop();
            
            if(this.sendable){
                this.data = this.form.toQueryString();
                this.send();
            }
        }.bind(this));
        
        return this;
    }
});

RequestController.Link = new Class({
    Extends: RequestController,
    
    initialize: function(link, options){
        this.link = $(link);
        
        if(!this.link) return;

        this.parent(options);
        this._actions();
    },
    
    _actions: function(){
        this.link.addEvent('click', function(e){
            e.stop();
            
            if(this.sendable){
                this.send()
            }
        }.bind(this));
        
        return this;
    }    
});