var cWindow_UserLogin = {

    init: function()
    {
        $(this.dom_button_send).show();
        $(this.dom_button_cancel).show();

        for (var i in this.info) {
            this['dom_' + i].val('');
            this.changeNotice(i, 'info', this.info[i]);
        }

        if (this.outside_error_code) {
            var error = this.errors[this.outside_error_code];
            this.changeNotice(error.field, 'error', error.text);
            this.outside_error_code = undefined;
        }
    },

    setEventListener: function(listener)
    {
        if (!this.listeners_collection) {
            this.listeners_collection = new ListenersCollection();
        }
        this.listeners_collection.addListener(listener);
    },

    eventButtonCancelClick: function(e)
    {
        e.preventDefault();
        this.listeners_collection.notify('cancelSend', null);
    },

    eventButtonSendClick: function(e)
    {
        e.preventDefault();

        try {
            var r = Request.create();
            if (r !== null) {
                this.listeners_collection.notify('startSend', null);
                r.send(URL_AJAX_HANDLER, {'rm': 'user_login',
                                          '__login':  this.dom_user_login.val(),
                                          '__password': this.dom_user_password.val()}, this);
                delete(r);
            }
        }
        catch (e) {
            this.handleJSException(e);
        }
    },

    requestCallback: function(result, request_params, exception)
    {
        if (request_params.rm != 'user_login') {
            this.m = cWindow.requestCallback;
            return this.m(result, request_params);
        }

        if (exception) {
            this.finishSend();
            alert('Server error. Try again');
            result.exception.handled = true;
        } else {

            if (!result.successful) {
                this.finishSend();

                this.dom_user_login.val('');
                this.dom_user_password.val('');

                var error = this.errors[result.error_code];
                this.changeNotice(error.field, 'error', error.text);

            } else {
                this.finishSend();
                window.location.replace(result.url);
            }
        }
    },

    finishSend: function()
    {
        this.listeners_collection.notify('doneSend', null)
        $(this.dom_button_send).show();
        $(this.dom_button_cancel).show();
    },

    changeNotice: function(field, mode, text) {
        this['dom_' + field + '_notice']
            .removeClass('positive')
            .removeClass('error')
            .removeClass('info')
            .addClass(mode).html(text);
    }
}

function Window_UserLogin(data)
{
    this.pc = Window;
    this.pc(data);

    for (var f in cWindow_UserLogin) {
        this[f] = cWindow_UserLogin[f];
    }

    this.dom_button_send = _(this.name + '_button_send');
    this.dom_button_cancel = _(this.name + '_button_cancel');

    var _this = this;

    jEvent.addEvent(this.dom_button_send, 'click', function(e) {_this.eventButtonSendClick(e)}, true);
    jEvent.addEvent(this.dom_button_cancel, 'click', function(e) {_this.eventButtonCancelClick(e)}, true);

    this.errors = {
        'USER_LOGIN_EMPTY': {'field': 'user_login', 'text': 'Can not be empty'},
        'USER_PASSWORD_INVALID': {'field': 'user_password', 'text': 'Password is not valid'},
        'USER_NOT_FOUND': {'field': 'user_login', 'text': 'User not found'},
        'USER_NEED_CONFIRMATION': {'field': 'user_login', 'text': 'Need confirmation, check your email'}
    };

    this.info = {
        'user_login': 'Enter your username',
        'user_password': 'Enter your password'
    };

    for (var i in this.info) {
        this['dom_' + i + '_notice'] = $(_(this.name + '_' + i + '_notice'));
        this['dom_' + i] = $(_(this.name + '_' + i))
            .val('')
            .click(function(field) {
               return function() {
                    if (!_this['dom_' + field + '_notice'].hasClass('positive')) {
                        _this.changeNotice(field, 'info', _this.info[field]);
                    }
               }
            }(i));
    }

    this.outside_error_code = data.error_code || undefined;
}

