﻿
var fb_dialog = {
    init: function () {

        if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
            $("body", "html").css({ height: "100%", width: "100%" });
            //$("html").css("overflow", "hidden");
            if (document.getElementById("TB_HideSelect") === null) {//iframe to hide select elements in ie6
                $("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div>");
            }
        } else {
            if (document.getElementById("TB_overlay") == null) {
                $("body").append("<div id='TB_overlay'></div>");
            }
        }

        var sbox = '\
                        <table style="display:none" class="pop_dialog_table" id="FB_dialog">\
                        <tbody><tr><td class="pop_topleft"></td><td class="pop_border"></td><td class="pop_topright"></td></tr>\
                        <tr><td class="pop_border"></td>\
                        <td class="pop_content"><h2><span id="FB_dialog_title"></span>\
                        </h2><div class="dialog_content" id="FB_dialog_content">\
                        <span id="FB_dialog_loading" > 正在加载数据...</span>\
                        </div></td>\
                        <td class="pop_border"></td>\
                        </tr><tr><td class="pop_bottomleft"></td>\
                        <td class="pop_border"></td><td class="pop_bottomright"></td></tr>\
                        </tbody></table>';

        $("#TB_overlay").html(sbox);
        this.overlaySize();
        return this;
    },
    overlaySize: function () {
        if (window.innerHeight && window.scrollMaxY) {
            yScroll = window.innerHeight + window.scrollMaxY;
        } else if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
            yScroll = document.body.scrollHeight;
        } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
            yScroll = document.body.offsetHeight;
        }
        $("#TB_overlay").css("height", yScroll + "px");
        return this;
    },
    getPageSize: function () {
        var de = document.documentElement;
        var w = window.innerWidth || self.innerWidth || (de && de.clientWidth) || document.body.clientWidth;
        var h = window.innerHeight || self.innerHeight || (de && de.clientHeight) || document.body.clientHeight;

        arrayPageSize = new Array(w, h)
        return arrayPageSize;
    },
    getPageScrollTop: function () {
        var yScrolltop;
        if (self.pageYOffset) {
            yScrolltop = self.pageYOffset;
        } else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
            yScrolltop = document.documentElement.scrollTop;
        } else if (document.body) {// all other Explorers
            yScrolltop = document.body.scrollTop;
        }
        arrayPageScroll = new Array('', yScrolltop)
        return arrayPageScroll;
    },
    position: function () {
        var pagesize = this.getPageSize();
        var arrayPageScroll = this.getPageScrollTop();
        var h = $("#FB_dialog").height();
        var w = $("#FB_dialog").width();

        //        $("#FB_dialog").css({ marginLeft: '-' + parseInt((w / 2), 10) + 'px', width: w + 'px' });
        //        if (!(jQuery.browser.msie && jQuery.browser.version < 7)) { // take away IE6
        //            $("#FB_dialog").css({ marginTop: '-' + parseInt((h / 2), 10) + 'px' });
        //        }

        $("#FB_dialog")
	.css({ left: ((pagesize[0] - w) / 2) + "px", top: (arrayPageScroll[1] + ((pagesize[1] - h) / 2)) + "px" })
	.css({ display: "block" })
    .css({ position: "absolute" });
        return this;
    },
    title: function (s) {
        if (s != undefined) {
            $("#FB_dialog_title").text(s);
            return this;
        }
        else {
            return $("#FB_dialog_title").text();
        }
        return this;
    },
    resize: function (w, h) {
        var _height = h ? h : 300;
        var _width = w ? w : 400;
        if (h != "auto" && w != "auto") {
            $("#FB_dialog_content")
        .css({ width: _width + "px", height: _height + "px" });
        }
        return this;
    },
    close: function () {
        $("#FB_dialog").fadeOut("fast", function () {
            $('#TB_overlay,#FB_dialog,#TB_HideSelect,#FB_dialog_iframe').unbind().remove(); 
            if(navigator.userAgent.indexOf("MSIE")>0) { //如果是IE 
            CollectGarbage(); 
        } 
        });
        return this;
    },
    openUrl: function (u, t, w, h) {
        //{url,title,width,height}
        this.init();
        var d = (new Date).getTime();
        if (u.indexOf("?") == -1) {
            u = u + "?_t=" + d
        }
        else {
            u = u + "&_t=" + d
        }
        this.load(w, h);
        $("#FB_dialog_content").append($('<iframe id="FB_dialog_iframe" name="FB_dialog_iframe" scrolling="no" frameborder="0"></iframe>').hide());
        $("#FB_dialog_iframe").attr("src", u);
        var self = this;
        var iframe = document.getElementById("FB_dialog_iframe");
        if (iframe.attachEvent) {
            iframe.attachEvent("onload",
         function () {
             $("#FB_dialog_loading").remove();
             $("#FB_dialog_iframe").show();
             if (w == "auto") {
                 if (iframe.contentDocument && iframe.contentDocument.body.offsetWidth) {
                     w = iframe.contentDocument.body.offsetWidth;
                 } else {
                     w = iframe.Document.body.scrollWidth;
                 }
             }
             if (h == "auto") {

                 if (iframe.contentDocument && iframe.contentDocument.body.offsetHeight) {
                     h = iframe.contentDocument.body.offsetHeight;
                 } else {
                     h = iframe.Document.body.scrollHeight;
                 }
             }

             self.resize(w, h);
             self.position();
             self.title(t);
         });
        }
        else {
            iframe.onload = function () {
                $("#FB_dialog_loading").remove();
                $("#FB_dialog_iframe").show();
                if (w == "auto") {
                    if (iframe.contentDocument && iframe.contentDocument.body.offsetWidth) {
                        w = iframe.contentDocument.body.offsetWidth;
                    } else {
                        w = iframe.Document.body.scrollWidth;
                    }
                }
                if (h == "auto") {

                    if (iframe.contentDocument && iframe.contentDocument.body.offsetHeight) {
                        h = iframe.contentDocument.body.offsetHeight;
                    } else {
                        h = iframe.Document.body.scrollHeight;
                    }
                }

                self.resize(w, h);
                self.position();
                self.title(t);
            };
        }




        return this;
    },
    openHtml: function (str, t, w, h) {
        this.init();
        this.load(w, h);
        this.title(t);
        if (str != undefined) {
            $("#FB_dialog_content").html(str);
            this.resize(w, h);

            this.position();
        }
        return this;
    },
    openAjax: function (url, parm, t, w, h, fun) {
        var self = this;
        this.init();
        this.load(w, h);
        this.title(t);
        $.ajax({
            type: "get",
            url: url,
            data: parm,
            cache: false,
            success: function (result) {
                if (result != null) {
                    $("#FB_dialog_content").html(result);
                    self.resize(w, h);
                    self.position();

                    if ($.isFunction(fun)) {
                        fun();
                    }
                }
            }
        });
    },
    load: function (w, h) {
        this.title("等待...");
        //this.resize(w, "auto");
        this.position();
        $("#FB_dialog").show();
        return this;
    },
    insertHtml: function (str) {
        $("#FB_dialog_content").html(str);
        this.resize("auto", "auto");
        this.position();
    }
}



var dialog = {
    init: function () {

        if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
            $("body", "html").css({ height: "100%", width: "100%" });
            //$("html").css("overflow", "hidden");
            if (document.getElementById("TB_HideSelect") === null) {//iframe to hide select elements in ie6
                $("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div>");
            }
        } else {
            if (document.getElementById("TB_overlay") == null) {
                $("body").append("<div id='TB_overlay'></div>");
            }
        }
        var sbox = '<div class="popout" id="Pop_dialog" style="display:none"><table border="0" cellspacing="0" cellpadding="0"><tbody><tr><td class="bgcorner1"></td><td class="pobg1"></td><td class="bgcorner2"></td></tr><tr><td class="pobg4"></td><td><div class="popoutContent" id="Pop_dialog_content"><span id="Pop_dialog_loading" > 正在加载数据...</span></div></td><td class="pobg2"></td></tr><tr><td class="bgcorner4"></td><td class="pobg3"></td><td class="bgcorner3"></td></tr></tbody></table></div>'
        $("#TB_overlay").html(sbox);
        this.overlaySize();
        return this;
    },
    overlaySize: function () {
        if (window.innerHeight && window.scrollMaxY) {
            yScroll = window.innerHeight + window.scrollMaxY;
        } else if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
            yScroll = document.body.scrollHeight;
        } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
            yScroll = document.body.offsetHeight;
        }
        $("#TB_overlay").css("height", yScroll + "px");
        return this;
    },
    getPageSize: function () {
        var de = document.documentElement;
        var w = window.innerWidth || self.innerWidth || (de && de.clientWidth) || document.body.clientWidth;
        var h = window.innerHeight || self.innerHeight || (de && de.clientHeight) || document.body.clientHeight;

        arrayPageSize = new Array(w, h)
        return arrayPageSize;
    },
    getPageScrollTop: function () {
        var yScrolltop;
        if (self.pageYOffset) {
            yScrolltop = self.pageYOffset;
        } else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
            yScrolltop = document.documentElement.scrollTop;
        } else if (document.body) {// all other Explorers
            yScrolltop = document.body.scrollTop;
        }
        arrayPageScroll = new Array('', yScrolltop)
        return arrayPageScroll;
    },
    position: function () {
        var pagesize = this.getPageSize();
        var arrayPageScroll = this.getPageScrollTop();
        var h = $("#Pop_dialog").height();
        var w = $("#Pop_dialog").width();

        //        $("#Pop_dialog").css({ marginLeft: '-' + parseInt((w / 2), 10) + 'px', width: w + 'px' });
        //        if (!(jQuery.browser.msie && jQuery.browser.version < 7)) { // take away IE6
        //            $("#Pop_dialog").css({ marginTop: '-' + parseInt((h / 2), 10) + 'px' });
        //        }

        $("#Pop_dialog")
	.css({ left: ((pagesize[0] - w) / 2) + "px", top: (arrayPageScroll[1] + ((pagesize[1] - h) / 2)) + "px" })
	.css({ display: "block" })
    .css({ position: "absolute" });
        return this;
    },
    title: function (s) {
        if (s != undefined) {
            $("#Pop_dialog_title").text(s);
            return this;
        }
        else {
            return $("#Pop_dialog_title").text();
        }
        return this;
    },
    resize: function (w, h) {
        var _height = h ? h : 300;
        var _width = w ? w : 400;

        if (h != "auto" && w != "auto") {
            $("#Pop_dialog_content")
        .css({ width: _width + "px", height: _height + "px" });
        }
        return this;
    },
    close: function () {
        $("#Pop_dialog").fadeOut("fast", function () {
            $('#TB_overlay,#Pop_dialog,#TB_HideSelect,#Pop_dialog_iframe').unbind().remove();
            if (navigator.userAgent.indexOf("MSIE") > 0) { //如果是IE 
                CollectGarbage();
            }
        });
        return this;
    },
    openUrl: function (u, w, h) {
        //{url,title,width,height}
        this.init();
        var d = (new Date).getTime();
        if (u.indexOf("?") == -1) {
            u = u + "?_t=" + d
        }
        else {
            u = u + "&_t=" + d
        }
        this.load(w, h);
        $("#Pop_dialog_content").append($('<iframe id="Pop_dialog_iframe" name="Pop_dialog_iframe" scrolling="no" frameborder="0"></iframe>').hide());
        $("#Pop_dialog_iframe").attr("src", u);
        var self = this;
        var iframe = document.getElementById("Pop_dialog_iframe");
        if (iframe.attachEvent) {
            iframe.attachEvent("onload",
         function () {
             $("#Pop_dialog_loading").remove();
             $("#Pop_dialog_iframe").show();
             if (w == "auto") {
                 if (iframe.contentDocument && iframe.contentDocument.body.offsetWidth) {
                     w = iframe.contentDocument.body.offsetWidth;
                 } else {
                     w = iframe.Document.body.scrollWidth;
                 }
             }
             if (h == "auto") {

                 if (iframe.contentDocument && iframe.contentDocument.body.offsetHeight) {
                     h = iframe.contentDocument.body.offsetHeight;
                 } else {
                     h = iframe.Document.body.scrollHeight;
                 }
             }

             self.resize(w, h);
             self.position();
             //self.title(t);
         });
        }
        else {
            iframe.onload = function () {
                $("#Pop_dialog_loading").remove();
                $("#Pop_dialog_iframe").show();
                if (w == "auto") {
                    if (iframe.contentDocument && iframe.contentDocument.body.offsetWidth) {
                        w = iframe.contentDocument.body.offsetWidth;
                    } else {
                        w = iframe.Document.body.scrollWidth;
                    }
                }
                if (h == "auto") {

                    if (iframe.contentDocument && iframe.contentDocument.body.offsetHeight) {
                        h = iframe.contentDocument.body.offsetHeight;
                    } else {
                        h = iframe.Document.body.scrollHeight;
                    }
                }

                self.resize(w, h);
                self.position();
                self.title(t);
            };
        }

        return this;
    },
    openHtml: function (str, t, w, h) {
        this.init();
        this.load(w, h);
        //this.title(t);
        if (str != undefined) {
            $("#Pop_dialog_content").html(str);
            this.resize(w, h);

            this.position();
        }
        return this;
    },
    MouseOverOpenAjax: function (url, offset) {
        var self = this;
        if (!popshow) {
            this.init();
            $("#Pop_dialog").mouseover(function () { popshow = true; }).mouseleave(function () {
                popshow = false;
                dialog.close();
            });
        }

        $("#Pop_dialog").css("left", (offset.left - 10) + "px");
        $("#Pop_dialog").css("top", (offset.top - $("#Pop_dialog").height()) + "px");
        $("#Pop_dialog").css({ display: "block" }).css({ position: "absolute" });
        $("#Pop_dialog").show();
        $.ajax({
            type: "get",
            url: url,
            cache: false,
            success: function (result) {
                if (result != null) {
                    $("#Pop_dialog_content").html(result);
                    self.resize("auto", "auto");
                    $("#Pop_dialog").css("left", (offset.left - 10) + "px");
                    $("#Pop_dialog").css("top", (offset.top - $("#Pop_dialog").height()) + "px");
                    $("#Pop_dialog").css({ display: "block" }).css({ position: "absolute" });
                    $("#Pop_dialog").show()
                }
            }
        });
    },
    openAjax: function (url, parm, t, w, h, fun) {
        var self = this;
        this.init();
        this.load(w, h);
        this.title(t);
        $.ajax({
            type: "get",
            url: url,
            data: parm,
            cache: false,
            success: function (result) {
                if (result != null) {
                    $("#Pop_dialog_content").html(result);
                    self.resize(w, h);
                    self.position();

                    if ($.isFunction(fun)) {
                        fun();
                    }
                }
            }
        });
    },
    Alert: function (str) {
        var self = this;
        this.init();
        this.insertHtml('<div class="popTop"><span class="adel" onclick="dialog.close();"></span>提示</div><div class="clearfix popErrorContent" >' + str + '</div><div class="popBottom"><a href="javascript:void(0)" class="linkImg03" onclick="dialog.close();">确 定</a></div>');
        this.load();
    },
    Confirm: function (str, fun) {
        var self = this;
        this.init();
        this.insertHtml('<div class="popTop"><span class="adel" onclick="dialog.close();"></span>提示</div><div class="clearfix popErrorContent" style="width:250px;" >' + str + '</div><div class="popBottom"><div style="float:left; margin-left:30px;"><a href="javascript:void(0)" class="linkImg03" id="Dialog_btn_Ok">确 定</a></div><div style="float:left; margin-left:30px;"><a href="javascript:void(0)" class="linkImg03" onclick="dialog.close();">取 消</a></div></div>');

        if ($.isFunction(fun)) {
            $("#Dialog_btn_Ok").bind("click", function () { fun() });
        }
    },
    load: function (w, h) {
        //this.title("等待...");
        //this.resize(w, "auto");
        this.position();
        $("#Pop_dialog").show();
        return this;
    },
    insertHtml: function (str) {
        $("#Pop_dialog_content").html(str);
        this.resize("auto", "auto");
        this.position();
    }
}
