var Splash = new Class({
    submit_form: true,
    
    initialize: function(){
        this.alpha_test         = $('alpha_test');
        this.form               = $('signup_form');
        this.cancel             = $('cancel');
        this.captcha            = $('new_captcha');
        this.captcha_container  = $('captcha_view');
        this.form_slide         = new Fx.Slide('signup_wrapper').hide();

        this.cancel.addEvent('click', function(e){
            this.form_slide.toggle();
            this.newEquation();
        }.bind(this));
        
        this.alpha_test.addEvent('click', function(e){
            e.stop();
            this.form_slide.toggle(); 
        }.bind(this));
        
        this.captcha.addEvent('click', function(e){
            e.stop();
            this.newEquation();
        }.bind(this));
        
        this.formAction().setSend();
    },
    
    formAction: function(){
        this.form.addEvent('submit', function(e){
            e.stop();
            
            if(this.submit_form){
                this.request.send(this.form.toQueryString());
                this.message_title = 'The request is loading';
                this.coverForm();
            }
        }.bind(this));        
        
        return this;
    },
    
    newEquation: function(){
        this.request.send('action=captcha');
    },
    
    setSend: function(){
        this.request = new Request({
            'url': '/process.php',
            'method': 'post',
            'onComplete': function(result){
                var resp = JSON.decode(result);

                switch(resp.status){
                    case 'new_captcha':
                        this.captcha_container.set('html', resp.message);
                        break;
                    
                    case 'errors':
                        var errors = [];
                        
                        for(var i in resp.errors){
                            errors.push(new Element('li').set('html', resp.errors[i]));
                        }
                        
                        if(errors.length){
                            new Element('ul').adopt(errors).inject(this.message_body);
                            this.message_title.set('html', 'Errors');
                        }
                        
                        break;
                    
                    case 'success':
                        this.message_title.set('html', 'success!');
                        this.message_body.set('html', resp.message);
                        this.form.reset();
                        break;
                }
            }.bind(this)
        });
        
        return this;
    }, 
    
    coverForm: function(){
        this.submit_form = false;
        
        var size = this.form.getSize();
        
        this.cover = new Element('div', {
            'styles' : {
                'height' : size.y,
                'width' : size.x,
                'background-color' : '#000',
                'opacity' : .8,
                'position': 'absolute',
                'top': 0,
                'left' : 0,
                'z-index': 100
            }
        }).inject(this.form);
        
        this.message_title = new Element('div', {'class': 'message_title'}).set('html', this.message_title_text);
        this.message_body = new Element('div', {'class': 'message_body'}).set('html', this.message_body_text);
        
        this.close_button = new Element('a', {
            'href' : '#',
            'events' : {
                'click' : function(e){
                    e.stop();
                    this.removeCover();
                }.bind(this)
            }
        }).set('html', 'close');
        
        this.message_buttons = new Element('div', {'class' : 'message_buttons'}).adopt(this.close_button);
        
        this.message = new Element('div', {
           'id' : 'message',
           'styles' : {
               'top' : (size.y - 150) / 2,
               'left' : (size.x - 300) / 2,
               'z-index' : 150
           } 
        }).adopt(this.message_title, this.message_body, this.message_buttons).inject(this.form);
        
        return this;
    },
    
    removeCover: function(){
        if(this.cover) this.cover.dispose();
        
        if(this.message) this.message.dispose();
        
        if(this.close_button) this.close_button.dispose();
        
        if(this.message_buttons) this.message_buttons.dispose();
        
        this.submit_form = true;
        
        return this;
    }
});