var loginBox = new Class({
    initialize: function(options){
        this.options = null;
        this.options = {
            overlay     : new Overlay({container: document.body}),
            status      : {
                initialText     : "Logging on...",
                secondText      : "Connection in progress, please wait...",
                //thirdText       : "Connected, we are going through ...",
                finalText       : "Cannot establish connection. Please contact technical support.",
                fatalText       : "Connection error, please try logging in again.",
                logoutText      : "Logging out...",
                disabledText    : "Your login has been disabled, please contact support center.",
                incorrectText   : "The username or password you entered is incorrect.",
                connectedText   : "Connected, please wait...",
                technicalText   : "Sorry...  we're experiencing technical difficulties.",
                badTLevelText   : "Login setup problem, please contact the support center.",
                heavyLoadText   : "We are experiencing unexpected high levels of website acitvity. Your session may run slowly. Click 'Continue' to login or 'Close' and try again later.",
                criticalLoadText: "We are experiencing unexpected critical levels of website acitvity. You cannot be logged in at this time, please try again later."
            },
            url             : 'modules.php',
            login           : $('loginSubmit'), 
            loginBox        : $("loginBox"),
            inputs          : { close : $("close"), cancel : $("cancel"), continueInput : $('continue') },
            text            : $('text'), 
            checker         : false,
            logout          : false,
            ajax            : false,
            isheavy         : false,
            iscritical      : false
        },
        $extend(this.options, options);
        this.setupAjax();
        this.addButtonEvents();
    },

    setupAjax : function(){    
        this.options.ajax = new Ajax(this.options.url, {
            method: 'post',
            login: this, 
            onRequest:  function() { 
                this.options.login.options.loginBox.setStyle('display', 'block');
            },
            onSuccess:  function(r) {
                switch(r){
                    case "1": 
                        this.options.login.showError(this.options.login.options.status.disabledText);
                        break;
                    case "5":
                        this.options.login.showError(this.options.login.options.status.badTLevelText);
                        break;
                    case "4": 
                        this.options.login.options.text.setHTML(this.options.login.options.status.connectedText);
                        window.location = this.url; 
                    break;
                    case "0": 
                        this.options.login.options.inputs.close.value = "Try again";
                        this.options.login.showError(this.options.login.options.status.incorrectText);
                    break;
                    default:
                        this.options.login.options.inputs.close.value = "Try again";
                        this.options.login.showError(this.options.login.options.status.technicalText)
                } 
            },
            onFailure:      function() { 
                this.options.login.showError(this.options.login.options.status.fatalText)
            },
            onCancel:       function() {}
        });
    },

    showError : function (message){
        this.options.loginBox.setStyle('display', 'block');
        this.options.text.setHTML(message).addClass('error');
        this.options.inputs.close.setStyle('display', '').focus();
    }, 

    addButtonEvents : function () {
        if( this.options.inputs.continueInput )
            this.options.inputs.continueInput.addEvent('click', function(e){
                new Event(e).stop();
                this.fireAjax();
            }.bindWithEvent(this));
        
        if( this.options.inputs.close )
            this.options.inputs.close.addEvent('click', function(e){
                new Event(e).stop();
                this.options.loginBox.setStyle('display', 'none');
                this.options.ajax.cancel();
                $clear(this.options.checker);
                this.reset();
                this.options.overlay.hide();
                location.reload(true);
            }.bindWithEvent(this));
        
        /*if( this.options.inputs.cancel )
            this.options.inputs.cancel.addEvent('click', function(e){
                new Event(e).stop();
                $clear(this.options.checker);
                this.reset();
                this.options.text.setHTML(this.options.status.logoutText);
                (function(){
                    this.options.loginBox.setStyle('display', 'none');
                    this.options.overlay.hide();
                }).delay(10000, this);
            }.bindWithEvent(this));*/

        if( this.options.login )
            this.options.login.addEvent('click', function(e){
                new Event(e).stop();
                if( this.options.iscritical ) 
                    this.isCriticalLoad();
                else if( this.options.isheavy ) 
                    this.isHeavyLoad();
                else
                    this.fireAjax();
            }.bindWithEvent(this));
    },

    isHeavyLoad : function () {
        this.options.overlay.show();
        this.showError(this.options.status.heavyLoadText ); 
        this.options.inputs.continueInput.setStyle('display', '');
    },

    isCriticalLoad : function(){
        this.options.overlay.show();
        this.showError( this.options.status.criticalLoadText );
    },

    fireAjax : function() {  
        this.reset();    
        if( !this.options.isheavy ) 
            this.options.overlay.show();
        var data = {
            'login[uname]'      : $('login_uname').getValue(),
            'login[pass]'       : $('login_password').getValue(),
            'login[rememberme]' : $('rememberme').checked ?$('rememberme').getValue():'',
            'login[locale]'     : $('locale')? $('locale').getValue():'',
            'op'                : 'modload',
            'name'              : 'NS-User',
            'file'              : 'index'
        }
        this.options.ajax.request(data);
        if( this.options.checker) 
            $clear(this.options.checker);
        this.options.checker = this.check.periodical(1000, this);
    },

    reset : function() {
        this.ctime = 0;
        $each(this.options.inputs,  
            function(input) {
                input.setStyle('display', 'none')
            }
        );
        this.options.inputs.close.value = "Close";
        this.options.text.removeClass('error');
        $clear(this.options.logout);
        this.options.text.setHTML(this.options.status.initialText);
    },

    check : function() {
        if(this.options.ajax.running){
            if( this.options.ajax.isSuccess() == false && this.ctime >= 30) { 
                this.options.text.setHTML(this.options.status.secondText);
            }
            if( this.options.ajax.isSuccess() == false && this.ctime >= 60) {
                this.showError(this.options.status.finalText);
                this.options.ajax.cancel();
            }
            this.ctime++;
        }
    }
});
