﻿//首先得导入prototype.js，timer.js(用于显示消息弹出框)
//可以弹出嵌套iframe,弹出普通的html内容,弹出确认框,弹出alert对话框,弹出消息框，均为模式对话框


//获取浏览器的一些属性
function getValue(str)
{
    var nResult=0;
    switch(str)
    {
        case "scrollTop":
            if(typeof(window.pageYOffset)!='undefined')
            {
                nResult=window.pageYOffset;
            }
            else if(typeof(document.compatMode)!='undefined'&&document.compatMode!='BackCompat')
            {
                nResult=document.documentElement.scrollTop;
            }
            else if(typeof(document.body)!='undefined')
            {
                nResult=document.body.scrollTop;
            }
            break;
        case "clientWidth":
            if(typeof(document.compatMode)!='undefined'&&document.compatMode!='BackCompat')
            {
                nResult=document.documentElement.clientWidth;
            }
            else if(typeof(document.body)!='undefined')
            {
                nResult=document.body.clientWidth;
            }
            break;
        case "clientHeight":
            if(typeof(document.compatMode)!='undefined'&&document.compatMode!='BackCompat')
            {
                nResult=document.documentElement.clientHeight;
            }
            else if(typeof(document.body)!='undefined')
            {
                nResult=document.body.clientHeight;
            }
            break;
        case "scrollWidth":
            if(typeof(document.compatMode)!='undefined'&&document.compatMode!='BackCompat')
            {
                nResult=document.documentElement.scrollWidth;
            }
            else if(typeof(document.body)!='undefined')
            {
                nResult=document.body.scrollWidth;
            }
            break;
        case "scrollHeight":
            if(typeof(document.compatMode)!='undefined'&&document.compatMode!='BackCompat')
            {
                nResult=document.documentElement.scrollHeight;
            }
            else if(typeof(document.body)!='undefined')
            {
                nResult=document.body.scrollHeight;
            }
            break;
        default:break;
    }
    return nResult;
};

function space(flag)
{
    if(flag=="begin")
    {
        var ele=document.getElementById("ft");
        if(typeof(ele)!="undefined"&&ele!=null)
            ele.id="ft_popup";
        ele=document.getElementById("usrbar");
        if(typeof(ele)!="undefined"&&ele!=null)
            ele.id="usrbar_popup";
    }
    else if(flag=="end")
    {
        var ele=document.getElementById("ft_popup");
        if(typeof(ele)!="undefined"&&ele!=null)
            ele.id="ft";
        ele=document.getElementById("usrbar_popup");
        if(typeof(ele)!="undefined"&&ele!=null)
            ele.id="usrbar";
    };
};

//弹出类
var Popup=Class.create();
Popup.prototype=
{
    iframeIdName:'ifr_popup',
    initialize:function(config)
    {
        this.config=Object.extend({contentType:1,
                                    isHaveTitle:true,
                                    scrollType:'no',
                                    isBackgroundCanClick:false,
                                    isSupportDraging:true,
                                    isShowShadow:true,
                                    isReloadOnClose:true,
                                    width:400,
                                    height:300,
                                    autoClose:false,
                                    closeCountDown:3000
                                  },
                                  config||{});
        this.info={shadowWidth:4,
                    title:'',
                    contentUrl:'',
                    contentHtml:'',
                    callBack:null,
                    parameter:null,
                    confirmCon:'',
                    alertCon:'',
                    msg:'',
                    type:'normal',
                    someHiddenTag:'select,object,embed',
                    someHiddenEle:'',
                    overlay:0,
                    coverOpacity:40
                 };
        this.color={cColor:'#000000',
                    bColor:'#FFFFFF',
                    tColor:'#B4D333',
                    wColor:'#FFFFFF'
                    };
        this.dropClass=null;
        this.timer=null;
        this.someToHidden=[];
        if(!this.config.isHaveTitle)
        {
            this.config.isSupportDraging=false;
        }
        this.iniBuild();
    },
    setContent:function(arrt,val)
    {
        if(val!='')
        {
            switch(arrt)
            {
                case 'width':
                    this.config.width=val;
                    break;
                case 'height':
                    this.config.height=val;
                    break;
                case 'title':
                    this.info.title=val;
                    break;
                case 'contentUrl':
                    this.info.contentUrl=val;
                    break;
                case 'contentHtml':
                    this.info.contentHtml=val;
                    break;
                case 'callBack':
                    this.info.callBack=val;
                    break;
                case 'parameter':
                    this.info.parameter=val;
                    break;
                case 'confirmCon':
                    this.info.confirmCon=val;
                    break;
                case 'alertCon':
                    this.info.alertCon=val;
                    break;
                case 'someHiddenTag':
                    this.info.someHiddenTag=val;
                    break;
                case 'someHiddenEle':
                    this.info.someHiddenEle=val;
                    break;
                case 'overlay':
                    this.info.overlay=val;
                    break;
                case 'contentMsg':
                    this.info.msg=val;
                    break;
                case 'type':
                    this.info.type=val;
                    break;
                default:
                    break;
            };
        };
    },
    iniBuild:function()
    {
        $('dialogCase')?$('dialogCase').parentNode.removeChild($('dialogCase')):function(){};
        var oDiv=document.createElement('span');
        oDiv.id='dialogCase';
        document.body.appendChild(oDiv);
    },
    build:function()
    {
        var baseZIndex=10001+this.info.overlay*10;
        var showZIndex=baseZIndex+2;
        this.iframeIdName='ifr_popup'+this.info.overlay;
        var path='/images/images/dialogclose.gif';
        var close='<input type="image" id="dialogBoxClose" src="'+path+'" border="0" width="16" height="16" align="absmiddle" title="关闭"/>';
        var cB='filter:alpha(opacity='+this.info.coverOpacity+');opacity:'+this.info.coverOpacity/100+';';
        var cover='<div id="dialogBoxBG" style="position:absolute;top:0px;left:0px;width:100%;height:100%;z-index:'+baseZIndex+';'+cB+'background-color:'+this.color.cColor+';display:none;"></div>';
        var mainBox='<div id="dialogBox" style="border:1px solid '+this.color.tColor+';display:none;z-index:'+showZIndex+';position:relative;width:'+this.config.width+'px;"><table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="'+this.color.bColor+'">';
        if(this.config.isHaveTitle)
        {
            mainBox+='<tr height="24" bgcolor="'+this.color.tColor+'"><td><table style="-moz-user-select:none;height:24px;" width="100%" border="0" cellpadding="0" cellspacing="0" ><tr>'+'<td width="6" height="24"></td><td id="dialogBoxTitle" style="color:'+this.color.wColor+';font-size:14px;font-weight:bold;">'+this.info.title+'&nbsp;</td>'+'<td id="dialogClose" width="20" align="right" valign="middle">'+close+'</td><td width="6"></td></tr></table></td></tr>'
        }
        else
        {
            mainBox+='<tr height="10"><td align="right">'+close+'</td></tr>'
        };
        mainBox+='<tr style="height:'+this.config.height+'px" valign="top"><td id="dialogBody" style="position:relative;"></td></tr></table></div>'+'<div id="dialogBoxShadow" style="display:none;z-index:'+baseZIndex+';"></div>';
        if(!this.config.isBackgroundCanClick)
        {
            $('dialogCase').innerHTML=cover+mainBox;
            this.setBackgroundSize();
            window.onresize=this.setBackgroundSize;
        }
        else
        {
            $('dialogCase').innerHTML=mainBox;
        }
        Event.observe($('dialogBoxClose'),
                "click",
                this.reset.bindAsEventListener(this),
                false);
        if(this.config.isSupportDraging)
        {
            dropClass=new Dragdrop(this.config.width,this.config.height,this.info.shadowWidth,this.config.isSupportDraging,this.config.contentType);
            $("dialogBoxTitle").style.cursor="move";
        };
        if(this.config.autoClose)
        {
            this.timer=new Timer(this);
            this.timer.setTimeout('reset',this.config.closeCountDown);
        };
        this.lastBuild()
    },
    setBackgroundSize:function()
    {
        var getValueWidth;
        var getMaxValueWidth=[getValue("clientWidth"),getValue("scrollWidth")];
        getValueWidth=eval("Math.max("+getMaxValueWidth.toString()+")");
        $('dialogBoxBG').style.width=getValueWidth;
        var getValueHeight;
        var getMaxValueHeight=[getValue("clientHeight"),getValue("scrollHeight")];
        getValueHeight=eval("Math.max("+getMaxValueHeight.toString()+")");
        $('dialogBoxBG').style.height=getValueHeight;
    },
    lastBuild:function()
    {
        var confirm='<div style="width:100%;height:100%;text-align:center;"><div style="margin:20px 20px 0 20px;font-size:14px;line-height:16px;color:#000000;">'+this.info.confirmCon+'</div><div style="margin:20px;"><input id="dialogOk" type="button" value=" 确定 "/>&nbsp;<input id="dialogCancel" type="button" value=" 取消 "/></div></div>';
        var alert='<div style="width:100%;height:100%;text-align:center;"><div style="margin:20px 20px 0 20px;font-size:14px;line-height:16px;color:#000000;">'+this.info.alertCon+'</div><div style="margin:20px;"><input id="dialogYES" type="button" value=" 确定 "/></div></div>';
        var failureMsg = '<div style="width:100%;height:100%;"><table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0"><tr><td vargin="middle" width="50" align="right"><img src="/images/images/failure.gif" width="18" height="18" border="0"></td><td><div style="margin:2px 20px 2px 20px;font-size:14px;line-height:16px;color:#000000;">'+this.info.msg+'</div></td><tr></table></div>';
        var succeedMsg = '<div style="width:100%;height:100%;"><table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0"><tr><td vargin="middle" width="50" align="right"><img src="/images/images/succeed.gif" width="18" height="18" border="0"></td><td><div style="margin:2px 20px 2px 20px;font-size:14px;line-height:16px;color:#000000;">'+this.info.msg+'</div></td><tr></table></div>';;
        var normalMsg = '<div style="width:100%;height:100%;text-align:center;"><div style="margin:20px 20px 20px 20px;font-size:14px;line-height:16px;color:#000000;">'+this.info.msg+'</div></div>';;
        var baseZIndex=10001+this.info.overlay*10;
        var coverIfZIndex=baseZIndex+4;
        if(this.config.contentType==1)
        {
            var openIframe="<iframe width='100%' style='height:"+this.config.height+"px' name='"+this.iframeIdName+"' id='"+this.iframeIdName+"' src='"+this.info.contentUrl+"' frameborder='0' scrolling='"+this.config.scrollType+"'></iframe>";
            var coverIframe="<div id='iframeBG' style='position:absolute;top:0px;left:0px;width:1px;height:1px;z-index:"+coverIfZIndex+";filter: alpha(opacity=00);opacity:0.00;background-color:#ffffff;'><div>";
            $("dialogBody").innerHTML=openIframe+coverIframe;
        }
        else if(this.config.contentType==2)
        {
            $("dialogBody").innerHTML=this.info.contentHtml;
        }
        else if(this.config.contentType==3)
        {
            $("dialogBody").innerHTML=confirm;
            Event.observe($('dialogOk'),"click",this.forCallback.bindAsEventListener(this),false);
            Event.observe($('dialogCancel'),"click",this.close.bindAsEventListener(this),false);
        }
        else if(this.config.contentType==4)
        {
            $("dialogBody").innerHTML=alert;
            Event.observe($('dialogYES'),"click",this.close.bindAsEventListener(this),false);
        }
        else if(this.config.contentType==5)
        {
            if(this.info.type=='succeed')
            {
                 $("dialogBody").innerHTML = succeedMsg;
            }
            else if(this.info.type=='failure')
            {
                $("dialogBody").innerHTML = failureMsg;
            }
            else
            {
                $("dialogBody").innerHTML = normalMsg;
            };
        };
        
    },
    reBuild:function()
    {
        $('dialogBody').height=$('dialogBody').clientHeight;
        this.lastBuild()
    },
    show:function()
    {
        this.hiddenSome();
        this.middle();
        if(this.config.isShowShadow)this.shadow()
    },
    forCallback:function()
    {
        return this.info.callBack(this.info.parameter)
    },
    shadow:function()
    {
        var oShadow=$('dialogBoxShadow');
        var oDialog=$('dialogBox');
        oShadow['style']['position']="absolute";
        oShadow['style']['background']="#000";
        oShadow['style']['display']="";
        oShadow['style']['opacity']="0.2";
        oShadow['style']['filter']="alpha(opacity=20)";
        oShadow['style']['top']=oDialog.offsetTop+this.info.shadowWidth+"px";
        oShadow['style']['left']=oDialog.offsetLeft+this.info.shadowWidth+"px";
        oShadow['style']['width']=oDialog.offsetWidth+"px";
        oShadow['style']['height']=oDialog.offsetHeight+"px";
    },
    middle:function()
    {
        if(!this.config.isBackgroundCanClick)
            $('dialogBoxBG').style.display='';
        var oDialog=$('dialogBox');
        oDialog['style']['position']="absolute";
        oDialog['style']['display']='';
        var sClientWidth=getValue("clientWidth");
        var sClientHeight=getValue("clientHeight");
        var sScrollTop=getValue("scrollTop");
        var sleft=(sClientWidth/2)-(oDialog.offsetWidth/2);
        var iTop=(sClientHeight/2+sScrollTop)-(oDialog.offsetHeight/2);
        var sTop=iTop>0?iTop:(sClientHeight/2+sScrollTop)-(oDialog.offsetHeight/2);
        if(sTop<1)
            sTop="20";
        if(sleft<1)
            sleft="20";
        oDialog['style']['left']=sleft+"px";
        oDialog['style']['top']=sTop+"px";
    },
    reset:function()
    {
        if(this.config.isReloadOnClose)
        {
            top.location.reload();
            //window.location.href = window.location.href;
        };
        this.close();
    },
    close:function()
    {
        $('dialogBox').style.display='none';
        if(!this.config.isBackgroundCanClick)
            $('dialogBoxBG').style.display='none';
        if(this.config.isShowShadow)
            $('dialogBoxShadow').style.display='none';
        $('dialogBody').innerHTML='';
        this.showSome();
    },
    hiddenSome:function()
    {
        var tag=this.info.someHiddenTag.split(",");
        if(tag.length==1&&tag[0]=="")
        {
            tag.length=0;
        }
        for(var j=0;j<tag.length;j++)
        {
            this.hiddenTag(tag[j]);
        };
        var ids=this.info.someHiddenEle.split(",");
        if(ids.length==1&&ids[0]=="")
            ids.length=0;
        for(var j=0;j<ids.length;j++)
        {
            this.hiddenEle(ids[j]);
        };
        space("begin");
    },
    hiddenTag:function(tagName)
    {
        var ele=document.getElementsByTagName(tagName);
        if(ele!=null)
        {
            for(var j=0;j<ele.length;j++)
            {
                if(ele[j].style.display!="none"&&ele[j].style.visibility!='hidden')
                {
                    ele[j].style.visibility='hidden';
                    this.someToHidden.push(ele[j]);
                };
            };
        };
    },
    hiddenEle:function(id)
    {
        var ele=document.getElementById(id);
        if(typeof(ele)!="undefined"&&ele!=null)
        {
            ele.style.visibility='hidden';
            this.someToHidden.push(ele);
        }
    },
    showSome:function()
    {
        for(var j=0;j<this.someToHidden.length;j++)
        {
            this.someToHidden[j].style.visibility='visible';
        };
        space("end");
    }
};
var Dragdrop=Class.create();
Dragdrop.prototype=
{
    initialize:function(width,height,shadowWidth,showShadow,contentType)
    {
        this.dragData=null;
        this.dragDataIn=null;
        this.backData=null;
        this.width=width;
        this.height=height;
        this.shadowWidth=shadowWidth;
        this.showShadow=showShadow;
        this.contentType=contentType;
        this.IsDraging=false;
        this.oObj=$('dialogBox');
        Event.observe($('dialogBoxTitle'),
        "mousedown",
        this.moveStart.bindAsEventListener(this),false);
    },
    moveStart:function(event)
    {
        this.IsDraging=true;
        if(this.contentType==1)
        {
            $("iframeBG").style.display="";
            $("iframeBG").style.width=this.width;
            $("iframeBG").style.height=this.height;
        };
        Event.observe(document,"mousemove",this.mousemove.bindAsEventListener(this),false);
        Event.observe(document,"mouseup",this.mouseup.bindAsEventListener(this),false);
        Event.observe(document,"selectstart",this.returnFalse,false);
        this.dragData={x:Event.pointerX(event),y:Event.pointerY(event)};
        this.backData={x:parseInt(this.oObj.style.left),y:parseInt(this.oObj.style.top)};
   },
   mousemove:function(event)
   {
        if(!this.IsDraging)return;
        var iLeft=Event.pointerX(event)-this.dragData["x"]+parseInt(this.oObj.style.left);
        var iTop=Event.pointerY(event)-this.dragData["y"]+parseInt(this.oObj.style.top);
        if(this.dragData["y"]<parseInt(this.oObj.style.top))
            iTop=iTop-12;
        else if(this.dragData["y"]>parseInt(this.oObj.style.top)+25)
            iTop=iTop+12;this.oObj.style.left=iLeft+"px";
        this.oObj.style.top=iTop+"px";
        if(this.showShadow)
        {
            $('dialogBoxShadow').style.left=iLeft+this.shadowWidth+"px";
            $('dialogBoxShadow').style.top=iTop+this.shadowWidth+"px";
        };
        this.dragData={x:Event.pointerX(event),y:Event.pointerY(event)};
        document.body.style.cursor="move";
    },
    mouseup:function(event)
    {
        if(!this.IsDraging)return;
        if(this.contentType==1)
            $("iframeBG").style.display="none";
        document.onmousemove=null;
        document.onmouseup=null;
        var mousX=Event.pointerX(event)-(document.documentElement.scrollLeft||document.body.scrollLeft);
        var mousY=Event.pointerY(event)-(document.documentElement.scrollTop||document.body.scrollTop);
        var oObjBottom;
        var oObjRight;
        if(this.showShadow)
        {
            oObjBottom=parseInt(this.oObj.style.top)+this.height+this.shadowWidth;
            oObjRight=parseInt(this.oObj.style.left)+this.width+this.shadowWidth;
        }
        else
        {
            oObjBottom=parseInt(this.oObj.style.top)+this.height;
            oObjRight=parseInt(this.oObj.style.left)+this.width;
        };
        if(mousX<1||mousY<1||mousX>document.body.clientWidth||mousY>document.body.clientHeight||parseInt(this.oObj.style.left)<0||parseInt(this.oObj.style.top)<0||oObjRight>document.body.clientWidth||oObjBottom>document.body.clientHeight)
        {
            this.oObj.style.left=this.backData["x"];
            this.oObj.style.top=this.backData["y"];
            if(this.showShadow)
            {
                $('dialogBoxShadow').style.left=this.backData.x+this.shadowWidth;
                $('dialogBoxShadow').style.top=this.backData.y+this.shadowWidth;
            };
        };
        this.IsDraging=false;
        document.body.style.cursor="";
        Event.stopObserving(document,"selectstart",this.returnFalse,false);
   },
   returnFalse:function(){return false;}
};

function g_ModelShowIframe(url,title,w,h)
{
    var g_pop=new Popup({contentType:1,scrollType:'yes',isReloadOnClose:false,width:w,height:h});
    g_pop.setContent("title",title);
    g_pop.setContent("contentUrl",url);
    g_pop.build();
    g_pop.show();
}

function g_ModelShowIframeReload(url,title,w,h)
{
    var g_pop=new Popup({contentType:1,scrollType:'yes',isReloadOnClose:true,width:w,height:h});
    g_pop.setContent("title",title);
    g_pop.setContent("contentUrl",url);
    g_pop.build();
    g_pop.show();
}

function g_ModelShowHtmlString(html,title,w,h)
{
    var g_pop=new Popup({ contentType:2,isReloadOnClose:false,width:340,height:300});
    g_pop.setContent("contentHtml",strHtml);
    g_pop.setContent("title","html字符串示例");
    g_pop.build();
    g_pop.show();
}

function g_ModelShowConfirm(msg,title,w,h,callback,parameters)
{
    var g_pop=new Popup({ contentType:3,isReloadOnClose:true,width:w,height:h});
    g_pop.setContent("title",title);
    g_pop.setContent("confirmCon",msg);
    g_pop.setContent("callBack",callback);
    g_pop.setContent("parameter",parameters);
    g_pop.build();
    g_pop.show();
}

function g_ModelShowAlert(msg,title,w,h)
{
    var g_pop=new Popup({ contentType:4,isReloadOnClose:false,width:w,height:h});
    g_pop.setContent("title",title);
    g_pop.setContent("alertCon",msg);
    g_pop.build();
    g_pop.show();
}

function g_ModelShowMessage(type,msg,title,w,h,ac,ccd)
{
    var g_pop=new Popup({ contentType:5,isReloadOnClose:false,width:w,height:h,autoClose:ac,closeCountDown:ccd});
    g_pop.setContent("title",title);
    g_pop.setContent("contentMsg",msg);
    g_pop.setContent("type",type);
    g_pop.build();
    g_pop.show();
}

function g_ShowMessage(type,msg,title,w,h,ac,ccd)
{
    var g_pop=new Popup({ contentType:5,isReloadOnClose:false,isBackgroundCanClick:true,width:w,height:h,autoClose:ac,closeCountDown:ccd});
    g_pop.setContent("title",title);
    g_pop.setContent("contentMsg",msg);
    g_pop.setContent("type",type);
    g_pop.build();
    g_pop.show();
}

function g_ShowMessageReLoad(type,msg,title,w,h,ac,ccd)
{
    var g_pop=new Popup({ contentType:5,isReloadOnClose:true,isBackgroundCanClick:true,width:w,height:h,autoClose:ac,closeCountDown:ccd});
    g_pop.setContent("title",title);
    g_pop.setContent("contentMsg",msg);
    g_pop.setContent("type",type);
    g_pop.build();
    g_pop.show();
}

function g_ModelShowHtmlStringReLoad(html,title,w,h)
{
    var g_pop=new Popup({ contentType:2,isReloadOnClose:true,width:w,height:h});
    g_pop.setContent("contentHtml",html);
    g_pop.setContent("title",title);
    g_pop.build();
    g_pop.show();
}

