﻿var modelDivInfo = {envelopDiv:null,divs:[]};
function ModelDiv(divId,maxWidth,maxHeight)
{
    this.div = document.getElementById(divId);
    this.maxWidth = maxWidth;
    this.maxHeight = maxHeight
    this.widthPixel = 70;
    this.heightPixel = 70;
    this.background = "black";
    this.opacity = 0.3;
}
ModelDiv.prototype.show = function(isEnlarge)
{
    if(modelDivInfo.envelopDiv!=null)
    {
        modelDivInfo.envelopDiv.style.zIndex = parseInt(modelDivInfo.envelopDiv.style.zIndex) + 2;
        this.div.style.zIndex = parseInt(this.getCurrentDiv().style.zIndex) + 2;
    }
    else
    {
        //背景层开始
        var bg = document.createElement("div");
        bg.style.position = "absolute";
        bg.style.left = 0;
        bg.style.top = 0;
        bg.style.background = this.background;
        bg.style.zIndex = 1000;
        bg.style.filter = "alpha(opacity="+this.opacity * 100+")";  //IE透明滤镜
        bg.style.opacity = this.opacity;                        //FF透明滤镜
        if(isIE) //IE
        {
            if(document.documentElement.scrollHeight<document.documentElement.clientHeight)
                bg.style.height = document.documentElement.clientHeight + "px";
            else
                bg.style.height = document.documentElement.scrollHeight + "px";
            bg.style.width = document.documentElement.scrollWidth + "px";       
            //IE中用来遮盖<select></select>
//            var iframe = document.createElement("iframe");
//            iframe.style.position = "absolute";
//            iframe.style.left = 0;
//            iframe.style.top = 0;
//            iframe.style.width = bg.style.width;
//            iframe.style.height = bg.style.height;
//            iframe.style.filter = "alpha(style=0,opacity=0)";  //IE中把iframe设置成通明，FF不需要
//            bg.appendChild(iframe);
            
        }
        else
        {
            bg.style.height = document.documentElement.scrollHeight + "px";
            bg.style.width = document.documentElement.scrollWidth + "px";       
        }
       //背景层结束
        
        document.body.appendChild(bg);
        modelDivInfo.envelopDiv = bg;
        this.div.style.zIndex = 1001;
    }
    modelDivInfo.divs.push(this.div);
    
    if(!isEnlarge)
    {
        this.div.style.width = this.maxWidth + "px";
        this.div.style.height = this.maxHeight + "px";   
        this.div.style.marginBottom = (-this.maxHeight/2) + "px";
        this.div.style.marginRight = (-this.maxWidth/2) + "px";
    }
    else
    {
        this.div.style.width = "1px";
        this.div.style.height = "1px";
        this.div.style.marginBottom = 0;
        this.div.style.marginRight = 0;
    }
    this.div.style.display = "block";
    this.div.style.position = "absolute";
    this.div.style.bottom = "50%";  //用bottom和marginBottom来定位DIV居中
    this.div.style.right = "50%";
    if(isIE)
        this.div.setActive();
    else
        this.div.blur();
    if(isEnlarge)
        setTimeout("enlargeDiv('"+this.div.id+"',"+this.maxWidth+","+this.maxHeight+","+this.widthPixel+","+this.heightPixel+")",1);
}
ModelDiv.prototype.getCurrentDiv = function()
{
    var currentDiv = null;
    if(modelDivInfo.divs.length != 0)
        currentDiv = modelDivInfo.divs[0];
    for(var i=1;i<modelDivInfo.divs.length;i++)
    {
        if(currentDiv.style.zIndex < modelDivInfo.divs[i].style.zIndex)
            currentDiv = modelDivInfo.divs[i];
    }
    return currentDiv;
}
ModelDiv.prototype.close = function()
{
    this.div.style.display = "none";
    modelDivInfo.divs.remove(this.div);
    if(this.getCurrentDiv()==null)
    {
        document.body.removeChild(modelDivInfo.envelopDiv);
        modelDivInfo.envelopDiv = null;
    }
    else
        modelDivInfo.envelopDiv.style.zIndex = parseInt(modelDivInfo.envelopDiv.style.zIndex) - 2;
}
function enlargeDiv(divID,maxWidth,maxHeight,widthPixel,heightPixel)
{
    var div = document.getElementById(divID);
    var width = parseInt(div.style.width);
    var height = parseInt(div.style.height);
    
    width = width + widthPixel;
    
    height = height + heightPixel;
    if(width>maxWidth)
        width = maxWidth;
    if(height>maxHeight)
        height = maxHeight;
    div.style.width = width + "px"; //放大DIV的宽度
    div.style.height = height + "px";
    div.style.marginBottom = (-height/2) + "px";  //把DIV向下边移动放大的宽度的一半，保持DIV在中间，并逐渐放大
    div.style.marginRight = (-width/2) + "px";
    if(width<maxWidth||height<maxHeight)
        setTimeout("enlargeDiv('"+divID+"',"+maxWidth+","+maxHeight+","+widthPixel+","+heightPixel+")",1);
}

