/*
 * @require dialog.scss
 * */
define(['utils'],function (utils) {
    var dialogs = {};

    function dialogFactory(dialogConfig, bindShow) {
        // 下面都是默认值
        var defaultConfig = {
            title: null,    // 弹窗的标题，为了用户体验，这个务必设置
            buttons: null,  // button 的设置方式，下面会详细介绍
            mask: true,    // 是否模态
            type: "html", // html/iframe/ajax
            content: "",  // 内容，通常是一段 html 字符串；和上面的 type 有关系；
            width: 480,   // 宽度
            height:215,
            appendTo: document.body,   // 生成的弹窗放置位置，通常这个不用动
            autoShow: true     // 初始化弹窗完成后，是否自动显示
        };

        dialogConfig = $.extend(true, defaultConfig, dialogConfig);
        var dialog = new SQ.Dialog(dialogConfig);
        dialog.bind("hide", function () {
            dialog.destroy();
        });
        dialog.bind("show", function () {
            var buttons = $('.sq-dialog-buttons a', dialog.el);
            if (buttons.size() === 2) {
                buttons.eq(1).addClass('btn2');
            }
            dialog.el.css({
                "z-index":999
            });
            bindShow && bindShow(dialog.el);
        });
        dialog.show();
        return dialog;
    };

    SQ.sAlert = function (content) {
        var alert = new SQ.Dialog({
            title: '温馨提示',
            content: content,
            width: 269
        });
        alert.bind('show', function () {
            $('.sq-dialog-body', alert.el).css({
                "border-color": "#960D09",
                "border-width": "1px",
                "border-style": "solid",
                "background": "url("+__uri('bg.png')+")"
            });
            $('.sq-dialog-titlebar', alert.el).css({
                "background-position": "-128px 0"
            });
        });
        alert.show();
    };

    SQ.alert = (utils.isIE&&(+utils.ieVersion < 8))?window.alert:function (content) {
        content = '<div class="dialog-text-wrapper"><div class="dialog-text-vertical-middle">' + content + '</div></div>';

        dialogs.alert = new SQ.Dialog({
            content: content,
            width: 370,
            height:145
        });
        dialogs.alert.bind('show', function () {
            $('.sq-dialog-body', dialogs.alert.el).css({
                "background": "url("+__uri('alert.png')+")"
            });
            $('.sq-dialog-titlebar', dialogs.alert.el).css({
                "background-position": "-128px 0"
            });
            dialogs.alert.el.css({
                "z-index":9999
            });
        });
        dialogs.alert.bind("hide",function(){
            dialogs.alert.destroy();
        });
        dialogs.alert.show();
        return dialogs.alert;
    };

    SQ.modal = function (content, confirm, cancel) {
        if (typeof(dialogs.modal) === "undefined") {
            dialogs.modal = new SQ.Dialog({
                title: "信息提示",
                content: content,
                width: 369,
                buttons: {
                    "确认": confirm || $.noop,
                    "取消": cancel || $.noop
                }
            });
        } else {
            dialogs.modal.setContent(content);
            dialogs.modal.show();
        }

        return dialogs.modal;
    };

    return dialogFactory;
});