var tsmsgbox=
{
    attop:false,
    atleft:false,
    isopen:false,
    bstyle:null,
    init:false,
    ibox:null,
    contentbox:null,
    passdata:null,
    content:null,
    onload:null,
    doinit:function()
    {
        if (this.init) return;
        tsfuncs.setevent("resize",window,this.resized);
        tsfuncs.setevent("scroll",window,this.scrolled);
        tsfuncs.setevent("load",window,this.winready);
        this.init=true;
    },
    close:function()
    {
        this.setcombos(false);
        this.bstyle=null;
        this.isopen=false;
        this.destroyIBOX();
        this.getBG().style.display="none";
    },
    setcombos:function(turnOff)
    {
        var c=document.body.getElementsByTagName("select"),e;
        for (var ct=0;ct<c.length;ct++)
        {
            e=c[ct];
            try
            {            
                if (turnOff) 
                {
                    e.ostate=e.ostate==undefined?e.style.visibility:e.ostate;
                }
                e.style.visibility=turnOff?"hidden":e.ostate;
                e.ostate=!turnOff?undefined:e.ostate;
            }
            catch(e){}
        }
    },
    popup:function(contentpage,w,h,pd)
    {
        w=w==undefined?500:w;
        h=h==undefined?400:h;
        if ((contentpage==undefined) || (this.isopen && this.bstyle!="popup")) this.close();
        if (contentpage==undefined) return;
        if (!this.isopen)
        {
            this.isopen=true;        
            this.bstyle="popup";
            this.createBG();
            this.createIBOX(w,h);
            this.ibox.innerHTML="<iframe name='tsmsgbox_content' onload='parent.tsmsgbox.assign()' id='tsmsgbox_content' src='' style='width:"+w+"px; height:"+h+"px; border:2px solid #777777;'>"
            this.contentbox=tsfuncs.geID("tsmsgbox_content");
            this.passdata=pd;
        }
        this.contentbox.src=contentpage;
        this.position();
        this.setcombos(true);
    },
    assign:function()
    {
    	this.content=this.contentbox.contentDocument?this.contentbox.contentDocument:this.contentbox.contentWindow;
    },
    alert:function(msg,clr,w,h)
    {
        w=w==undefined?400:w;
        h=h==undefined?50:h;
        clr=clr==undefined?"#770000":clr;
        if ((msg==undefined || msg=="") || (this.isopen && this.bstyle!="alert")) this.close();
        if (msg==undefined || msg=="") return;
        if (!this.isopen)
        {
            this.isopen=true;   
            this.bstyle="alert";     
            this.createBG();
            this.createIBOX(w,h,clr);
        }
        this.ibox.innerHTML=msg;
        this.position();
        this.setcombos(true);
    },
    destroyIBOX:function()
    {
        if (this.ibox!=null) 
        {
            if (tsfuncs.IE) this.ibox.removeNode(true); else this.ibox.parentNode.removeChild(this.ibox)
        }
        this.contentbox=null;
        this.ibox=null;
        this.attop=false;
        this.atleft=false;
    },
    createIBOX:function(w,h,clr)
    {
        this.destroyIBOX();
        var ib=document.createElement("div");
        ib.id="ts_msgbox_ib";
    	ib.style.backgroundColor=clr==undefined?"#FFFFFF":clr;
    	ib.style.position="absolute";
    	ib.style.borderWidth="3px";
    	ib.style.borderStyle="outset";
    	ib.style.borderColor="#FFFFFF";
    	ib.style.zIndex=3001;
    	ib.style.display="none";
    	ib.style.width=w+"px";
    	ib.style.height=h+"px";
    	ib.style.top="0px";
    	ib.style.left="0px";
        document.body.appendChild(ib);
        this.position();
        this.ibox=ib;
    	this.ibox.style.display="block";
    },
    getBG:function()
    {
        return tsfuncs.geID("ts_msgbox_bg");
    },
    winready:function()
    {
    },
    createIB:function()
    {
    },
    createBG:function()
    {
        var bg=this.getBG();
        if (!bg)
        {
            bg=document.createElement("div");
            bg.id="ts_msgbox_bg";
        	bg.style.backgroundColor="#000000";
        	bg.style.filter="alpha(opacity=30)";
        	bg.style.MozOpacity=.30;
        	bg.style.position="absolute";
        	bg.style.border="none";
        	bg.style.zIndex=3000;
        	this.sizeBG(bg);
        	bg.style.left="0px";
        	bg.style.top="0px";
            document.body.appendChild(bg);
        }
        else
        {
        	this.sizeBG(bg);
        }
       	bg.style.display="block";
        return bg;
    },
    sizeBG:function(bg)
    {
        if (!bg) bg=this.createBG();
        bg.style.height=(tsfuncs.getTH())+"px";
        bg.style.width=tsfuncs.getTW()+"px";
    },
    position:function()
    {
        if (this.ibox!=null)
        {
            var e=this.ibox;
            var w=tsfuncs.GetWidth(e);
            var h=tsfuncs.GetHeight(e);
            var vw=tsfuncs.getVW();
            var vh=tsfuncs.getVH();
            var st=tsfuncs.getST();
            var sl=tsfuncs.getSL();
            //alert(st+":"+h+":"+vh);
            var b=(vh/2)-(h/2);
            if (b<0)
            {
                b=this.attop?-1:0;
                this.attop=true;
            }
            else this.attop=false;
            if (b>=0) e.style.top=st+(b)+"px";
            b=(vw/2)-(w/2);
            if (b<0)
            {
                b=this.atleft?-1:0;
                this.atleft=true;
            }
            else this.atleft=false;
            if (b>=0) e.style.left=sl+(b)+"px";
        }
    },
    setsize:function()
    {
        this.sizeBG();
    },
    resized:function()
    {
        if (!tsmsgbox.isopen) return;
    	try
    	{
    	    tsmsgbox.setsize();
    	    tsmsgbox.position();
    	}
    	catch(e)
    	{
    	}
    },
    scrolled:function()
    {
        if (!tsmsgbox.isopen) return;
    	try
    	{
    	    tsmsgbox.position();
    	}
    	catch(e)
    	{
    	}
    }
}
tsmsgbox.doinit();
