var overlay_callbacks=new Array();
var login_callbacks=new Array();
var overlay_active=new Array();
function SetOverlayCloseCallback(fn)
{
 	var id='overlay';
	overlay_callbacks[id]=fn;
}

var login_callbacks=new Array();
function SetLoginCallback(fn)
{
 	var id='overlay';
	login_callbacks[id]=fn;
}

function CheckLogin()
{
 	var id='overlay';
	AjaxRequest.post({
		'url':'/ajax/user_loggedin.php',
		'onComplete':function(request){if((request.responseText=='1') && login_callbacks[id]){login_callbacks[id]();}}
	});
}
function CallOverlayCloseCallback(id)
{
	if(overlay_callbacks[id])	
		overlay_callbacks[id]();
}

function LoadOverlay(thehtml)
{
 	var id='overlay';
	jQuery('#'+id).html(thehtml);	
	if(!overlay_active[id])
		ShowOverlay(id,true);
}

function ShowOverlay(id,show)
{
  	if(id && document.getElementById(id))
  	{
  	  	var pbody=jQuery('BODY');
  	  	var overlay=jQuery('#'+id+'_container');
		var obackground=jQuery('#'+id+'_container DIV.page_overlay_background');
		var ocontent=jQuery('#'+id+'_container DIV.page_overlay_contents');
  	  	if(show)
  	  	{
  	  	 	pageheight=pbody.height();
  	  	 	pagewidth=pbody.width();
  	  	  	var sz=getPageSize();
  	  	  	var scrl=getPageScrollPos();


  	  	  	if(sz[1]>pageheight) 	pageheight=sz[1];
// 	  	  	if(sz[0]>pagewidth) 	pagewidth=sz[0];
  	  	   	
				     	  	  	  	  
			overlay.css({'opacity':'1.00','display':'block','width':sz[0]+'px','height':pageheight+'px'});
			obackground.css({'opacity':'0.01','display':'block','width':sz[0]+'px','height':pageheight+'px'});
			ocontent.css({'opacity':'0.01','display':'block'});
			
			var contentleft=(pagewidth-ocontent.width())/2;			
			ocontent.css({'left':contentleft+'px'});

			//center content.
			var ctop=(sz[3]-ocontent.height())/2;
			if(ctop<0)	
				ctop=10;			
			ctop+=scrl[1];
			if(ctop+ocontent.height()>sz[1])
				ctop=(sz[1]-ocontent.height())-30;
			if(ctop<0)	
				ctop=10;			
			ocontent.css({'top':ctop+'px'});

			obackground.fadeTo(750,0.75);
			ocontent.fadeTo(1000,1.00);

			overlay_active[id]=true;
		}
		else
  	  	{
//			overlay.css({'display':'none'});
//			overlay.fadeOut(750,function(){overlay.css({'display':'none'});});

			obackground.fadeOut(1000,function(){overlay.css({'display':'none'});});
			ocontent.fadeOut(750,function(){overlay.css({'display':'none'});});

			overlay_active[id]=false;
		}
  	}
  	return false;
}


function getPageSize(){

var xScroll, yScroll;
var scroll=getPageScroll();
xScroll=scroll[0];
yScroll=scroll[1];

var windowWidth, windowHeight;
if (self.innerHeight) { // all except Explorer
windowWidth = self.innerWidth;
windowHeight = self.innerHeight;
} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
windowWidth = document.documentElement.clientWidth;
windowHeight = document.documentElement.clientHeight;
} else if (document.body) { // other Explorers
windowWidth = document.body.clientWidth;
windowHeight = document.body.clientHeight;
}

// for small pages with total height less then height of the viewport
if(yScroll < windowHeight){
pageHeight = windowHeight;
} else {
pageHeight = yScroll;
}

// for small pages with total width less then width of the viewport
if(xScroll < windowWidth){
pageWidth = windowWidth;
} else {
pageWidth = xScroll;
}


arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
return arrayPageSize;
}


function getPageScroll()
{ 
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) 
	{
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} 
	else if (document.body.scrollHeight > document.body.offsetHeight)
	{ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} 
	else if (document.documentElement && document.documentElement.scrollHeight > document.documentElement.offsetHeight)
	{ // Explorer 6 strict mode
		xScroll = document.documentElement.scrollWidth;
		yScroll = document.documentElement.scrollHeight;
	} 
	else 
	{ // Explorer Mac...would also work in Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	arrayPageScroll = new Array(xScroll,yScroll);
	return arrayPageScroll;	
}

function getPageScrollPos()
{
	arrayPageScroll = new Array(f_scrollLeft(),f_scrollTop());
	return arrayPageScroll;	

}
function f_scrollLeft() 
{
	return f_filterResults
	(
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() 
{
	return f_filterResults(
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body)
{
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}



