/*
RQPopup.js
Michael Zancan 
© 2Roqs 2007
All rights reserved

requires jquery.js and jquery.dimensions.js


*/

var RQPopup = 
{

	m_bg				: 0,
	m_popupObj			: 0,
	m_loaderObj			: null,

	m_bgColor			: "000000",
	m_popupColor		: null,
	m_padding			: 7,
	m_bgAlpha			: 0.8,
	m_imgLoaded			: false,
	m_bgReady			: false,
	m_bodyHeight		: 100,
	m_scrollTop			: 0,
	m_windowHeight		: 0,
	loaderInfo			: null,
	M_showBg			: function(m)
	{	
		if(m)
		{

			this.m_scrollTop = $(window).scrollTop();
			this.m_windowHeight = $(window).height();

			if( !this.m_popupObj)
			{
				this.loaderInfo="<img src='Data/Img/cssPopupLoader.gif' /><div>"+sStrLoading+"</div>";
				
				this.m_popupObj = document.createElement("div");
				$(this.m_popupObj).prependTo($(document.body));
				$(this.m_popupObj).addClass("popupBg");
				this.m_bodyHeight = Math.max( Math.max($(document).height(),$("body").height()), $(window).height());
				
				$(this.m_popupObj).css({position:"absolute", display:"none",opacity:""+this.m_bgAlpha,width:"100%",height:this.m_bodyHeight,background:"#"+this.m_bgColor,zIndex:"2002",top:"0",left:"0"});
				$(this.m_popupObj).attr("id",'CssPopupDiv');
				$(this.m_popupObj).fadeIn("fast",RQDelegate(this,this.M_cbShow));
			}
		}
		else
		{

			if(this.m_popupObj)
			{	
				$(this.m_obj).hide();
				$(this.m_popupObj).fadeOut("fast",RQDelegate(this,this.M_cbClosed));
				$(window).resize(null);
				this.m_bgReady	=false;
			}

		}
	}
	,
	M_cbShow : function()
	{
		this.m_bgReady=true;
		this.M_showImg();

	}
	,
	M_cbClosed :function()
	{	$(this.m_obj).remove();
		$(this.m_popupObj).remove();
		if(this.loaderInfo)
			$(this.m_loaderObj).remove();
		this.m_obj = null;
		this.m_popupObj = null;
		this.m_loaderObj = null;
		// Specific to SudManagement: unhide sphere slideshow
		$("#slideshow").show();

	}
	,
	M_cbContentLoaded :function()
	{	this.m_imgLoaded=true;
		this.M_showImg();
	}
	
	,
	M_showImg :function()
	{	if(this.m_imgLoaded && this.m_bgReady)
		{
			if(this.loaderInfo)
				$("#popupLoader").fadeOut('slow',RQDelegate(this,this.M_cbShowImg));
			else
				this.M_cbShowImg();

		}

	}
	,
	M_cbShowImg : function()
	{
		if(this.m_imgLoaded && this.m_bgReady)
		{	
			this.M_centerObj("popupFrame");
			window.setTimeout(RQDelegate(this,this.M_showImgAndFade),1);
			$(window).resize( RQDelegate(this,this.M_centerPopup)).scroll( RQDelegate(this,this.M_centerPopup));
	
		}
	}
	,
	M_showImgAndFade : function()
	{
		this.M_centerPopup();
		$("#popupImg").addClass("CSSPopupImage").fadeIn('fast');
		$(".popupTitle").slideDown('fast');
		$("#popupFrame").addClass("CSSPopupFrame");

	}
	,
	M_centerPopup :function()
	{	
		if( this.m_obj)
		{
			//this.m_bodyHeight = Math.max( Math.max($(document).height(),$("body").height()), $(window).height());
			this.m_scrollTop = $(window).scrollTop();
			this.m_windowHeight = $(window).height();

			var pads2=2*this.m_padding;
			var imgW=$("#popupImg").width();
			var imgH=$("#popupImg").height();
			var img= getImg('popupImg');
			if( img )
			{	imgW = Math.max(img.width,imgW);
				imgH = Math.max(img.height,imgH);
			}
			imgW+=pads2
			imgH+=pads2
			var x=parseInt(0.5*($(document).width()-imgW));
			var oy =this.m_scrollTop;// $(window).scrollTop();
			var y=oy+parseInt(0.5*(this.m_windowHeight-imgH));

			$("#popupFrame").css("top",y).css("left",x);
		}

	}
	,
	M_centerObj : function(id)
	{	
		//this.m_bodyHeight = Math.max( Math.max($(document).height(),$("body").height()), $(window).height());
		var imgW=$("#"+id).width();
		var imgH=$("#"+id).height();
		var x=parseInt(0.5*($(document).width()-imgW));
		var oy = this.m_scrollTop;//$(window).scrollTop();
		var y=oy+parseInt(0.5*(this.m_windowHeight-imgH));

		$("#"+id).css({top:y,left:x});

	}

	,
	M_popup : function(url,title)
	{
		this.M_showBg(true);

		//image?
		if( RegExp("(jpe?g|gif|png)$", "i").test(url))
		{
			this.m_imgLoaded = false;
			
			this.m_obj = document.createElement("div");

			// loader 
			if(this.loaderInfo)
			{
				this.m_loaderObj = document.createElement("div");
				$(this.m_loaderObj).attr('id','popupLoader').css({position:"absolute",display:"block",top:"0",left:"0",margin:"0",padding:""+this.m_padding+"px",zIndex:"2200"}).html(this.loaderInfo);
				$(this.m_popupObj).after(this.m_loaderObj);
				this.M_centerObj('popupLoader');
			}


			// position div
			//$(this.m_popupObj).after(this.m_obj);
			$(this.m_obj).prependTo($(document.body));	// alternative placement. Works too

			$(this.m_obj).attr('id','popupFrame').css({position:"absolute",display:"block",top:0,left:0,margin:"0",padding:""+this.m_padding,zIndex:"2201"}).html("<img style=\"display:none;\" id='popupImg' border=\"0\"/>");


			// title
			if( title)
				$("#popupImg").after("<div class=\"popupTitle\" style=\"display:none;\">"+title+"</div>");
			
			$("#popupImg")
			.click(RQDelegate(this,this.M_close))
			.load(RQDelegate(this,this.M_cbContentLoaded))
			.error( RQDelegate(this,this.M_showBg) )			// showBg will close background
			.attr('src',url);



		}
		else
			this.M_showBg(false);

	}

	,
	M_close : function()
	{
		this.M_showBg(false);

	}
	,
	M_cbAjaxResponse : function(message,isErr)
	{
		if( !isErr)
		{
			var o =getObj("CssPopupPageContent");
			if( o)
			{
				o.innerHTML = message;
			}
		}
	}
}
