﻿//公共变量&方法扩展
var __isIE = (document.all) ? true : false;

var __isIE6 = __isIE && ([/MSIE (\d)\.0/i.exec(navigator.userAgent)][0][1] == 6);

var __Class = {
    create: function () {
        return function () { this.initialize.apply(this, arguments); }
    }
};

var __Extend = function (destination, source) {
    for (var property in source) {
        destination[property] = source[property];
    }
};

var __Bind = function (object, fun) {
    return function () {
        return fun.apply(object, arguments);
    }
};

var __Each = function (list, fun) {
    for (var i = 0, len = list.length; i < len; i++) { fun(list[i], i); }
};

var __Contains = function (a, b) {
    return a.contains ? a != b && a.contains(b) : !!(a.compareDocumentPosition(b) & 16);
};

var $$ = function (id) {
    return "string" == typeof id ? document.getElementById(id) : id;
};

var __movingDrag = false;
var __movingNode = null;
function __releaseMoving(e) {
    __movingDrag = false;
    if (__movingNode) {
        if (__isIE)
            __movingNode.releaseCapture();
        else
            document.releaseEvents(Event.MOUSEMOVE | Event.MOUSEUP);
        __movingNode = null;
    }
}

//Init
if (__isIE) {
    document.attachEvent("onmouseup", __releaseMoving);
} else {
    document.addEventListener("mouseup", __releaseMoving, false);
}



//OverLayer Class
var OverLayer = __Class.create();
OverLayer.prototype = {
    initialize: function (options) {

        this.SetOptions(options);

        this.Layer = $$(this.options.Layer) || document.body.insertBefore(document.createElement("div"), document.body.childNodes[0]);

        this.Color = this.options.Color;
        this.Opacity = parseInt(this.options.Opacity);

        with (this.Layer.style) { display = "none"; left = top = 0; position = "fixed"; width = height = "100%"; }

        if (__isIE6) {
            this.Layer.style.position = "absolute";
            //ie6设置覆盖层大小程序
            this._resize = __Bind(this, function () {
                this.Layer.style.width = Math.max(document.documentElement.scrollWidth, document.documentElement.clientWidth) + "px";
                this.Layer.style.height = Math.max(document.documentElement.scrollHeight, document.documentElement.clientHeight) + "px";
            });
            //遮盖select
            this.Layer.innerHTML = '<iframe src="" style="position:absolute;top:0;left:0;width:100%;height:100%;filter:alpha(opacity=0);"></iframe>';
        }
    },
    //设置默认属性
    SetOptions: function (options) {
        this.options = {//默认值
            Layer: null, //覆盖层对象
            Color: "gray", //背景色
            Opacity: 50 //透明度(0-100)
        };
        __Extend(this.options, options || {});
    },
    //显示
    Show: function () {
        //兼容ie6
        if (__isIE6) { this._resize(); window.attachEvent("onresize", this._resize); }
        //设置样式
        with (this.Layer.style) {
            //设置透明度
            __isIE ? filter = "alpha(opacity=" + this.Opacity + ")" : opacity = this.Opacity / 100;
            backgroundColor = this.Color; display = "block";
            zIndex = __lightIndex;
            __lightIndex++;
        }
    },
    //关闭
    Close: function () {
        this.Layer.style.display = "none";
        if (__isIE6) { window.detachEvent("onresize", this._resize); }
    }
};



//LightBox Class
var LightBox = __Class.create();
LightBox.prototype = {
    //初始化
    initialize: function (box, options) {
        var _div = document.createElement('DIV');
        var _maxHeight = document.documentElement.clientHeight - 40;
        var _maxWidth = document.documentElement.clientWidth - 40;
        var _ctrlStyle ='cursor:pointer;font-family:verdana;font-weight:bold;color:#ffffff;background-color:#CCCCCC;height:20px; width:20px; padding:0px; margin:0px; border:0px;font-size:9px;line-height:9px;';
        _div.setAttribute('id', box + '_Window');
        _div.innerHTML = '<dl id="' + box + '_Layer" style="height:' + _maxHeight + 'px;width:' + _maxWidth + ';margin:0px auto;padding:0px;background:#FF8000;border:0px;">\n' +
        '<div style="position:relative;background:#ffffff;border:1px solid #CCCCCC;top:-5px;left:-5px;">\n' +
        '  <dt id="' + box + '_Head" style="height:21px;width:auto;margin:0px;padding:0px;background-color:#CCCCCC;border-bottom:1px solid #bbbbbb;">\n' +
      // ' <span style="float:left;font-weight:bold;color:#ffffff;padding-left:4px;padding-top:2px;cursor:default" id="' + box + '_Title"></span>\n' +━〓

        '    <span style="float:right;width:100px;">' +
        '       <input id="' + box + '_Min" type="button" value="" style="' + _ctrlStyle + '" />' +
        '       <input id="' + box + '_Max" type="button" value="" style="' + _ctrlStyle + '" />' +
        '       <input id="' + box + '_Close" type="button" value="X" style="' + _ctrlStyle + '" />' +
        '    </span>\n' +
        '  </dt>\n' +
        '  <dd id="' + box + '_Body" style="height:' + (_maxHeight - 24) + 'px;width:auto;margin:0px;padding:0px;">\n' +
        '    <iframe id="' + box + '_Pager" scrolling="" marginheight="0" marginwidth="0" frameborder="0" width="100%" height="100%"  src=""></iframe>\n' +
        '  </dd>\n' +
        '</div>\n</dl>\n';
        document.body.insertBefore(_div, document.body.childNodes[0]);

        this.ID = box;
        this.Height = 300;
        this.Width = 400;
        this.BoxLayer = $$(this.ID + '_Layer'); //显示层
        this.OverLayer = new OverLayer(options); //覆盖层
        this.SetOptions(options);

        this.Fixed = !!this.options.Fixed;
        this.Over = !!this.options.Over;
        this.Center = !!this.options.Center;
        this.onShow = this.options.onShow;
        this.onClose = this.options.onClose;
        this.Top = this.options.Top;
        this.Bottom = this.options.Bottom;
        this.Left = this.options.Left;
        this.Right = this.options.Right;

        this.BoxLayer.style.display = "none";

        //兼容ie6用的属性
        if (__isIE6) {
            this._top = this._left = 0; this._select = [];
            this._fixed = __Bind(this, function () { this.Center ? this.SetCenter() : this.SetFixed(); });
        }

        //注册关闭按钮
        var o = this;
        $$(this.ID + '_Head').onmouseover = function (e) {
            moveLightBox($$(o.ID + '_Head'), $$(o.ID + '_Layer'));
        };
        $$(this.ID + '_Close').onclick = function () { o.Close(); };
        $$(this.ID + '_Max').onclick = function () { o.Max(); };
        $$(this.ID + '_Min').onclick = function () { o.Min(); };
    },
    //设置默认属性
    SetOptions: function (options) {
        this.options = {//默认值
            Over: false, //是否显示覆盖层
            Fixed: false, //是否固定定位
            Center: true, //是否居中
            onShow: function () { }, //显示时执行
            onClose: function () { }, //关闭时执行
            Top: null,
            Bottom: null,
            Left: null,
            Right: null,
            zIndex: 0
        };
        __Extend(this.options, options || {});
    },
    //兼容ie6的固定定位程序
    SetFixed: function () {
        this.BoxLayer.style.top = document.documentElement.scrollTop - this._top + this.BoxLayer.offsetTop + "px";
        this.BoxLayer.style.left = document.documentElement.scrollLeft - this._left + this.BoxLayer.offsetLeft + "px";
        this._top = document.documentElement.scrollTop; this._left = document.documentElement.scrollLeft;
    },
    //兼容ie6的居中定位程序
    SetCenter: function () {
        this.BoxLayer.style.marginTop = document.documentElement.scrollTop - this.BoxLayer.offsetHeight / 2 + "px";
        this.BoxLayer.style.marginLeft = document.documentElement.scrollLeft - this.BoxLayer.offsetWidth / 2 + "px";
    },
    //显示
    Show: function (pager, title, _height, _width) {
        if (_height)
            this.Height = _height;
        if (_width)
            this.Width = _width;

        //固定定位
        this.BoxLayer.style.position = this.Fixed && !__isIE6 ? "fixed" : "absolute";
        this.BoxLayer.style.zIndex = __lightIndex;
        __lightIndex++;

        //上下左右
        if (this.Top != null)
            this.BoxLayer.style.top = this.Top;
        if (this.Bottom != null)
            this.BoxLayer.style.bottom = this.Bottom;
        if (this.Left != null)
            this.BoxLayer.style.left = this.Left;
        if (this.Right != null)
            this.BoxLayer.style.right = this.Right;

        //覆盖层
        this.Over && this.OverLayer.Show();

        this.BoxLayer.style.display = "block";

        if (_height) {
            $$(this.ID + '_Layer').style.height = _height + 'px';
            $$(this.ID + '_Body').style.height = (_height - 24) + 'px';
        }

        if (_width) {
            $$(this.ID + '_Layer').style.width = _width + 'px';
            $$(this.ID + '_Body').style.width = (_width - 2) + 'px';
        }

        //居中
        if (this.Center) {
            this.BoxLayer.style.top = this.BoxLayer.style.left = "50%";
            //设置margin
            if (this.Fixed) {
                this.BoxLayer.style.marginTop = -this.BoxLayer.offsetHeight / 2 + "px";
                this.BoxLayer.style.marginLeft = -this.BoxLayer.offsetWidth / 2 + "px";
            } else {
                this.SetCenter();
            }
        }

        //兼容ie6
        if (__isIE6) {
            if (!this.Over) {
                //没有覆盖层ie6需要把不在BoxLayer上的select隐藏
                this._select.length = 0;
                __Each(document.getElementsByTagName("select"), __Bind(this, function (o) {
                    if (!__Contains(this.BoxLayer, o)) { o.style.visibility = "hidden"; this._select.push(o); }
                }))
            }
            //设置显示位置
            this.Center ? this.SetCenter() : this.Fixed && this.SetFixed();
            //设置定位
            this.Fixed && window.attachEvent("onscroll", this._fixed);
        }

        //设置导航页
        if (pager)
            $$(this.ID + '_Pager').src = pager;

        //设置标题
        if (title)
           $$(this.ID + '_Title').innerHTML = title;
        else
            $$(this.ID + '_Title').innerHTML = pager;

        this.onShow();
    },
    //关闭
    Close: function () {
        var pager = $$(this.ID + '_Pager');
        if (pager) {
            pager.src = 'about:blank';
        }
        this.BoxLayer.style.display = "none";
        this.OverLayer.Close();
        if (__isIE6) {
            window.detachEvent("onscroll", this._fixed);
            __Each(this._select, function (o) { o.style.visibility = "visible"; });
        }
        this.onClose();
    }
    ,
    //Max
    Max: function () {
        $$(this.ID + '_Body').style.display = '';
        $$(this.ID + '_Layer').style.height = this.Height + 'px';
        $$(this.ID + '_Layer').style.width = this.Width + 'px';
        $$(this.ID + '_Body').style.width = (this.Width - 2) + 'px';
    }
    ,
    //Min
    Min: function () {
        $$(this.ID + '_Body').style.display = 'none';
        $$(this.ID + '_Layer').style.height = '24px';
        $$(this.ID + '_Layer').style.width = '200px';
        $$(this.ID + '_Body').style.width = '198px';
    }
};



var __lightBoxes = new Object();
var __lightIndex = 1000;
function showLightBox(id, url, title, _height, _width, _options) {
    /// <summary>
    /// showLightBox 弹出窗口层(全局调用)
    /// </summary>
    /// <param name="id">窗口ID</param>
    /// <param name="url">页面地址</param>
    /// <param name="url">页面标题</param>
    /// <param name="_height">高度(可选)</param>
    /// <param name="_width">宽度(可选)</param>
    /// <param name="_options">可选项(Over:false, Fixed:false, Center:true, onShow:function(){}, onClose:function(){}, Top:0, Bottom:0, Left:0, Right:0)</param>
    /// <returns>void</returns>

    var _lightBox = __lightBoxes[id];
    if (!_lightBox) {
        _lightBox = new LightBox(id, _options);
        __lightBoxes[id] = _lightBox;
        var __upper = 0;
        var __height = _height.toString();
        __upper = __height.length - 1;
        if (__height.indexOf('%') == __upper) {
            _height = (document.body.clientHeight - 4) * parseInt(__height.substr(0, __upper), 10) / 100;
        }
        var __width = _width.toString();
        __upper = __width.length - 1;
        if (__width.indexOf('%') == __upper) {
            _width = (document.body.clientWidth - 4) * parseInt(__width.substr(0, __upper), 10) / 100;
        }
        _lightBox.Show(url,title, _height, _width);

    }
    else {
        _lightBox.Max();
        _lightBox.Show(url,title);
    }
}


function hideLightBox(id) {
    /// <summary>
    /// showLightBox 隐藏窗口层(全局调用)
    /// </summary>
    /// <param name="id">窗口ID</param>
    /// <returns>bool</returns>

    var _lightBox = __lightBoxes[id];
    if (_lightBox) {
        _lightBox.Min();
        return true;
    }
    return false;
}


function closeLightBox(id) {
    /// <summary>
    /// showLightBox 关闭窗口层(全局调用)
    /// </summary>
    /// <param name="id">窗口ID</param>
    /// <returns>bool</returns>

    var _lightBox = __lightBoxes[id];
    if (_lightBox) {
        _lightBox.Close();
        return true;
    }
    return false;
}


function moveLightBox(o, p) {
    /// <summary>
    /// moveLightBox 移动窗口层(全局调用)
    /// </summary>
    /// <param name="o">捕获对象</param>
    /// <param name="p">移动对象</param>
    /// <returns>bool</returns>

    var x, y;
    o = $$(o);
    p = $$(p) || o;

    o.onmousedown = function (e) {
        if (!e) e = window.event;
        __movingDrag = true;
        __movingNode = o;
        if (__isIE)
            __movingNode.setCapture();
        else
            document.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP);
        with (p) {
            style.position = "absolute";
            var temp1 = offsetLeft; var temp2 = offsetTop;
            x = e.clientX; y = e.clientY;
            style.left = temp1 + e.clientX - x + "px";
            style.top = temp2 + e.clientY - y + "px";
            style.marginLeft = 0;
            style.marginTop = 0;
        }
        p.onmousemove = function (e) {
            if (!e) e = window.event;
            if (!__movingDrag) return false;
            with (p) {
                style.left = temp1 + e.clientX - x + "px";
                style.top = temp2 + e.clientY - y + "px";
            }
        };
    };
}
