﻿// 页面弹出操作或提示层 Author:guoqiaobin 090217
var screenDivId = "screenDiv";
function PageAlert(o){
    if(o){
        //弹出层是否可见
        this.isVisible = false;
        //弹出层对象
        this.alertDiv = o;
        this.alertDiv.style.zIndex = "101";
        this.alertDiv.style.position = "absolute";
        this.alertDiv.style.display = "none";
        this.width = null;
        this.height = null;
    //    this.left = null;
    //    this.top = null;
    }
    
    //全屏遮盖层
    if(document.getElementById(screenDivId)){
        this.screenDiv = document.getElementById(screenDivId);
    }else{
        this.screenDiv = document.createElement("div");
//        screenDiv.setAttribute("style", "float:left; position:absolute; left:0; top:0; width:100%; height:100%; z-index:100; background:#000; display:none; filter:alpha(opacity=30); -moz-opacity:.3; opacity:0.3;");//此方法的透明度设置在ie6中无效……
//        this.screenDiv.style.float = "left";
        this.screenDiv.id = screenDivId;
        this.screenDiv.style.display = "none";
        this.screenDiv.style.position = "absolute";
        this.screenDiv.style.left = "0";
        this.screenDiv.style.top = "0";
        this.screenDiv.style.zIndex = "100";
        
        //iframe打底，ie6下遮盖住下拉框
//        <iframe style="position: absolute; z-index: -1; width: 630px; height: 400px;" frameborder="0 " src="about:blank "></iframe>
        var iframeObj = document.createElement("iframe");
        iframeObj.src = "about:blank ";
        iframeObj.style.position = "absolute";
        iframeObj.style.zIndex = "-1";
        iframeObj.style.left = "0";
        iframeObj.style.top = "0";
        iframeObj.style.width = "100%";
        iframeObj.style.height = "100%";
//        iframeObj.allowTransparency = "true";设置iframe透明
        iframeObj.style.filter = "chroma(color=#ffffff)";
        this.screenDiv.appendChild(iframeObj);
        
        //div透明层
        var divObj = document.createElement("div");
        divObj.style.width = "100%";
        divObj.style.height = "100%";
        divObj.style.background = "Gray";
        if(!isNaN(divObj.style.opacity)){
            divObj.style.opacity = 0.3;
        }else{
            divObj.style.filter = "alpha(opacity=30)";
        }
        this.screenDiv.appendChild(divObj);
        
        document.body.appendChild(this.screenDiv);
    }
    
    
    //初始化遮盖层
    this.initScreenDiv = function(){
        if(this.screenDiv){
            this.screenDiv.style.width = 
                document.body.clientWidth > document.body.scrollWidth ? document.body.clientWidth + "px" : document.body.scrollWidth + "px";
            this.screenDiv.style.height = 
                document.body.clientHeight > window.screen.availHeight ? document.body.clientHeight + "px" : window.screen.availHeight + "px";
        }
    }
    
    //设置弹出层居中显示
    //@param
    //oWidth  弹出层的宽
    //oHeight 弹出层的高
    this.setCenter = function(oWidth, oHeight){
        if(oWidth && oHeight){
            var doc = (document.compatMode && document.compatMode == 'CSS1Compat') ? document.documentElement : document.body;
            this.width = oWidth;
            this.height = oHeight;
            this.alertDiv.style.left = doc.scrollLeft + (doc.clientWidth - this.width) / 2 + "px";
            this.alertDiv.style.top = doc.scrollTop + (doc.clientHeight - this.height) / 2 + "px";
        }
    }
    
    //设置弹出层定位
    //@param
    //oWidth  弹出层的宽
    //oHeight 弹出层的高
    this.setLeftTop = function(oLeft, oTop){
        if(this.alertDiv.style.display != "none"){
            if(oLeft && oTop){
                this.alertDiv.style.left = oLeft;
                this.alertDiv.style.top = oTop;
            }else{
                this.alertDiv.style.left = 0;
                this.alertDiv.style.top = 0;
            }
        }
    }
    
    //打开弹出层
    this.open = function(){
        if(this.alertDiv && this.screenDiv){
            this.initScreenDiv();
            this.alertDiv.style.display = "block";
            this.screenDiv.style.display = "block";
            this.isVisible = true;
        }
    }
    
    //关闭弹出层
    this.close = function(){
        if(this.alertDiv && this.screenDiv){
            this.alertDiv.style.display = "none";
            this.screenDiv.style.display = "none";
            this.isVisible = false;
        }
    }
    
    //窗口尺寸变化（在页面窗口window.onresize事件中调用）
    this.resize = function(){
        if(this.isVisible){
            this.initScreenDiv();
//            this.setCenter();
        }
    }
}

