﻿///弹出框
$.dialogIndex = 0;
$.dialogCount = 0;
$.dialog = function(params, divid)
{
    var o = { title: "系统提示", auto: false, w: 300, h: 160, maxw: 800, maxh: 500, minw: 300, minh: 160, bg: true, content: "&nbsp;", cancelbut: true, but: {}, close: function() { } };
    $.extend(o, params);
    var hw = $(document).width();
    var hh = $(document).height();
    o.w = o.w < 300 ? 300 : o.w;
    o.h = o.h < 160 ? 160 : o.h;
    o.minw = o.minw < 300 ? 300 : o.minw;
    o.minh = o.minh < 160 ? 160 : o.minh;
    if (o.auto) {
        o.w = hw;
        o.h = hh;
        o.maxw = hw;
        o.maxh = hh;
    } else {
        o.w = o.w > hw ? hw : o.w;
        o.h = o.h > o.h ? o.h : o.h;
        o.maxw = o.maxw > hw ? hw : o.maxw;
        o.maxh = o.maxh > hh ? hh : o.maxh;
    }
    o.content = o.content == "" ? "&nbsp;" : o.content;
    var dialogbg;
    var dialogp;
    var dialog;
    function dialogDiv()
    {
        var bo = false;
        if (divid != null && $("#" + divid) != null) {
            dialog = $("#" + divid);
            var aspx = dialog.data("aspx");
            if (aspx != null && aspx == true) {
                var di = dialog.data("dialogIndex");
                dialogbg = $("#dialog_bg" + di);
                dialogp = $("#dialog_p" + di);
                dialogp.css("display", "block");
                dialogbg.css("display", "block");
            }
            else {
                dialogbg = $("<div></div>");
                dialogp = $("<div></div>");
                $("body").append(dialogbg);
                $("body").append(dialogp);
                dialog.data("aspx", true);
                dialog.data("aspxbut", false);
                bo = true;
            }
            dialog.css("display", "block");
        } else {
            dialogbg = $("<div></div>");
            dialogp = $("<div></div>");
            dialog = $("<div><div class='cctoptxt'></div></div>");
            $("body").append(dialogbg);
            $("body").append(dialogp);
            $("body").append(dialog);
            dialog.attr("id", 'dialog_' + $.dialogIndex);
            bo = true;
        }
        if (bo) {
            $.dialogIndex++;
            dialogbg.attr("id", 'dialog_bg' + $.dialogIndex);
            dialogbg.attr("className", o.bg ? "dialogbg" : "dialogbg2");
            dialogbg.css("z-index", 1155 * $.dialogIndex + 50);
            dialogp.attr("id", 'dialog_p' + $.dialogIndex);
            dialogp.css("z-index", (1155 * $.dialogIndex + 100));
            dialogp.css("position", "absolute");
            dialogp.css("left", "0px");
            dialogp.css("top", "0px");
            dialog.attr("className", "dialog");
            dialog.wrapInner("<div class='cctop'><div class='cctoptxt'></div></div>");
            dialog.append("<div class='ccbottom'></div>");
            dialog.wrapInner("<div class='ccenter'></div>");
            dialog.prepend("<div class='border cleft'></div>").append("<div class='border cright'></div>");
            dialog.wrapInner("<div class='center'></div>");
            dialog.prepend("<div class='title'><div class='titleb tleft'></div><div class='tcenter'><div style='position: relative;'><div class='titleb tico'></div><div class='ttext'></div><div class='titleb tclose'></div> </div> </div> <div class='titleb tright'></div> </div>");
            dialog.append("<div class='border bottom'></div>");
            dialog.attr("className", 'dialog');
            dialog.css("z-index", 1155 * $.dialogIndex + 200);
            dialog.data("dialogIndex", $.dialogIndex);
            dialogbg.bgiframe();
            dialogp.bgiframe();
            dialog.bgiframe();
        }
    }
    dialogDiv();
    dialogbg.css("width", hw + "px");
    dialogbg.css("height", hh + "px");
    dialogp.css("width", (hw) + "px");
    dialogp.css("height", (hh) + "px");
    var hst = $(document).scrollTop();
    var hsl = $(document).scrollLeft();
    var left = parseInt(($("html").width() - o.w) / 2) + hsl;
    var top = parseInt(($("html").height() - o.h) / 2) + hst;
    left = (left < 0 ? 5 : left);
    top = (top < 0 ? 5 : top);
    dialog.css("left", left + "px");
    dialog.css("top", top + "px");
    var title = dialog.find("div.title .ttext");
    var close = dialog.find("div.title .tclose");
    var center = dialog.find("div.center");
    var cctoptxt = center.find("div.cctoptxt");
    var ccbottom = center.find("div.ccbottom");
    title.text(o.title);
    var aspx = dialog.data("aspx");
    if (aspx == null && aspx != true) {
        cctoptxt.html(o.content);
    }
    var aspxbut = dialog.data("aspxbut");
    if (aspxbut == null || aspxbut == false) {
        if (aspxbut == false) {
            dialog.data("aspxbut", true);
        }
        $.each(o.but, function(name, e)
        {
            var cbo = name.indexOf("__") >= 0 ? true : false;
            if (cbo) {
                name = name.replace("__", "");
            }
            ccbottom.append("&nbsp;<input type='button' class='ccbutton' value='" + name + "'/>");
            if (typeof e == "String" && e == "close") {
                e = dclose;
            }
            var but = ccbottom.find("input[type='button']:last-child");
            but.click(e);
            if (cbo) {
                but.bind("click", dclose);
            }
        });
        if (o.cancelbut) {
            ccbottom.append("&nbsp;<input type='button' class='ccbutton' value='取消'/>");
            ccbottom.find("input[type=button]:last-child").click(dclose);
        }
    }
    close.mouseover(function()
    {
        $(this).addClass("tclosemove");
    }).mouseout(function()
    {
        $(this).removeClass("tclosemove");
    }).click(function()
    {
        dclose();
    }).attr("title", "关闭");
    function dclose()
    {
        o.close();
        var aspx = dialog.data("aspx");
        if (aspx != null && aspx == true) {
            dialog.css("display", "none");
            dialogp.css("display", "none");
            dialogbg.css("display", "none");
        } else {
            dialog.remove();
            dialogp.remove();
            dialogbg.remove();
            $.dialogCount--;
        }
    }
    function dsize()
    {
        var heigth = dialog.height();
        var width = dialog.width();
        title.css("width", (width - 28) + "px");
        dialog.find("div.title .tright").css("left", (width - 4) + "px");
        dialog.find("div.title .tclose").css("left", (width - 25) + "px");
        dialog.find("div.title .tcenter").css("width", (width - 8) + "px");
        var center = dialog.find("div.center");
        var ch = heigth - 28 - (($.browser.msie && $.browser.version < 7.0) ? 30 : 0);
        center.css("height", ch + "px").css("width", (width - 2) + "px");
        center.find("div.cleft").css("height", (heigth - 28) + "px");
        center.find("div.ccenter").css("height", (heigth - 28) + "px").css("width", (width - 2) + "px");
        center.find("div.cright").css("height", (heigth - 28) + "px").css("left", (width - 1) + "px");
        center.find("div.cctoptxt").css("height", (heigth - 68) + "px");
        dialog.find("div.bottom").css("width", (width) + "px");
    }
    if (($.browser.mozilla || ($.browser.msie && $.browser.version >= 7.0)) && hw > o.w && hh > o.h) {
        dialog.draggable({ containment: dialogp, handle: title, scroll: true });
    }
    if ($.browser.mozilla || ($.browser.msie && $.browser.version >= 7.0)) {
        // dialog.resizable({ maxHeight: 800, maxWidth: o.maxw, minHeight: 800, minWidth: o.minw });
    }
    dialog.width(o.w);
    dialog.height(o.h);
    dsize();
    if ($.browser.mozilla || ($.browser.msie && $.browser.version >= 7.0)) {
        dialog.resize(function() { dsize(); });
    }
    $.dialogCount++;
    return dialog;
}
//关闭窗口
$.fn.closeForm = function()
{
    var dialogIndex = $(this).data("dialogIndex");
    var aspx = $(this).data("aspx");
    if (aspx != null && aspx == true) {
        $(this).css("display", "none");
        $("#dialog_bg" + dialogIndex).css("display", "none");
        $("#dialog_p" + dialogIndex).css("display", "none");
    } else {
        $(this).remove();
        $("#dialog_bg" + dialogIndex).remove();
        $("#dialog_p" + dialogIndex).remove();
        $.dialogCount--;
    }
}
//获取窗口内容
$.fn.content = function()
{
    return $(this).find("div.ccenter").find("div.cctoptxt");
}
//警告框
$.alert = function(msg, img)
{
    $.confirm({ msg: msg, img: img, cancelbut: false });
}
//确认框
$.confirm = function(params)
{
    var o = {
        msg: "提示",
        img: 0,
        bg: true,
        cancelbut: true,
        ok: "",
        no: "",
        close: function() { }
    };
    $.extend(o, params);
    var eimg = "warning";
    var but = { 确定__: o.ok };
    if (o.ok != "" && o.no != "") {
        but = { 是__: o.ok, 否__: o.no }
        if (o.img == 0)
            o.img = 2;
    }
    if (o.img == 0)
        o.img = 1;
    switch (o.img) {
        case 2:
            eimg = "info";
            break;
        case 3:
            eimg = "question";
            break;
        case 4:
            eimg = "error";
            break;
    }
    var content = "<div style='margin:10px 20px 10px 20px;'>";
    content += "<div style='width: 32px; height: 32px; float: left;margin-top: 10px;' class='" + eimg + "'></div>";
    content += "<div style='margin-left: 40px;vertical-align:middle;'>" + o.msg + "</div></div>";
    $.dialog({ bg: o.bg, content: content, cancelbut: o.cancelbut, but: but, close: o.close });
}
//窗口
$.form = function(params, divid)
{
    var o = {
        title: "新窗口",
        bg: true,
        auto: false,
        w: 520,
        h: 320,
        url: "",
        content: "",
        cancelbut: true,
        but: {},
        close: function() { }
    };
    $.extend(o, params);

    if (o.url != "") {
        o.content = "<iframe src='" + o.url + "' style='width: 100%; height: 100%' frameborder='0' scrolling='yes'></iframe>";
    }
    return $.dialog({ title: o.title, bg: o.bg, auto: o.auto, w: o.w, h: o.h, maxw: o.mw, maxh: o.mh, content: o.content, cancelbut: o.cancelbut, but: o.but, close: o.close }, divid);
}