var backlay = null;
var lytebox = null;
var stts = null;
var opacount = 0;
var append = false;

var PATH_JS = "js/";

function createLyteBox(boxid, htmldoc, ypos, path_remote, overlay_png)
{
	if( ypos == null )
	{
		ypos = 10;
	}

	if( path_remote == null )
	{
		path_remote = "";
	}
	
	if( overlay_png == null )
	{
		overlay_png = "overlay.png";
	}
	
	var swidth = parseInt(document.body.scrollWidth);

	// Get or Create elements of lytebox and back layer.
	lytebox = $(boxid);
	if( !lytebox )	// if element doesn't exist
	{
		// create new element
		lytebox = document.body.appendChild(document.createElement("div"));
		lytebox.setAttribute ("id", boxid);
		append = true;
	}

	if( backlay == null )
	{
		backlay = document.body.appendChild(document.createElement("div"));
	}

	// set the back layer color and size
	width = parseInt(document.body.scrollWidth);
	height = parseInt(document.body.scrollHeight || document.documentElement.scrollHeight);
	height2 = document.documentElement.clientHeight || document.body.clientHeight;
	if( height < height2 )
	{
		height = height2;
	}

	// set back layer image
	if( navigator.appName.indexOf("Microsoft") != -1 )	// if IE
	{
		Element.setStyle(backlay,
			{
				"top": "0px",
				"left": "0px",
				"z-index": "50",
				"display": "none",
				"position": "absolute",
				"width": width + "px",
				"height": height + "px",
				"filter": "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + path_remote + PATH_JS + overlay_png + "', sizingMethod='scale')"
			}
		);
	}
	else
	{
		Element.setStyle(backlay,
			{
				"top": "0px",
				"left": "0px",
				"z-index": "50",
				"display": "none",
				"position": "absolute",
				"width": width + "px",
				"height": height + "px",
				"background": "transparent url(" + path_remote + PATH_JS + overlay_png + ") 0 0 repeat"
			}
		);
	}

	// set the lytebox frame
	Element.setStyle(lytebox, {"position": "absolute", "z-index": "100"});
	if( append )
	{
		lytebox.innerHTML = htmldoc;
	}

	// show elements
	Element.setStyle(backlay, {"display": "block"});
	Element.setStyle(lytebox, {"display": "block", "visibility": "visible"});

	leftpos = swidth / 2 - parseInt(Element.getWidth(lytebox)) / 2;
	Position.prepare();
	toppos = parseInt(Position.deltaY) + ypos;
	Element.setStyle(lytebox, {"top": toppos + "px", "left": leftpos + "px"});

//	setTimeout("fadeIn()", 40);
}

function fadeIn()
{
	if( opacount < 100 )
	{
		opacount += 10;
		if(lytebox.style.filter != null)		lytebox.style.filter = "alpha(opacity = " + opacount + ")";
		if(lytebox.style.opacity != null)		lytebox.style.opacity = parseFloat (opacount / 100);
		if(lytebox.style.MozOpacity != null)	lytebox.style.MozOpacity = parseFloat (opacount / 100);
		setTimeout("fadeIn()", 40);
	}
}

function updateLyteBox(htmldoc)
{
	if( htmldoc == "" )
	{
		alert("no data");
		return;
	}

	if( lytebox )
	{
		lytebox.innerHTML = htmldoc;

		leftpos = parseInt(document.body.scrollWidth) / 2 - parseInt(Element.getWidth(lytebox)) / 2;
		Position.prepare();
		toppos = parseInt(Position.deltaY) + 10;
		Element.setStyle(lytebox, {"top": toppos + "px", "left": leftpos + "px"});
	}
	else
	{
		createLyteBox(boxid, htmldoc);
	}
}

function deleteLyteBox()
{
	// delete element from document
	if( backlay )
	{
		document.body.removeChild(backlay);
		backlay = null;
	}
	if( lytebox )
	{
		if( stts )
		{
			lytebox.removeChild(stts);
			stts = null;
		}
		if( append )
		{
			document.body.removeChild(lytebox);
			lytebox = null;
			append = false;
		}
		else
		{
//			Element.setStyle(lytebox, {"y": "-1000px", "height": "1px", "margin-top": "-1000px"});
//			Element.setStyle(lytebox, {"display": "none"});
			Element.setStyle(lytebox, {"visibility": "hidden"});
		}
	}
}

function showLyteBoxStatus(htmldoc)
{
	if( lytebox )
	{
		if( stts )
		{
			stts.innerHTML = htmldoc;
		}
		else
		{
			stts = lytebox.appendChild(document.createElement ("div"));
			Element.setStyle(stts, {"position": "absolute", "border": 0, "background-color": "#900"});
			stts.innerHTML = htmldoc;
		}
	}
}

function hideLyteBoxStatus()
{
	if( lytebox && stts )
	{
		lytebox.removeChild(stts);
		stts = null;
	}
}
