var showLoginDialog = function() {

    var dialog = null;
    var afterLoginFnWrap = null;

    var initDialog = function() {
        MK.append(document.body, 'div', {
                'id': 'login_dialog',
                'style': {
                    'visibility': 'hidden'
                },
                'content': // '<div class="hd">모임 참석</div>' +
                '<div class="hd"></div>' +
                '<div class="bd">' +
                    '<form action="/login/" method="POST" id="login">' +
                    '<h2>로그인</h2>' +
                    '<p><label for="id_login-email">이메일 주소</label><br /><input type="text" name="login-email" id="id_login-email" /></p>' +
                    '<p><label for="id_login-password">비밀번호</label><br /><input type="password" name="login-password" id="id_login-password" /></p>' +
                    '<input type="submit" value="로그인">' +
                    '<input type="hidden" name="login_type" value="login" />' +
                    '</form>  ' +
                    '<p id="or">or</p>' +
                    '<h2>처음 오신 분</h2>' +
                    '<form action="/login/" method="POST" id="signup">' +
                    '<p><label for="id_signup-email">이메일 주소</label><br /><input type="text" name="signup-email" id="id_signup-email" /></p>' +
                    '<p><label for="id_signup-name">이름</label><br /><input id="id_signup-name" type="text" name="signup-name" maxlength="30" /></p>' +
                    '<p><label for="id_signup-password">비밀번호</label><br /><input type="password" name="signup-password" id="id_signup-password" /></p>' +
                    '<input type="submit" value="가입완료">' +
                    '<input type="hidden" name="login_type" value="signup" />' +
                    '<input type="hidden" name="next" value="" />' +
                    '</form>' +
                    '</div>'
            });

        dialog = new YAHOO.widget.Dialog("login_dialog", {
                //'width': "400px",
                'fixedcenter': true,
                'visible': false, 
                'constraintoviewport': true,
                'draggable': false,
                'postmethod': "manual",
                'modal': true
                // 'effect': {effect:YAHOO.widget.ContainerEffect.FADE,duration:0.1},
            } );
        dialog.validate = function() { return false; };
        dialog.beforeShowEvent.subscribe(function() {
                dialog.center();
            });

        var kl = new YAHOO.util.KeyListener(document, { keys:27 }, {
                fn: dialog.cancel, 
                scope: dialog, 
                correctScope: true 
            } ); 

        dialog.cfg.queueProperty("keylisteners", kl); 


        // Render the Dialog
        dialog.render();

        // XXX: for YUI bug
        dialog.defaultHtmlButton = {
            click: function() {}
        };


        MK.hijackForm({
                formEl: _QL('#login_dialog form'),
                callback: function(result, data) {
                    if (!result) {
                        return;
                    }

                    dialog.hide();
                    afterLoginFnWrap();
                },
                urlFn: MK.hijackForm.jsonFormat
            });
    };

    var setInfo = function(info) {
        MK.set(_Q("#login_dialog .hd"), {'content': info});
    };

    return function(afterLoginFn, info) {
        afterLoginFnWrap = afterLoginFn;

        if (!dialog) {
            initDialog();
        }

        setInfo(info);

        dialog.show();
    };
}();

MK.config.loginFn = showLoginDialog;


Event.onDOMReady(function() {
        Event.on(_QL('a[href=/login/]'), 'click', function(ev) {
                Event.stopEvent(ev);

                showLoginDialog(function() {
                        window.location.pathname = window.location.pathname;
                    }, "로그인");
            });
    });

function showSimpleDialog(title, body, okButton, cancelButton, okFn, preOkFn) {
    var simpleDlg = new YAHOO.widget.SimpleDialog("simple-dlg", {
            width: "20em",
            fixedcenter: true,
            visible: false,
            draggable: false,
            close: true,
            constraintoviewport: true,
            modal: true,
            buttons: [{text:okButton, handler: function() {if (!preOkFn || preOkFn && preOkFn()) {simpleDlg.hide(); simpleDlg.destroy(); okFn(); }}, isDefault:true},
            {text:cancelButton,  handler: function() {simpleDlg.hide(); simpleDlg.destroy(); }}]
        } );
    var kl = new YAHOO.util.KeyListener(document, { keys:27 }, {
            fn: function() {
                simpleDlg.hide();
                simpleDlg.destroy();
        }}); 
    simpleDlg.cfg.queueProperty("keylisteners", kl); 
    simpleDlg.setHeader(title);
    simpleDlg.setBody(body);
    simpleDlg.render(document.body);

    simpleDlg.show();
}

