if (typeof(Application) == 'undefined') {
	Application = Class.create();
}

LoginForm = Class.create();
LoginForm.prototype = {
    initialize: function (options) {
        // default options
        this.options = Object.extend({
            formId: "signupForm",
            theme: "alphacube", //"alphacube" "mac_os_x"
            openEffect: "popup_effect",
            contentId: "loginPanel",
            messageId: "login_error_msg",
            userPanelId: "authorization",
            txtLoginId: "login"
        }, options || {});

        // init event handlers
        this.onLoginSuccessHandler = this.onLogin_Success.bindAsEventListener(this);
        this.onLoginFailedHandler = this.onLogin_Failed.bindAsEventListener(this);
        this.onShowHandler = this.onShow.bindAsEventListener(this);

        // init fields
        this.window = null;
    },

    initWindow: function(elementOwner) {
        var effect = new PopupEffect(elementOwner, {className: this.options.openEffect});
        this.window = new Window(
            {
                className:this.options.theme,
                width: 260, height:null,
                showEffect:effect.show.bind(effect),
                hideEffect:effect.hide.bind(effect),
                minimizable:false,
                maximizable:false
            }
        )
        this.window.getContent().update($(this.options.contentId).innerHTML);
        //Windows.addObserver(this.onShow);
    },

    show: function(senderObj) {
        try {
            if (this.window == null)
                this.initWindow(senderObj);

            var position = Position.cumulativeOffset(senderObj);
            var size = senderObj.getDimensions();
            this.window.setLocation(position[1] + size.height, position[0] - 200);
            this.hideMessage();
            this.enableForm();
            this.window.show();
            window.setTimeout(this.onShowHandler, 600);
        } catch (e) {
            alert(e.message);
        }
    },

    showMessage: function(message) {
        var msgLabel = $(this.options.messageId);
        if (msgLabel) {
            Element.show(msgLabel);
            if (!Prototype.Browser.WebKit)
                msgLabel.update(message);
            else
                alert(message);
            this.window.updateHeight();
        } else {
            alert(message);
        }
    },

    hideMessage: function() {
        var msgLabel = $(this.options.messageId);
        if (msgLabel) {
            msgLabel.update("");
            //msgLabel.hide();
        }
    },

    enableForm: function() {
        var signupForm = $(this.options.formId);
        if (signupForm)
            Form.enable(signupForm);
    },

    disableForm: function() {
        var signupForm = $(this.options.formId);
        if (signupForm)
            Form.disable(signupForm);
    },

    onShow: function () {
        var login = Form.findFirstElement(this.options.formId);
        if (login) {
            login.activate();
        }
        //this.window.updateHeight();
    },

    onLogin_Start: function(eventObj) {
        try {
            this.hideMessage();
            // lock submit event
            if (eventObj.preventDefault)
                Event.stop(eventObj);

            var signupForm = $(this.options.formId);
            var options = {
                parameters:
                    {
                        'isAsyncPostBack': 1
                    },
                onException: this.onLoginFailedHandler,
                onFailure: this.onLoginFailedHandler,
                onSuccess: this.onLoginSuccessHandler
            };
            signupForm.request(options);
            //this.disableForm();
        } catch(e) {
            alert("Error: " + e.message);
            this.enableForm();
        }
        return false;
    },

    // Occure when async responce received
    // {(bool) success, (string) userPanel, (string) error}
    onLogin_Success: function(transport, json) {
        var dataObj;
        this.enableForm();
        try {
            dataObj = transport.responseText.evalJSON();
            if (dataObj.success) {
                this.window.hide();
                $(this.options.userPanelId).update(dataObj.userPanel);
				location.href = dataObj.redirectUrl;
			} else if (dataObj.forum) {
				location.href = dataObj.redirectUrl;
            } else if (dataObj.error) {
                this.showMessage(dataObj.error)
            }
        } catch(e) {
            alert("Error:" + e.message);
        }
    },

    // failed async postback
    onLogin_Failed: function(transport, exObj) {
        this.enableForm();
        this.showMessage(exObj.message)
    }
};