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

requires jquery.js and jquery.dimensions.js


*/

function CssPopup()
{
	this.m_bg = 0;
	this.m_popupObj=0;
	this.m_loaderObj	=null;

	this.m_bgColor		= "000000";
	this.m_popupColor	= null; //"F5F5F5";
	this.m_padding		= 7;
	this.m_bgAlpha		= 0.6;
	this.m_imgLoaded	= false;
	this.m_bgReady		= false;
	this.m_bodyHeight	= 100;
	this.loaderInfo		= null;
	this.M_showBg = function(m)
	{	
		if(m)
		{
			this.loaderInfo="<img src='Data/Img/cssPopupLoader.gif' />&nbsp;"+sStrLoading	
			if( !this.m_popupObj)
			{
				// Specific to SudManagement: hide sphere slideshow
				$("#slideshow").hide();
				
				this.m_popupObj = document.createElement("div");
				$(this.m_popupObj).prependTo($(document.body));
				$(this.m_popupObj).addClass("popupBg");
				this.m_bodyHeight = Math.max($(document).height(),$("body").height());
				$(this.m_popupObj).css({position:"absolute", display:"none",opacity:""+this.m_bgAlpha,width:"100%",height:this.m_bodyHeight,background:"#"+this.m_bgColor,zIndex:"2",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));
				this.m_bgReady	=false;
			}

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

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

	}

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

		}

	}
	this.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));
	
		}
	}
	this.M_showImgAndFade =function()
	{
		this.M_centerPopup();
		$("#popupImg").addClass("CSSPopupImage").fadeIn('fast');
		$(".popupTitle").slideDown('fast');
		$("#popupFrame").addClass("CSSPopupFrame");

	}
	this.M_centerPopup=function()
	{
		if( this.m_obj)
		{
			this.m_bodyHeight = Math.max($(document).height(),$("body").height());
			var pads2=2*this.m_padding;
			var imgW=$("#popupImg").width();
			var imgH=$("#popupImg").height();
			if( img = getImg('popupImg'))
			{	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 = $(document).scrollTop();
			var y=oy+parseInt(0.5*($(window).height()-imgH));

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

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

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

	}

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

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


			// position div
			$(this.m_popupObj).after(this.m_obj);

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



		}
		else
			this.M_showBg(false);

	}


	this.M_close = function()
	{
		this.M_showBg(false);

	}

	this.M_cbAjaxResponse = function(message,isErr)
	{
		if( !isErr)
		{
			var o =getObj("CssPopupPageContent");
			if( o)
			{
				o.innerHTML = message;
			}
		}
	}
}
// init global var
var CssPopupObject = new CssPopup();

//  CSSPopup
/// passing "center" as x centers the div
function CSSPopup(title,url)
{
	if(CssPopupObject)
	{	
		CssPopupObject.M_popup(url,title);
	}
}
function CSSPopupClose()
{
	if(CssPopupObject)
		CssPopupObject.M_close();

}

