﻿//<![CDATA[
/*
 * 公共程序
 * (c) jiang zheng 2009.06.17
 * QQ: 470192616
 */
 
 /*
// ----- main page use ------------
MsgBox.BackFn['HideBox'] = function(){
	// 隐藏对话框
	MsgBox.HideBox();
};

$('inBaseSet').onclick = function(){
	var url = 'subpage.asp?sn=323&st=545&v=' + Math.random();		
	MsgBox.ShowBox({src:url,width:528,height:391,top:50});
};

// ----- sub page use ------------
function CallParent(src,arg)
{
	try{parent.MsgBox.GetBack(src,arg)}
	catch(e){	}
}

CallParent('HideBox');

*/
var MsgBox =
{
    f: null, // 对话框背景层
    b: null, // 对话框层
    p: null, // iframe文件地址加载的ID
    curPos: { T: 0, L: 0 }, // 当前的位置
    isActive: false, // 是否活动窗口
    isIe: (!!(/msie/i.test(navigator.userAgent))),
    BackFn: new Object(),
    NoActionFn: null, //点击关闭按钮或者按esc键关闭窗口时执行的函数
    Hack: function(arg) {
        // 隐藏显示flash等媒体对象
        if (!MsgBox.isIe6()) return;

        var vs = (!!arg) ? 'hidden' : 'visible';
        try {
            var o, i, j;
            o = document.getElementsByTagName("object");
            for (i = 0, j = o.length; i < j; i++) { o[i].style.visibility = vs; }
            o = document.getElementsByTagName("select");
            for (i = 0, j = o.length; i < j; i++) { o[i].style.visibility = vs; }
        } catch (e) { }
    },
    isIe6: function() {
        // 判断是否IE6
        var me = MsgBox;
        var rarg = false;
        if (me.isIe) {
            var re = /MSIE( )(\d+(\.\d+)?)/;
            if (re.test(window.navigator.userAgent)) { if (6 == parseFloat(RegExp.$2)) { rarg = true; } }
        }
        return rarg;
    },
    Cent: function() {
        // 设置位置
        var me = MsgBox;
        var fw = Math.max(document.documentElement.scrollWidth, document.documentElement.clientWidth);
        var fh = Math.max(document.documentElement.scrollHeight, document.documentElement.clientHeight);

        me.curPos.L = Math.ceil((fw - me.b.clientWidth) / 2);
        me.b.style.left = me.curPos.L + "px";
        me.f.style.width = fw + "px";
        me.f.style.height = fh + "px";
        me.f.style.display = 'block';
        me.b.style.display = 'block';
    },
    CreateBox: function(Fn) {
        // 创建显示框
        var me = MsgBox;
        var BoxLt = '_' + Math.random().toString().substr(2);

        var divf = document.createElement('div');
        divf.id = 'xcncsFrame' + BoxLt;
        divf.style.zIndex = 11000;
        divf.style.display = 'block';
        divf.style.position = 'absolute';
        divf.style.backgroundColor = '#333';
        divf.style.width = '0px';
        divf.style.height = '0px';
        divf.style.left = '0px';
        divf.style.top = '0px';
        divf.style.textAlign = 'center';
        if (me.isIe) {
            divf.style.filter = "alpha(opacity=50,style=0)";
        } else {
            divf.style.opacity = '0.5';
        }
        document.body.appendChild(divf);

        var divb = document.createElement('div');
        divb.id = 'xcncsBox' + BoxLt;
        divb.style.zIndex = 11001;
        divb.style.display = 'block';

        if (me.isIe6() == true) {
            divb.style.position = 'absolute';
        } else {
            divb.style.position = 'fixed';
        }

        divb.style.backgroundColor = '#FFFFFF'; // 'Transparent';		
        divb.style.width = '0px';
        divb.style.height = '0px';
        divb.style.left = '0px';
        divb.style.top = '0px';
        divb.style.margin = 'auto';
        /*
        var divp = document.createElement('iframe');
        divp.id  = 'xcncsTag' + BoxLt;
        divp.style.zIndex    = 11003;
        divp.style.width     = '0px';
        divp.style.height    = '0px';
        divp.setAttribute('frameborder','0');
        divp.setAttribute('marginheight','0');
        divp.setAttribute('marginwidth','0');
        divp.setAttribute('allowtransparency','transparen');
        divb.appendChild(divp);
        */
        divb.innerHTML = '<iframe id="xcncsTag' + BoxLt + '" src="/Dialog/aboutBlank.htm" style="width:0px; height:0px; z-index: 11003;" scrolling="no" frameborder="0" marginheight="0" marginwidth="0"></iframe>';
        document.body.appendChild(divb);

        me.f = $('#xcncsFrame' + BoxLt)[0];
        // 背景面板对象
        me.b = $('#xcncsBox' + BoxLt)[0];
        // 消息框对象
        me.p = $('#xcncsTag' + BoxLt)[0];
        // 目标框架对象
        if (typeof (Fn) == 'function') {
            Fn();
        }
    },
    SetPos: function(arg) {
        // 设置位置
        var me = MsgBox;
        if (arg.top) {
            me.curPos.T = arg.top;
            me.b.style.top = arg.top + 'px';
        }
        if (arg.width) {
            me.p.style.width = arg.width + 'px';
            me.b.style.width = arg.width + 'px';
        }
        if (arg.height) {
            me.p.style.height = arg.height + 'px';
            me.b.style.height = arg.height + 'px';
        }
        me.Cent();
        if (me.isIe6() == true) { me.Ie6Hack(); }
    },
    ShowBox: function(arg) {
        // 显示对话框
        var me = MsgBox;
        if (me.f == null) {
            me.CreateBox(function() {
                me.ShowBox2(arg);
            });
        } else {
            me.ShowBox2(arg);
        }
    },
    ShowBox2: function(arg) {
        var me = MsgBox;
        me.p.src = arg.src + (-1 == arg.src.toString().indexOf('?') ? '?vs=' : '&vs=') + Math.random();
        me.isActive = true;
        me.Hack(true);
        me.SetPos(arg);
        // Comm.BindElementEventAdd(document, 'keydown', me.KeyPressHandler);
    },
    HideBox: function(isCall) {
        // 隐藏对话框 是否强制关闭，关闭按钮关闭或者是escape键关闭的则为1否则为0
        var me = MsgBox;
        me.Hack(false);
        me.p.src = "/Dialog/aboutBlank.htm";
        me.f.style.display = 'none';
        me.b.style.display = 'none';
        me.isActive = false;
        me.f = null;
        isCall = isCall ? (isCall == 1 ? 1 : 0) : 0; // 验证是否强制关闭的

        if (isCall == 1) {
            if (typeof (me.NoActionFn) == 'function') {
                me.NoActionFn();
            }
        }
        // Comm.BindElementEventRemove(document, 'keydown', me.KeyPressHandler);
    },
    GetBack: function(src, arg) {
        // 回调处理
        var me = MsgBox;
        var fn = me.BackFn[src];
        if (fn && typeof (fn) == 'function') {
            fn(arg);
        } else {
            me.HideBox();
            alert('Error: Undefined callback function name of ' + src + ' ');
        }
    },
    KeyPressHandler: function(event) {
        // 按键事件检测
        var me = MsgBox;
        var e = window.event || event;
        var kC = e.keyCode;
        if (kC == 27) {
            // ESC 键
            if (me.isActive) {
                me.HideBox(1);
            }
        }
    },
    Ie6Hack: function() {
        // 针对IE6的处理
        var me = MsgBox;
        if (me.isActive) {
            me.b.style.top = (document.documentElement.scrollTop + MsgBox.curPos.T) + "px";
            me.b.style.left = (document.documentElement.scrollLeft + MsgBox.curPos.L) + "px";
        }
    }
};

MsgBox.BackFn['ExecFun'] = function(fn) {
    // 执行给定的函数
    MsgBox.HideBox();
    fn();
};

MsgBox.BackFn['GoToUrl'] = function(url){
	// 跳转到指定页面
	MsgBox.HideBox();
	top.location = url;
};
MsgBox.BackFn['HideBox'] = function(arg){
    // 隐藏对话框
	MsgBox.HideBox(arg);
};
MsgBox.BackFn['SetPos'] = function(arg){
	// 改变对话框属性
	MsgBox.SetPos(arg);
}

window.onresize = function(){
    // 如果是活动的就设置中心主要是修正改变窗口大小事件
	if (MsgBox.isActive) {
		try {
			MsgBox.Cent();
		}catch (e){ }
	}
};

if(MsgBox.isIe6()==true){
    window.onscroll = function(){
        MsgBox.Ie6Hack();
    }
}

document.onkeydown = MsgBox.KeyPressHandler;
 
 //]]>
