/*
*功能：自动定义弹出窗口
*参数：
*      fnCallBakMsgBox ：回调函数strBtn, strMsg, strMsgLevel, strTitle
*      strBtn      ：按钮类型 
*                  Ok          : 确定,
*                  YesNo       : 是，否,
*                  YesNoCancel ：是，否，取消
*      strMsg      ：弹出窗口，显示的消息的内容
*      strMsgLevel ：消息的级别
*                  Info:提示信息,
*                  Warn: 警告信息,
*                  Err :错误信息
*      strTitle    ：弹出窗口的标题
**/
function cihoAlert(strMsg, strBtn, fnCallBakMsgBox, strMsgLevel, strTitle) {
    if (!strMsg) {
        strMsg = "系统错误！";
    }
    //设定弹出message窗口的标题
    if (!strTitle) {
        if (strMsgLevel) {
            switch (strMsgLevel) {
                case msgBox.msgLevel.Info:
                    strTitle = "提示！";
                    break;
                case msgBox.msgLevel.Warn:
                    strTitle = "警告！";
                    break;
                case msgBox.msgLevel.Err:
                    strTitle = "错误！";
                    break;
            }
        }
        else {
            strTitle = "提示！";
        }
    }

    //创建MsgBox
    createDlgBox(strMsg, strBtn, fnCallBakMsgBox, strMsgLevel, strTitle);
    strBtn = null;
    strMsg = null;
    strTitle = null;

    //重置dialog的遮罩层的高度
    $('.ui-widget-overlay').height(window.document.documentElement.clientHeight);
}

function createDlgBox(strMsg, strBtn, fnCallBakMsgBox, strMsgLevel, strTitle) {
    var dlgId = "divMsgBox";

    //创建弹出窗（添加/修改）
    createMsgDlg(dlgId, window.parent.document.documentElement.clientHeight, "100%", true, "", false, false);

    //整个模板窗口的内容
    var strPop = "";
    strPop += "<div id=\"" + dlgId + "_pop_top\" class=\"pop\" style=\"width:350px;padding:0px;\">";
    strPop += "    <div class=\"pop_x\"><a id=\"idClose\" href=\"#\"></a></div>";
    strPop += "    <div class=\"pop_top\">";
    strPop += "        <h2>" + strTitle + "</h2>";
    strPop += "    </div>";

    strPop += "     <div class=\"pop_box\">";
    //START OF CONTENT
    strPop += "         <div class=\"pop_disp\">";
    strPop += "<br>";
    strPop += "             <div style=\"margin:0px auto; text-align:center;width:260px;font-size:14px;padding:20px 0px;\">" + strMsg + "</div>";
    strPop += "<br>";
    strPop += "         </div>";
    //END OF CONTENT

    strPop += "         <div class=\"clr\"></div>";
    strPop += "         <div class=\"box_btn_width\">";
    //START OF BUTTON
    strPop += "             <div class=\"box_btn\">";
    if (strBtn == msgBox.dlgBtn.YesNo) {
        strPop += "                         <div class=\"btn02\"><a id=\"idYes\" href=\"#\">是</a></div>";
        strPop += "                         <div class=\"btn03\"><a id=\"idNo\" href=\"#\">否</a></div>";
    }
    else if (strBtn == msgBox.dlgBtn.YesNoCancel) {
        strPop += "                         <div class=\"btn04\"><a id=\"idYes\" href=\"#\">是</a></div>";
        strPop += "                         <div class=\"btn04\"><a id=\"idNo\" href=\"#\">否</a></div>";
        strPop += "                         <div class=\"btn04\"><a id=\"idCancel\" href=\"#\")\">取消</a></div>";
    }
    else {
        strPop += "                         <div class=\"btn01\"><a id=\"idOk\" href=\"#\">确定</a></div>";
    }
    strPop += "               </div>";
    //END OF BUTTON

    strPop += "            </div>";
    strPop += "        </div>";
    strPop += "    </div>";

    $("#" + dlgId).append(strPop);

    $("#idYes").click(function () {
        closeMsgBox(dlgId);
        if (fnCallBakMsgBox) {
            fnCallBakMsgBox(msgBox.dlgRst.Yes);
        }
        return false;
    });
    $("#idNo").click(function () {
        closeMsgBox(dlgId);
        if (fnCallBakMsgBox) {
            fnCallBakMsgBox(msgBox.dlgRst.No);
        }
        return false;
    });
    $("#idOk").click(function () {
        closeMsgBox(dlgId);
        if (fnCallBakMsgBox) {
            fnCallBakMsgBox(msgBox.dlgRst.Ok);
        }
        return false;
    });
    $("#idCancel").click(function () {
        closeMsgBox(dlgId);
        if (fnCallBakMsgBox) {
            fnCallBakMsgBox(msgBox.dlgRst.Cancel);
        }
        return false;
    });
    $("#idClose").click(function () {
        closeMsgBox(dlgId);
        if (fnCallBakMsgBox) {
            fnCallBakMsgBox(msgBox.dlgRst.No);
        }
        return false;
    });

    //增加窗口的拖动功能
    //$("#" + dlgId + "_pop_top").parent().parent().draggable({ "scroll": false });
    $("#" + dlgId).dialog("option", "width", "350");

    //绑定事件,响应回车。
    if (strBtn == msgBox.dlgBtn.YesNo || strBtn == msgBox.dlgBtn.YesNoCancel) {
        bingEvents(dlgId, "idYes");
    }
    else {
        bingEvents(dlgId, "idOk");
    }
}
//绑定事件
function bingEvents(dlgId, ctlId) {
    //回车相应关闭事件
    $("#" + dlgId).focus().unbind('keydown').bind('keydown', function (event) {
        if (event.keyCode == 13 || event.keyCode == 32) {
            $('#' + ctlId + "," + dlgId).click();
        }
        return false;
    });
}
//关闭MessageBox窗口
function closeMsgBox(dlgId) {
    //$("#" + dlgId).dialog("close");
    cihoAlertDlgClose(dlgId);
}

////创建MessageBox窗口所属的dialog
//function createMsgDlg(dlgId, iHeight, iWidth, bModal, strTitle, bResizable, bcloseOnEscape) {
//    $("#" + dlgId).dialog({
//        draggable: false,
//        autoOpen: true,
//        //height: iHeight,
//        //width: iWidth,
//        modal: bModal,
//        stack: true,
//        //title: strTitle,
//        zIndex: 10000000,
//        dialogClass: "panel window",
//        //resizable: bResizable,
//        resizable: true,
//        closeOnEscape: bcloseOnEscape,
//        close: function () {
//            //清空层中的内容
//            $("#" + dlgId).empty();
//            //window.parent.closeShieldDiv(dlgId);
//        }
//    });

//    //清空层中的内容
//    $("#" + dlgId).empty();

//    //清除dialog原有的标题栏样式
//    $(".ui-dialog-titlebar").remove();
//    $("#" + dlgId).css("margin", "0px").css("padding", "0px");
//}

//关闭MessageBox弹出层，清空层的内容
function cihoAlertDlgClose(dlgId) {
    //删除窗体的遮罩层
    $("#body_" + dlgId).remove();

    $('#' + dlgId + "_content").remove();
    $('#' + dlgId).empty();
    $('#' + dlgId).hide();
    $('#' + dlgId)[0].innerHTML = "";
}
//创建MessageBox弹出层（窗口）
function createMsgDlg(dlgId, iHeight, iWidth, bModal, strTitle, bResizable, bcloseOnEscape) {
    //创建弹出窗口的遮罩层
    $("body").append("<div id='body_" + dlgId + "' class='menu_dlg_shield'></div>");
    $("#body_" + dlgId).css("display", "block");
    $("#body_" + dlgId).height(window.parent.document.documentElement.clientHeight);

    $("#" + dlgId).addClass("menu_dlg_bg");
    $("#" + dlgId).height(window.parent.document.documentElement.clientHeight);
    $("#" + dlgId).show()
    $('#' + dlgId).append('<div id="' + dlgId + '_content" class="menu_dlg_frm"></div>');
    $('#' + dlgId + '_content').css("margin-top", "200px");
}