Portfolio = new Class({
    initialize: function () {
        this.signin_main = $('sign_in-main-form');
        this.forgot_password = $('forgot_password-form');
        this.activate = $('activate-form');
        
        this.showing_forgot_password = false;
        this.requested_password = false;
        
        if($('slider')){
            this.slider = $('slider');
            this.slide_to = -230;
            this.slider.set('tween',{duration:600, transition:Fx.Transitions.Expo.easeOut});

            if (this.signin_main.getFirst('div').hasClass('errors')) {
                this.signin_main.setStyles({'margin-top':10});
                this.slide_to = -260;
            }            
        }
        
        this.register_events();
    },
    
    handle_activation: function (email, customer_number) {
        this.activate.getChildren().each(function(item){ item.fade('out'); });
        (function(){ this.activate.setStyle('background','url(/media/img/loader.gif) no-repeat center') }.bind(this)).delay(300);
        
        this.activate.set('send',{
            onSuccess: function (result) {
                data = JSON.decode(result);
                if (data.result=='failure') {
                    this.activate.getChildren().each(function(item){ item.setStyle('display','none');});
                    el = new Element('div');
                    el.addClass('errors').set('html','<br><br>'+data.message).setStyles({'height':100,'margin-bottom':0}).inject(this.activate, 'top').fade(0,1);
                    this.activate.setStyle('background','none');
                    (function(){
                        this.activate.getFirst('div').fade('out');
                        (function(){
                            this.activate.getFirst('div').destroy();
                            this.activate.getChildren().each(function(item){ item.setStyle('display','block').fade('in');});
                        }.bind(this)).delay(300)
                    }.bind(this)).delay(5000);
                } else if (data.result=='success') {
                    this.activate.getChildren().each(function(item){ item.setStyle('display','none');});
                    el = new Element('div');
                    el.addClass('notice').set('html','<br><br>'+data.message).setStyles({'height':100,'margin-bottom':0}).inject(this.activate, 'top').fade(0,1);
                    this.activate.setStyle('background','none');
                    (function(){
                        this.activate.getFirst('div').fade('out');
                        this.slider.tween('top',0);
                        (function(){
                            this.activate.getFirst('div').destroy();
                            this.activate.getChildren().each(function(item){ item.setStyles({'display':'block','opacity':1});});
                        }.bind(this)).delay(300)
                    }.bind(this)).delay(5000);
                }
            }.bind(this)
        });
        (function(){this.activate.send();}.bind(this)).delay(400);
    },
    
    handle_request_password: function () {
        this.forgot_password.getChildren().each(function(item){ item.fade('out'); });
        (function(){
            this.forgot_password.setStyle('background','url(/media/img/loader.gif) no-repeat center')
        }.bind(this)).delay(300);
        
        this.forgot_password.set('send',{
            onSuccess: function (result) {
                data = JSON.decode(result);
                if (data.result=='failure') {
                    this.forgot_password.getChildren().each(function(item){ item.setStyle('display','none');});
                    el = new Element('div');
                    el.addClass('errors').set('html','<br><br>'+data.message).setStyles({'height':100,'margin-bottom':0}).inject(this.forgot_password, 'top').fade(0,1);
                    this.forgot_password.setStyle('background','none');
                    (function(){
                        this.forgot_password.getFirst('div').fade('out');
                        (function(){
                            this.forgot_password.getFirst('div').destroy();
                            this.forgot_password.getChildren().each(function(item){ item.setStyle('display','block').fade('in');});
                        }.bind(this)).delay(300)
                    }.bind(this)).delay(5000);
                } else if (data.result=='success') {
                    this.forgot_password.getChildren().each(function(item){ item.setStyle('display','none');});
                    el = new Element('div');
                    el.addClass('notice').set('html','<br><br>'+data.message).setStyles({'height':100,'margin-bottom':0}).inject(this.forgot_password, 'top').fade(0,1);
                    this.forgot_password.setStyle('background','none');
                    (function(){
                        this.forgot_password.getFirst('div').fade('out');
                        this.toggle_forgot_password(true);
                        (function(){
                            this.forgot_password.getFirst('div').destroy();
                            this.forgot_password.getChildren().each(function(item){ item.setStyles({'display':'block','opacity':1});});
                        }.bind(this)).delay(300)
                    }.bind(this)).delay(5000);
                }
            }.bind(this)
        });
        (function(){this.forgot_password.send();}.bind(this)).delay(400);
    },

    register_events: function () {
        this.activate.getElement('input[type=submit]').addEvent('click',function(e){
            e.stop();
            email = this.activate.getElement('input[name=email]');
            customer_number = this.activate.getElement('input[name=customer_number]');
            if (email.get('value')=='' || !email.get('value').test(/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i)) email.highlight('#ffe2e2');
            if (customer_number.get('value')=='') customer_number.highlight('#ffe2e2');
            if (customer_number.get('value')!='' && email.get('value')!='' && email.get('value').test(/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i)) this.handle_activation(email, customer_number);
        }.bind(this));
        this.forgot_password.getElement('input[type=submit]').addEvent('click',function(e){
            e.stop();
            input = this.forgot_password.getElement('input[name=email]');
            if (input.get('value')=='' || !input.get('value').test(/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i)) input.highlight('#ffe2e2');
            else this.handle_request_password();
        }.bind(this));
        $('forgot_password').addEvent('click',function(e){
            e.stop();
            this.toggle_forgot_password(true);
        }.bind(this));
        $('cancel_send_password').addEvent('click',function(e){
            e.stop();
            this.toggle_forgot_password(true);
        }.bind(this));
        $('activate').addEvent('click',function(e){
            e.stop();
            if (this.slider.getStyle('top').toInt()==this.slide_to)
                this.activate.highlight('#ffe2e2');
            else
                this.slider.tween('top',this.slide_to);
        }.bind(this));
        $('back_to_signin').addEvent('click',function(e){
            e.stop();
            this.toggle_forgot_password(false);
            this.slider.tween('top',0);
        }.bind(this));
    },
    
    toggle_forgot_password: function(fade) {
        if (this.showing_forgot_password && !fade) {
            this.forgot_password.setStyles({display:'none',opacity:1});
            this.showing_forgot_password = false;
            this.signin_main.setStyles({display:'block',opacity:1});
        } else if (this.showing_forgot_password && fade) {
            this.forgot_password.fade('out');
            this.signin_main.setStyles({display:'block',opacity:0});
            (function(){ 
                this.forgot_password.setStyle('display','none')
                this.signin_main.fade('in');
                this.showing_forgot_password = false;
            }.bind(this)).delay(400);
        } else if (!this.showing_forgot_password && fade) {
            this.signin_main.fade('out');
            this.forgot_password.setStyles({display:'block',opacity:0});
            (function(){ 
                this.signin_main.setStyle('display','none')
                this.forgot_password.fade('in');
                this.showing_forgot_password = true;
            }.bind(this)).delay(400);
        }
    }
});

