var FeedbackForm = function() {
    var dialog;

    var eventHandler = function(ev) {
        Event.stopEvent(ev);
        dialog.show();
    };

    var initDialog = function() {
        var handleSubmit = function() {
            MK.ajaxFormSubmit(_Q('#feedback_form'), {
                    callback: function(result, data, formEl) {
                        dialog.hide();
                        hideLink();
                        _Q('#id_feedback_comment').value = '';
                    },
                    urlFn: MK.hijackForm.jsonFormat
                });
        };
        var handleCancel = function() {
            this.hide();
        };

        MK.append(document.body, 'div', {
                'id': 'feedback_dialog',
                'display': 'none',
                'content': '<div class="hd">moim.at에 한마디</div>' +
                    '<div class="bd">' +
                    'moim.at에 대해 자유롭게 써주세요.<br />'+
                    '<form id="feedback_form" action="/feedback/" method="POST">' +
                    '<textarea id="id_feedback_comment" rows="10" name="feedback_comment" style="width:500px"></textarea>'+
                    '</form>'
            });

        dialog = new YAHOO.widget.Dialog("feedback_dialog", {
                'width': "520px",
                'fixedcenter': true,
                'visible': false, 
                'constraintoviewport': true,
                'draggable': false,
                'postmethod': "manual",
                'modal': true,
                'buttons': [ { 'text': "보내기", 'handler': handleSubmit},
                { 'text': "취소", 'handler': handleCancel } ]
            } );
        dialog.beforeShowEvent.subscribe(function() {
                dialog.center();
            });
        dialog.validate = function() { return false; };

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

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


        // Render the Dialog
        dialog.render();
    };

    var initEvent = function() {
        Event.on(_QL('.feedback_link'), 'click', eventHandler, this, true);
        MK.set(_QL('.feedback_link'), {'href': '#'});
    };
    var hideLink = function() {
        var newElement = MK.insertBefore(
            _Q('.feedback_link'), 
            'div', {
                'content': '의견 고맙습니다.'
            });

        var anim = new YAHOO.util.ColorAnim(newElement, {
                'backgroundColor': {
                    'from': '#FF0',
                    'to': '#FAFAFA' 
                }}); 
        anim.onComplete.subscribe(function() {
                var anim2 = new YAHOO.util.Anim(newElement, {}, 2, YAHOO.util.Easing.easeNone);
                anim2.onComplete.subscribe(function() {
                        var anim3 = new YAHOO.util.ColorAnim(newElement, {
                                'color': {'to': '#FAFAFA'},
                                'backgroundColor': {'to': '#FAFAFA'}
                            }); 
                        anim3.onComplete.subscribe(function() {
                                MK.remove(newElement);
                            });

                        anim3.animate();
                    });
                anim2.animate();
            });
        anim.animate();
    };


    return {
        init: function() {
            initDialog();
            initEvent();
        }
    };
}();

Event.onDOMReady(FeedbackForm.init, FeedbackForm, true);

