﻿/// <reference path="jquery-1[1].3.2-vsdoc2.js" />
jQuery.dialog = function(options)
{
    this.uniqueId = "dialog_" + new Date().getMilliseconds();

    this.settings = jQuery.extend(
                                {
                                    ///标题
                                    title: "",
                                    ///确定按钮
                                    yesCallBack: "",
                                    yesBtnClassName: "btn_blue_s",
                                    yesBtnStyle: "",
                                    yesBtnText: "确定",
                                    ///取消按钮
                                    cancelCallBack: "",
                                    cancelBtnClassName: "btn_wit_s",
                                    cancelBtnStyle: "",
                                    cancelBtnText: "取消",
                                    ///是否自动聚焦
                                    autoFocus:true,                                    
                                    ///对话框内容
                                    isHTML: true,
                                    htmlOrUrl: "",
                                    ///当对话框内容为页面请求时使用
                                    params: "",
                                    optName: "",
                                    ///对话框默认宽度
                                    width:500 
                                },
                                options
                               );
}
jQuery.dialog.prototype.open = function(callBack)
{
    var html = "<div style=\"font-size:14px;text-align:center;line-height:1.5;vertical-align:middle;\"><img src=\"http://sns.qidian.com/images/new/ajax-loader.gif\" alt=\"加载中...\" title=\"加载中...\" />加载中...</div>";
    jQuery(document.body).append(
                            [
                                "<div class=\"popBoxBg\" id=\"popBoxBg_{0}\">".format(this.uniqueId),
                                    "<iframe style='backgroundColor:transparent;width:100%;height:100%;filter:alpha(opacity=0);'></iframe>",//IE6下遮挡select,同时遮挡未设通明属性的flash
                                "</div>",
                                "<div class=\"popBoxContainer\" id=\"popBoxContainer_{0}\">".format(this.uniqueId),
                                    "<div class=\"popBox\"\">",
                                    	"<table cellpadding=\"0\" cellspacing=\"0\" width=\"{0}\">".format(this.settings.width),
                                            "<tr>",
                                                "<td class=\"pbCtl\"></td>",
                                                "<td class=\"pbCt\"></td>",
                                                "<td class=\"pbCtr\"></td>",
                                            "</tr>",
                                            "<tr>",
                                                "<td class=\"pbL\"></td>",
                                                "<td>",
	                                                "<div class=\"pbInsideTitle\">",
		                                                "<span class=\"lVline\"></span>",
		                                                "<h6>{0}</h6>".format(this.settings.title),
		                                                "<span class=\"closePOP\" title=\"关闭\" id=\"spanClose_{0}\" click=\"{1}\">关闭</span>".format(this.uniqueId, this.settings.cancelCallBack),
		                                                "<span class=\"rVline\"></span>",
	                                                "</div>",
	                                                "<form onsubmit=\"return false\">",
	                                                    "<div class=\"pbInsideContent\">",
	                                                        "<div class=\"content\" id=\"popBoxContent_{0}\">{1}</div>".format(this.uniqueId, html),
	                                                        "<input style=\"display:none\" />",
			                                                "<div class=\"bootLine\">",
			                                                    "<input type=\"submit\" value=\"{0}\" id=\"btnOk_{1}\" class=\"{2}\" style=\"{3}\" onclick=\"{4}\"/>".format(
			                                                                                                                                                                    this.settings.yesBtnText,
			                                                                                                                                                                    this.uniqueId,
			                                                                                                                                                                    this.settings.yesBtnClassName,
			                                                                                                                                                                    this.settings.yesBtnStyle,
			                                                                                                                                                                    this.settings.yesCallBack
			                                                                                                                                                                ),
			                                                    "<input type=\"button\" value=\"{0}\" id=\"btnCancel_{1}\" class=\"{2}\" style=\"{3}\" onclick=\"{4}\"/>".format(
			                                                                                                                                                                    this.settings.cancelBtnText,
			                                                                                                                                                                    this.uniqueId,
			                                                                                                                                                                    this.settings.cancelBtnClassName,
			                                                                                                                                                                    this.settings.cancelBtnStyle,
			                                                                                                                                                                    this.settings.cancelCallBack
			                                                                                                                                                                ),
			                                                 "</div>",
			                                             "</div>",
			                                         "</form>",
	                                            "</td>",
	                                            "<td class=\"pbR\"></td>",
                                            "</tr>",
                                            "<tr>",
	                                            "<td class=\"pbCbl\"></td>",
	                                            "<td class=\"pbBt\"></td>",
	                                            "<td class=\"pbCbr\"></td>",
                                            "</tr>",
                                        "</table>",
                                    "</div>",
                                "</div>"
                            ].join("")
                       );
    var self = this;

    ///关闭滚轮事件，隐藏滚动条
    if (!jQuery.browser.msie || document.all[0].nodeValue)
    {
        jQuery(document.documentElement).css("overflow-y", "hidden");
    }
    else
    {
        jQuery(document.body).css("overflow-y", "hidden");
    }
    if (!jQuery.browser.msie && !jQuery.browser.mozilla)
    {
        jQuery(document).bind("mousewheel", stopScroll);
    }

    if (jQuery.browser.msie && (!jQuery.browser.msie < 7 || !document.all[0].nodeValue))
    {
        ///设置IE6遮罩层高度, 拉动窗口时改变遮罩的尺寸, 处理无文档头的问题（主站章节阅读页）
        ///设置对话框的top;
        jQuery("#popBoxContainer_{0}".format(this.uniqueId)).css({ top: jQuery(window).scrollTop() + 100 });
        jQuery(window).resize(
                            function()
                            {
                                jQuery("#popBoxBg_{0}".format(self.uniqueId)).css(
                                                                                    {
                                                                                        height: jQuery(window).height(),
                                                                                        width: jQuery(window).width(),
                                                                                        top: jQuery(window).scrollTop()
                                                                                    }
                                                                                );
                            }
                        );
        jQuery("#popBoxBg_{0}".format(this.uniqueId)).css(
                                                        {
                                                            height: jQuery(window).height(),
                                                            width: jQuery(window).width(),
                                                            top: jQuery(window).scrollTop()
                                                        }
                                                      );
        if (!document.all[0].nodeValue)
        {
            jQuery("#popBoxContainer_{0} table:first".format(self.uniqueId)).css({ marginLeft: (jQuery(window).width() - jQuery("#popBoxContainer_{0} table:first".format(self.uniqueId)).width()) / 2 });
            jQuery(window).resize(
                                function()
                                {
                                    jQuery("#popBoxContainer_{0} table:first".format(self.uniqueId)).css({ marginLeft: (jQuery(window).width() - jQuery("#popBoxContainer_{0} table:first".format(self.uniqueId)).width()) / 2 });
                                }
                            );
        }
    }

    if (this.settings.isHTML)
    {
        jQuery("#popBoxContent_{0}".format(this.uniqueId)).html(this.settings.htmlOrUrl);
        eval(callBack || "");
        if (this.settings.autoFocus)
        {
            jQuery("input[type=text]:visible,input[type=password]:visible,textarea:visible,#btnCancel_{0}:visible".format(self.uniqueId), jQuery("#popBoxContainer_{0}".format(this.uniqueId))[0])[0].focus();
        }
    }
    else
    {
        jQuery(document.body).append("<iframe style=\"display:none\" id=\"iframeTmp\"></iframe>");
        jQuery("#iframeTmp").attr("src", this.settings.htmlOrUrl + "?" + this.settings.params).bind("load", function()
        {
            jQuery('#popBoxContent_{0}'.format(self.uniqueId)).html(this.contentWindow.document.documentElement.innerHTML);
            eval(callBack || '');
            document.body.removeChild(this);
            if (self.settings.autoFocus)
            {
                jQuery("input[type=text]:visible,input[type=password]:visible,textarea:visible,#btnCancel_{0}:visible".format(self.uniqueId), jQuery("#popBoxContainer_{0}".format(self.uniqueId))[0])[0].focus();
            }
        });
    }

    jQuery("#spanClose_{0}:visible,#btnCancel_{0}:visible".format(this.uniqueId)).click(
                                                                                            function()
                                                                                            {
                                                                                                self.close();
                                                                                            }
                                                                                       );

    ///设置ESC键
    jQuery(document).keydown(
                        function(event)
                        {
                            if (event.keyCode == 27)
                            {
                                jQuery("#spanClose_{0}:visible".format(self.uniqueId)).click();
                            }
                        }
                     );
}
jQuery.dialog.prototype.close = function()
{
    ///恢复滚轮事件，滚动条
    if (!jQuery.browser.msie || document.all[0].nodeValue)
    {
        jQuery(document.documentElement).css("overflow-y", "scroll");
    }
    else
    {
        jQuery(document.body).css("overflow-y", "scroll");
    }
    if (!jQuery.browser.msie && !jQuery.browser.mozilla)
    {
        jQuery(document).unbind("mousewheel", stopScroll);
    }
    jQuery("#popBoxBg_{0},#popBoxContainer_{0}".format(this.uniqueId)).remove();
}
///关闭所有对话框实例
jQuery.closeAllDialog = function()
{
    ///恢复滚轮事件，滚动条
    if (!jQuery.browser.msie || document.all[0].nodeValue) 
    {
        jQuery(document.documentElement).css("overflow-y", "scroll");
    }
    else 
    {
        jQuery(document.body).css("overflow-y", "scroll");
    }
    if (!jQuery.browser.msie && !jQuery.browser.mozilla)
    {
        jQuery(document).unbind("mousewheel", stopScroll);
    }
    jQuery(".popBoxBg,.popBoxContainer".format(this.uniqueId)).remove();
}
///阻止滚轮滚动
function stopScroll() { return false; }