LoginFormController = new Class({

    initialize : function(formBlockId, buttonId, panelBlockId) {

        this.form_id = formBlockId;
        this.button_id = buttonId;
        this.panel_id = panelBlockId;

        window.addEvent('hashchange', this.hashEvent.bind(this));

        if ($(this.form_id)) {
            this.init_login_form();
        }
    },

    init_login_form : function() {
        $(this.form_id).set('send', {
            method : 'post',
            url : '/users/login',
            onComplete: function(response) { 
                $(this.panel_id).empty();
                $(this.panel_id).set('html', response);
                if ($(this.form_id)) {
                    this.init_login_form();
                }
            }.bind(this),
            evalScripts : true,
        });
        $(this.button_id).addEvent('click', function() {
            $(this.form_id).send();
        });
        $(this.form_id).getElements('input').addEvent('keyup', function(evt) {
            if (evt.key == 'enter') {
                $(this.form_id).send();
            }
        }.bind(this));
    },

    hashEvent : function(hash) {
        if (hash == '#login') {
            $(this.form_id).setStyle('display', 'block');
            window.location.hash = ''
        }
    }
});

window.addEvent('domready', function() {
    lc = new LoginFormController('login_block', 'login_button', 'top_panel');
});



