//////////////////
// FLOPUP STUFF //
//////////////////

function showFlopup(flopup_id, oCaller)
{
	var oBack = document.getElementById("flopup_back");
	if ( !oBack )
	{
		// If the back doesn't exist, create it
		var oBack = document.createElement("div");
		oBack.id = "flopup_back";
		oBack.onclick = function() { closeFlopup(); }
		document.body.appendChild(oBack);
	}
	else
	{
		// If another flopup is shown, hide it
		if (oBack.flopup_id !== undefined && oBack.flopup_id != "" )
		{
			var oMainOld = document.getElementById(oBack.flopup_id);
			if ( oMainOld ) oMainOld.style.display = "none";
		}
	}
	oBack.style.display = "block";
	oBack.flopup_id = flopup_id;

	var oMain = document.getElementById(flopup_id);
	if ( oMain ) oBack.parentNode.appendChild(oMain);
	else
	{
		// flopup doesn't exist, create it
		oMain = document.createElement("div");
		oMain.id = flopup_id;
		oMain.style.zIndex = 100;
		oBack.parentNode.appendChild(oMain);
	}

	if ( oCaller !== undefined ) setPosition(oMain, oCaller)
	else setPosition(oMain)
	oMain.style.display = "block";
	resizeFlopupBack();

	return oMain;
}


function resizeFlopupBack()
{
	var oBack = document.getElementById("flopup_back");
	if ( oBack )
	{
		if ( window.innerHeight && window.scrollMaxY ) h = (window.innerHeight+window.scrollMaxY)+"px";	// Firefox
		else h = document.body.scrollHeight+"px";	// IE 
		oBack.style.height = h;
	}
}


function closeFlopup()
{
	var oBack = document.getElementById("flopup_back");
	if ( oBack )
	{
		var oMain = document.getElementById(oBack.flopup_id);
		if ( oMain ) oMain.style.display = "none";

		oBack.style.display = "none";
	}
}


function setPosition(oMain, oCaller)
{
	w = getWidth(oMain);
	h = getHeight(oMain);

	if ( oCaller !== undefined )
	{
		pos = findPosition(oCaller);
		l = pos.x + Math.floor(getWidth(oCaller)/2) - Math.floor(w/2);
		t = getHeight(oCaller)+pos.y;
		if (document.all) t -= 7;		// IE 5.5 fix
		oMain.style.left = l+"px";
		oMain.style.top = t+"px";
	}
	else
	{
		var scroll_y = ( document.all ) ? document.documentElement.scrollTop : window.pageYOffset; 

		// if no caller, then center on screen
		win_w = ( window.innerWidth ) ? window.innerWidth : document.documentElement.clientWidth;
		win_h = ( window.innerHeight ) ?  window.innerHeight : document.documentElement.clientHeight;

		l = Math.floor(win_w/2) - Math.floor(w/2);
		t = Math.floor(win_h/2) - Math.floor(h/2) + scroll_y;
		oMain.style.left = l+"px";
		oMain.style.top = t+"px";
	}
}
