
var PagePopup=
{
	Holder			:null,
	Back			:null,
	Front			:null,
	Frame			:null,
	Close			:null,
	Overlay			:null,
	Scroll			:{X:0,Y:0},
	Timer			:null,
	Width			:0,
	Height			:0,
	CurTime			:0,
	TimeFade		:0,
	TimeSize		:0,
	OverlayImage	:0,
	OnCloseHandler	:null,	
	Opacity			:0,
	
	documentElement:null,
	
	Open:function(Url,Width,Height,Background,Opacity,TimeFade,TimeSize,OverlayImage,OnCloseHandler)
	{
		this.documentElement = this.CreateElements(OverlayImage);
		
		this.Scroll.X=(document.body.parentNode.scrollLeft	?document.body.parentNode.scrollLeft:document.body.scrollLeft);
		this.Scroll.Y=(document.body.parentNode.scrollTop	?document.body.parentNode.scrollTop	:document.body.scrollTop);

		document.body.parentNode.style.overflow	=document.body.style.overflow	='hidden';
		document.body.parentNode.scrollLeft		=document.body.scrollLeft		='0';
		document.body.parentNode.scrollTop		=document.body.scrollTop		='0';

		this.Url			=Url;
		this.Width			=Width;
		this.Height			=Height;
		this.Opacity		=Opacity;
		this.TimeFade		=TimeFade;
		this.TimeSize		=TimeSize;
		this.OverlayImage	=OverlayImage;
		this.OnCloseHandler	=OnCloseHandler;
		this.CurTime		=0;

		this.Frame.src='about:blank';
		this.Close.style.display='none';
		this.Overlay.style.display='none';
		
		PagePopup.Update();
		
		this.Holder.style.display	='block';
		this.Front.style.display	='block';
		this.Back.style.background	=Background;

		this.Frame.style.width	=Math.round(this.Width)+'px';
		this.Frame.style.left	=-Math.round(this.Width/2)+'px';
	},
	
	ClosePage:function()
	{
		var Ratio	=1-((this.CurTime/this.TimeFade)>1?1:((this.CurTime/this.TimeFade)<0?0:(this.CurTime/this.TimeFade)));
		var Opacity	=Math.round(Ratio*this.Opacity);

		this.Back.style.opacity		=(Opacity/100);
		this.Back.style.mozOpacity	=(Opacity/100);
		this.Back.style.KhtmlOpacity=(Opacity/100);
		this.Back.style.filter		='alpha(opacity='+Opacity+')';

		this.CurTime+=50;

		if(this.CurTime<=this.TimeFade)
		{
			clearTimeout(this.Timer);
			setTimeout(function(){ PagePopup.ClosePage(); },50);
		}
		else
		{
			this.Holder.style.display				='none';
			this.Front.style.display				='none';
			this.Overlay.style.display				='none';
			document.body.parentNode.style.overflow	=document.body.style.overflow	='hidden';
			document.body.parentNode.scrollLeft		=document.body.scrollLeft		=this.Scroll.X;
			document.body.parentNode.scrollTop		=document.body.scrollTop		=this.Scroll.Y;
			document.body.removeChild(this.documentElement);
			
			if (this.OnCloseHandler) 
			{
				this.OnCloseHandler();
			}
		}
	},
	
	Update:function()
	{
		var Width	=0;
		var Height	=0;

		if(this.CurTime<=this.TimeFade)
		{
			var Ratio	=(this.CurTime/this.TimeFade)>1?1:((this.CurTime/this.TimeFade)<0?0:(this.CurTime/this.TimeFade));
			var Opacity	=Math.round(Ratio*this.Opacity);

			this.Back.style.opacity		=(Opacity/100);
			this.Back.style.mozOpacity	=(Opacity/100);
			this.Back.style.KhtmlOpacity=(Opacity/100);
			this.Back.style.filter		='alpha(opacity='+Opacity+')';
		}
		else
		{
			Width	=((this.CurTime-this.TimeFade)/this.TimeSize)*this.Width;
			Width	=Width<2?2:(Width>this.Width?this.Width:Width);
			Height	=((this.CurTime-this.TimeFade)/this.TimeSize)*this.Height;
			Height	=Height<2?2:(Height>this.Height?this.Height:Height);
		}

		if(this.CurTime>=this.TimeFade+this.TimeSize)
		{
			this.Close.style.display='block';
			this.Close.style.top	=-Math.round(this.Height/2)+'px';
			this.Close.style.left	=Math.round(this.Width/2-16)+'px';
			if(this.Overlay.style.background)
			{
				this.Overlay.style.display	='block';
				this.Overlay.style.top		=-Math.round(this.Height/2)+'px';
				this.Overlay.style.left		=-Math.round(this.Width/2)+'px';
				this.Overlay.style.width	=this.Width+'px';
				this.Overlay.style.height	=this.Height+'px';
			}
			setTimeout(function(){ PagePopup.Frame.contentWindow.location = PagePopup.Url; },50);
		}

		this.Frame.style.height	=Math.round(Height)+'px';
		this.Frame.style.top	=-Math.round(Height/2)+'px';

		this.CurTime+=50;

		if(this.CurTime<=this.TimeFade+this.TimeSize)
		{
			clearTimeout(this.Timer);
			setTimeout(function(){ PagePopup.Update(); },50);
		}
	},

	CreateElements:function(OverlayImage)
	{
		//if(!this.Holder && !this.Back && !this.Front && !this.Frame && !this.Overlay)
		//{
			this.Holder=document.createElement('div');
			this.Holder.style.display	='none';
			this.Holder.style.position	='absolute';
			this.Holder.style.top		=this.Holder.style.left		='0px';
			this.Holder.style.width		=this.Holder.style.height	='100%';
			this.Holder.style.float		='none';
			this.Holder.style.padding	=this.Holder.style.margin=this.Holder.style.border='0px';
			this.Holder.style.zIndex	='1000';

			this.Back=document.createElement('div');
			this.Back.style.position	='absolute';
			this.Back.style.top			=this.Back.style.left	='0px';
			this.Back.style.width		=this.Back.style.height	='100%';
			this.Back.style.float		='none';
			this.Back.style.padding		=this.Back.style.margin=this.Back.style.border='0px';
			this.Back.style.zIndex		='1';

			this.Front=document.createElement('div');
			this.Front.style.position	='absolute';
			this.Front.style.top		=this.Front.style.left	='50%';
			this.Front.style.width		=this.Front.style.height='1px';
			this.Front.style.float		='none';
			this.Front.style.padding	=this.Front.style.margin=this.Front.style.border='0px';
			this.Front.style.zIndex		='2';

			this.Frame=document.createElement('iframe');
			this.Frame.name				='pagepopupframe';
			this.Frame.style.position	='absolute';
			this.Frame.style.top		=this.Frame.style.left	='0px';
			this.Frame.style.width		=this.Frame.style.height='1px';
			this.Frame.frameBorder		='0';
			this.Frame.style.float		='none';
			this.Frame.style.padding	=this.Frame.style.margin='0px';
			this.Frame.style.background	='#FFFFFF';

			var TextHolder=document.createElement('div');
				TextHolder.style.position	='absolute';
				TextHolder.style.left		=TextHolder.style.bottom='0px';
				TextHolder.style.width		='18px';
				TextHolder.style.height		='18px';
				TextHolder.style.overflow	='hidden';
				TextHolder.style.border		='1px solid #555555';
				TextHolder.style.margin		='0px';
				TextHolder.style.padding	='0px 0px 0px 2px';
				TextHolder.style.background	='#FFFFFF';
				
			var Text=document.createElement('span');
				Text.style.position		='absolute';
				Text.style.left			=Text.style.bottom='2px';
				Text.style.color		='#555555';
				Text.style.fontFamily	='arial';
				Text.style.fontWeight	='800';
				Text.style.fontSize		='18px';
				Text.style.lineHeight	='11px';
				Text.style.background	='transparent';
				Text.innerHTML			='x';

			this.Close=document.createElement('div');
			this.Close.style.position	='absolute';
			this.Close.style.width		='16px';
			this.Close.style.height		='16px';
			this.Close.style.overflow	='hidden';
			this.Close.style.float		='none';
			this.Close.style.margin		='0px';
			this.Close.style.padding	='0px';
			this.Close.style.background	='#FFFFFF';
			this.Close.style.border		='0px';
			this.Close.style.cursor		='pointer';
			this.Close.onmouseover		=function(){ TextHolder.style.background='#CCCCCC'; };
			this.Close.onmouseout		=function(){ TextHolder.style.background='#FFFFFF'; };
			this.Close.onclick			=function()
			{ 
				PagePopup.CurTime=0;
				PagePopup.Front.style.display='none';
				PagePopup.ClosePage();
			};
			this.Overlay=document.createElement('div');
			this.Overlay.style.position		='absolute';
			this.Overlay.style.top			='0px';
			this.Overlay.style.left			='0px';
			this.Overlay.style.width		='0px';
			this.Overlay.style.height		='0px';
			this.Overlay.style.float		='none';
			this.Overlay.style.margin		='0px';
			this.Overlay.style.padding		='0px';
			this.Overlay.style.background	=OverlayImage?'transparent url('+OverlayImage+') no-repeat center':'';
			this.Overlay.style.border		='0px';
			this.Overlay.style.cursor		='pointer';
			this.Overlay.style.zIndex		='100000';
			this.Overlay.onclick			=function(){ PagePopup.Overlay.style.display='none'; PagePopup.Frame.src=PagePopup.Url; };

			TextHolder.appendChild(Text);
			this.Close.appendChild(TextHolder);
			this.Front.appendChild(this.Frame);
			this.Front.appendChild(this.Close);
			this.Front.appendChild(this.Overlay);
			this.Holder.appendChild(this.Back);
			this.Holder.appendChild(this.Front);
			
			return document.body.appendChild(this.Holder);
		//}
	}
}
window.PagePopup = PagePopup;
