﻿
var binfo = (function()
{
    var ua = navigator.userAgent.toLowerCase();
	return {
	    ie : /*@cc_on!@*/false,
		i7 : /*@cc_on!@*/false && (parseInt(ua.match(/msie (\d+)/)[1],10) >= 7)
	};
})();

var config = { opac : 0.40, bgcolor : '#000', bzi : null, it : null, il : null };

var tool =
{
    restyle: function(e) {
        e.style.cssText = 'margin:0;padding:0;background-image:none;background-color:transparent;border:1;';
    },

    ststyle: function(e, dict) {
        var style = e.style;
        for (var n in dict) style[n] = dict[n];
    },

    getestyle: function(e, p) {
        if (binfo.ie)
            return e.currentStyle[p];
        else
            return e.ownerDocument.defaultView.getComputedStyle(e, '').getPropertyValue(p);
    },

    stopac: function(e, opac) {
        if (binfo.ie) {
            opac = Math.round(opac * 100);
            e.style.filter = (opac > 100 ? '' : 'alpha(opacity=' + opac + ')');
        }
        else
            e.style.opacity = opac;
    },

    getvoid: function() {
        if (binfo.ie)
            return (binfo.i7 ? '' : 'javascript:\'\'');
        return 'javascript:void(0)';
    },

    addevt: function(o, e, l) {
        if (binfo.ie)
            o.attachEvent('on' + e, l);
        else
            o.addEventListener(e, l, false);
    },

    remevt: function(o, e, l) {
        if (binfo.ie)
            o.detachEvent('on' + e, l);
        else
            o.removeEventListener(e, l, false);
    },

    isdtd: function(doc) {
        return ('CSS1Compat' == (doc.compatMode || 'CSS1Compat'));
    },

    getclsize: function(w) {
        if (binfo.ie) {
            var oSize, doc = w.document.documentElement;
            oSize = (doc && doc.clientWidth) ? doc : w.document.body;

            if (oSize)
                return { w: oSize.clientWidth, h: oSize.clientHeight };
            else
                return { w: 0, h: 0 };
        }
        else
            return { w: w.innerWidth, h: w.innerHeight };
    },

    getev: function() {
        if (binfo.ie) return window.event;
        var func = tool.getev.caller;
        while (func != null) {
            var arg = func.arguments[0];
            if (arg && (arg + '').indexOf('Event') >= 0) return arg;
            func = func.caller;
        }
        return null;
    },

    getepos: function(o) {
        var l, t;
        if (o.getBoundingClientRect) {
            var el = o.getBoundingClientRect(); l = el.left; t = el.top;
        }
        else {
            l = o.offsetLeft - Math.max(document.documentElement.scrollLeft, document.body.scrollLeft);
            t = o.offsetTop - Math.max(document.documentElement.scrollTop, document.body.scrollTop);
        }
        return { x: l, y: t };
    },

    getspos: function(w) {
        if (binfo.ie) {
            var doc = w.document;
            var oPos = { X: doc.documentElement.scrollLeft, Y: doc.documentElement.scrollTop };
            if (oPos.X > 0 || oPos.Y > 0) return oPos;
            return { X: doc.body.scrollLeft, Y: doc.body.scrollTop };
        }
        else
            return { X: w.pageXOffset, Y: w.pageYOffset };
    },
    // 加载xml文档
    getXml: function(xmlFile) {
        var xmlDoc;
        if (window.ActiveXObject) {
            xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
            xmlDoc.async = false;
            xmlDoc.load(xmlFile);
        }
        else if (document.implementation && document.implementation.createDocument) {
            xmlDoc = document.implementation.createDocument('', '', null);
            xmlDoc.load(xmlFile);
        }
        else {
            return null;
        }
        return xmlDoc;
    },
    //获取当前皮肤
    getSkin: function() {
        try {
            // 首先对xml对象进行判断
            var url = "../config/general.xml";
            //在子模块中引用时，使用绝对路径进行定位
            if (document.getElementById("PathUrl") != null) {
                url = document.getElementById("PathUrl").value + "config/general.xml";
            }
            else if (window.parent.document.getElementById("PathUrl") != null) {
                url = window.parent.document.getElementById("PathUrl").value + "config/general.xml";
            }
            else {
                var url = "config/general.xml";
            }
            var xmlDoc = tool.getXml(url);
            if (xmlDoc == null) {
                alert('您的浏览器不支持xml文件读取,于是本页面禁止您的操作,推荐使用IE5.0以上可以解决此问题!');
                return "";
            }
            // 然后开始获取需要的的第一个节点的属性值../skins/allpassegov/styles/
            var skin = xmlDoc.selectSingleNode('GeneralConfigInfo/Skin').text;
            return "../skins/" + skin + "/styles/";
        }
        catch (e) {
            return "";
        }
    },
    getlink: function(c) {
        if (c.length == 0) return;
        return '<' + 'link href="' + tool.getSkin() + c + '" type="text/css" rel="stylesheet"/>';
    },

    regdoll: function(w) {
        if (binfo.ie)
            w.$ = w.document.getElementById;
        else
            w.$ = function(id) { return w.document.getElementById(id); };
    },

    getedoc: function(e) {
        return e.ownerDocument || e.document;
    },

    disctmenu: function(doc) {
        doc.oncontextmenu = function(e) {
            e = e || event || this.parentWindow.event;
            var o = e.srcElement || e.target;
            if (!(o.type == 'password' || o.type == 'text' || o.type == 'textarea')) {
                if (binfo.ie) e.returnValue = false; else e.preventDefault();
            }
        };
    },

    getpath: function() {
        var bp, len, sc = document.getElementsByTagName('script');
        for (var i = 0; i < sc.length; i++) {
            bp = sc[i].src.substring(0, sc[i].src.toLowerCase().indexOf('dialog.js'));
            len = bp.lastIndexOf('/'); if (len > 0) bp = bp.substring(0, len + 1); if (bp) break;
        }
        if (binfo.ie && bp.indexOf('../') != -1) {
            var fp = window.location.href;
            fp = fp.substring(0, fp.lastIndexOf('/'));
            while (bp.indexOf('../') >= 0) {
                bp = bp.substring(3);
                fp = fp.substring(0, fp.lastIndexOf('/'));
            } return fp + '/' + bp;
        } else return bp;
    },



    //lcs_获取字符串长度，汉字算两个
    len: function(str) { return str.replace(/[^\x00-\xff]/g, "rr").length; },
    sub: function(str, n) {
        var r = /[^\x00-\xff]/g;
        if (str.replace(r, "mm").length <= n) return str;
        var m = Math.floor(n / 2);
        for (var i = m; i < str.length; i++) {
            if (str.substr(0, i).replace(r, "mm").length >= n)
            { return str.substr(0, i) + "..."; }
        }
        return str;
    },

    //lcs_获取对象距离浏览器顶端和左边的距离 
    getElementPos: function(element) {
        var offsetTop = element.offsetTop;
        var offsetLeft = element.offsetLeft;
        while (element = element.offsetParent) {
            offsetTop += element.offsetTop;
            offsetLeft += element.offsetLeft;
        }
        return { x: offsetLeft, y: offsetTop };
    },




    remnode: function(n) { return n.parentNode.removeChild(n); }
};

var dialog = (function() {
    var twin = window.parent, cover;
    while (twin.parent && twin.parent != twin) {
        try { if (twin.parent.document.domain != document.domain) break; } catch (e) { break; }
        twin = twin.parent;
    }
    var tdoc = twin.document;

    var getzi = function() {
        config.bzi = Math.max(config.bzi + 1, new Date().getTime());
        return config.bzi;
    };

    var resizehdl = function() {
        if (!cover) return;
        var rel = tool.isdtd(tdoc) ? tdoc.documentElement : tdoc.body;
        tool.ststyle(cover,
		{
		    'width': Math.max(rel.scrollWidth, rel.clientWidth, tdoc.scrollWidth || 0) - 1 + 'px',
		    'height': Math.max(rel.scrollHeight, rel.clientHeight, tdoc.scrollHeight || 0) - 1 + 'px'
		});
    };

    return {



        open: function(title, pageUrl, width, height) {
            this.opendlg(title, pageUrl, width, height, true, null, null, true, null);
        },


        alert: function(title, pageCode, width, height) {
            this.opendlg(title, pageCode, width, height, true, 'hcode', null, true, null);
        },

        success: function(title, pageCode, width, height) {
            this.opendlg(title, '<div style="text-align:center;"><img src="../images/main/tip_right.gif" style="vertical-align:middle; margin-right:10px;" />' + pageCode + '</div>', width, height, true, 'hcode', null, true, null);
        },

        fail: function(title, pageCode, width, height) {
            this.opendlg(title, '<div style="text-align:center;"><img src="../images/main/tip_wrong.gif" style="vertical-align:middle; margin-right:10px;" />' + pageCode + '</div>', width, height, true, 'hcode', null, true, null);
        },



        /*
        opendlg : function( 
        t,    窗口标题
        p,    页面地址或消息源码
        w,    窗口宽度
        h,    窗口高度
        c,    是否显示覆盖层
        i,    为hcode时为消息源码显示
        n,    窗口ID
        s,    是否固定定位
        l     窗口坐标{top:XX,left:XX}
        )
        */

        opendlg: function(t, p, w, h, c, i, n, s, l) {
            if (c) {
                this.dcover();
            }
            else {
                if (cover) {
                    cover = null;
                }
            }


            //得到顶级窗口的宽高
            var _maxh = top.document.body.clientHeight;
            var _maxw = top.document.body.clientWidth;



            var dinfo = { tit: t, page: p, win: window, topw: twin, link: i, sc: s, maxh: _maxh, maxw: _maxw };
            var clsize = tool.getclsize(twin), spos = tool.getspos(twin);

            var schdl = function() {
                var rel = tool.getspos(twin);
                tool.ststyle(dfrm, { 'top': rel.Y + config.it + 'px', 'left': rel.X + config.il + 'px' });
            };

            var it = (l && l.top) ? spos.Y + l.top : Math.max(spos.Y + (clsize.h - h - 20) / 2, 0);
            var il = (l && l.left) ? spos.X + l.left : Math.max(spos.X + (clsize.w - w - 20) / 2, 0);

            var dfrm = tdoc.createElement('iframe');
            tool.restyle(dfrm);

            if (n) dfrm.id = n;

            dfrm.frameBorder = 0;
            dfrm.src = tool.getpath() + 'dialog.html';

            tool.ststyle(dfrm,
			{
			    'position': 'absolute',
			    'top': it + 'px',
			    'left': il + 'px',
			    'width': w + 'px',
			    'height': h + 'px',
			    'zIndex': getzi()
			});

            if (s) {
                config.it = it - spos.Y;
                config.il = il - spos.X;
            };
            if (s) {
                tool.addevt(twin, 'scroll', schdl);
            }

            tdoc.body.appendChild(dfrm);
            dfrm._dlgargs = dinfo;
        },

        closdlg: function(d, c) {
            var dlg = ('object' == typeof (d)) ? d.frameElement : document.getElementById(d);
            if (dlg) tool.remnode(dlg);
            if (c) this.hcover(c);
        },

        dcover: function() {
            cover = tdoc.createElement('div');
            tool.restyle(cover);

            tool.ststyle(cover,
			{
			    'position': 'absolute', 'zIndex': getzi(), 'top': '0px',
			    'left': '0px', 'backgroundColor': config.bgcolor
			});
            tool.stopac(cover, config.opac);

            if (binfo.ie && !binfo.i7) {
                var ifrm = tdoc.createElement('iframe');
                tool.restyle(ifrm);
                ifrm.hideFocus = true;
                ifrm.frameBorder = 0;
                ifrm.src = tool.getvoid();

                tool.ststyle(ifrm,
				{
				    'width': '100%', 'height': '100%', 'position': 'absolute', 'left': '0px',
				    'top': '0px', 'filter': 'progid:DXImageTransform.Microsoft.Alpha(opacity=0)'
				});
                cover.appendChild(ifrm);
            }

            tool.addevt(twin, 'resize', resizehdl);
            resizehdl();
            tdoc.body.appendChild(cover);
        },

        gcover: function() { return cover; },
        hcover: function(o) { tool.remnode(o); cover = null; o = null; }
    };
})();
