AUI().add(
    'short-messages',
    function(A) {
        Liferay.N4Biz = {};
        Liferay.N4Biz.ShortMessages = A.Component.create({
            _popup: null,
            _confirmpopup: null,

            _getFormPopup: function(contentContainerId, formId, title, sendCaption, closeCaption) {
                var instance = this;
                var content = A.one('#'+contentContainerId).html();
                if (instance._isFormPopupInitialized()) {
                    if (instance._popup.get("visible")) {
                        //simply return visible dialog
                        return instance._popup;
                    } else if (instance._popup.contentContainerId != contentContainerId) {
                        //content ids don't match - drop current dialog
                        instance._popup.destroy();
                        instance._popup = null;
                    }
                }

                if (!instance._popup) {
                    instance._popup = new A.Dialog({
                        title: title,
                        bodyContent: content,
                        modal: true,
                        centered: true,
                        draggable: true,
                        resizable: {
                        },
                        width: 400,
                        zIndex:1000,
                        buttons: [
                            {
                                text: sendCaption,
                                handler: function() {
                                    var form = A.one('#'+formId);
                                    submitForm(form);
                                }
                            },
                            {
                                text: closeCaption,
                                handler: function() {
                                    this.close();
                                }
                            }
                        ]
                    }).render();
                    instance._popup.contentContainerId = contentContainerId;
                }
                return instance._popup;
            },

            _isFormPopupInitialized: function() {
                var instance = this;
                return !(typeof instance._popup == "undefined" || instance._popup == null)
            },

            _isFormPopupVisible: function() {
                var instance = this;
                if (instance._isFormPopupInitialized()) {
                    return instance._popup.get("visible");
                } else {
                    return false;
                }
            },

            showFormPopup: function(event) {
                var instance = this;
                instance._getFormPopup(event.contentContainerId, event.formId, event.title, event.sendCaption, event.closeCaption).show();
            },

            _getConfirmPopup: function(contentContainerId, title, closeCaption) {
                var instance = this;
                var content = A.one('#'+contentContainerId).html();
                if (!instance._confirmpopup) {
                    instance._confirmpopup = new A.Dialog({
                        title: title,
                        bodyContent: content,
                        modal: true,
                        centered: true,
                        draggable: true,
                        resizable: {
                        },
                        width: 400,
                        zIndex:1000,
                        buttons: [
                            {
                                text: closeCaption,
                                handler: function() {
                                    this.close();
                                }
                            }
                        ]
                    }).render();
                }
                return instance._confirmpopup;
            },

            showConfirmPopup: function(event) {
                var instance = this;
                instance._getConfirmPopup(event.contentContainerId, event.title, event.closeCaption).show();
            }
        });
    },
    '',
    {
        requires: ['aui-dialog']
    }
);

