function ShowFloatingWindow(elem, content, title, width)
{
	// Find / create floating window
	var wnd = $("floating_window");
	if(wnd == null)
	{
		var newDiv = document.createElement('div');
		newDiv.innerHTML = "<div id='floating_window' name='floating_window' style='z-index:100; display:none; position:absolute; background-color:#FFF'></div>";
		document.body.appendChild(newDiv);
		wnd = $("floating_window");
	}
	
	// Find coordinates
	var objItem = $(elem);
	var objParent = null;
	var intX = 0;
	var intY = 0;
	do
	{	// Walk up our document tree until we find the body
		// and add the distance from the parent to our counter.
		intX += objItem.offsetLeft;
		intY += objItem.offsetTop;
		objParent = objItem.offsetParent.tagName;
		objItem = objItem.offsetParent;
	}
	while(objParent != 'BODY');
	
	// Show it
	wnd.style.left = (intX - 100 > 0 ? intX - 100 : intX) + 'px';
	wnd.style.top = (intY - 50 > 0 ? intY - 50 : intY) + 'px';
	wnd.innerHTML = "<table style='border:1px solid #000; width:" + width + "' cellspacing='0'>" + 
		"<tr style='background-color:#000080; color:#FFF'><td width='98%'>" + title + 
		"</td><td align='right' width='2%' style='background-color:#C00; color:#FFF; cursor:pointer; width:5px'>" +
		"<span onclick='Element.hide(\"floating_window\")'>[X]</span></td></tr>" + 
		"<tr><td colspan='2' style='font-size:80%; padding:.5em'>" + content + "</td></tr></table>";
	Element.show(wnd);
}

function ShowWindow(name, text, width, height)
{
	if(!width)	width = 50;
	if(!height)	height = 75;
		
	var wnd = $(name);
	if(wnd != null)
		wnd.parentNode.removeChild(wnd);
		
	var newDiv = document.createElement('div');
	newDiv.innerHTML += "<div id='" + name + "' name='" + name + "' " + 
		"style='z-index:100; display:none; position:absolute; overflow-y:hidden;" + 
		"width:" + width + "%; height:" + height + "%; top:" + (100-height)/2 + "%; left:" + (100-width)/2 + 
		"%; background-color:#FFF; border:1px solid #000;'></div>";
	document.body.appendChild(newDiv);
	wnd = $(name);

	wnd.innerHTML = text;
	wnd.style.top = (((document.all) ? document.body.scrollTop : window.pageYOffset) + 70) + "px";
	Element.show(wnd);
}

function ShowComment(id)
{
	ShowWindow("floating_comment", 
		"<div align='center'><br/><br/><br/>Loading...<br/><img src='/files/images/pleasewait.gif'/></div>", 30, 20); 
	new Ajax.Request("/action/comments", 
		{	method: "get", 
			parameters : "id=" + id,
			onComplete: function(req)
			{
				if(req.responseText.slice(0,8) == "comment;") 
					ShowWindow("floating_comment", req.responseText.slice(8)); 
			}
		} );
}

function PostComment(id)
{
	if($('comment').value == "")
	{
		alert("Bad input");
		return false;
	}
	$("postb").disabled = true;
	Element.show("post_wait");
	new Ajax.Request("/action/comments/add", 
		{	method: "get", 
			parameters : "floating=1&itemid=" + id + "&comment=" + encodeURI($('comment').value),
			onComplete: function(req)
			{
				if(req.responseText.slice(0,8) == "comment;")
				{
					$("comment_contents").innerHTML = req.responseText.slice(8);
					$("postb").focus();
					$("comment").focus();
				}
			}
		} );
	return false;
}

function DeleteComment(id)
{
	if(confirm(Lang.delete_confirm))
	{
		Element.show("post_wait");
		new Ajax.Request("/action/comments/delete/" + id, 
			{	method: "get", 
				parameters : "floating=1",
				onComplete: function(req)
				{
					if(req.responseText.slice(0,8) == "comment;") 
						$("comment_contents").innerHTML = req.responseText.slice(8);
				}
			} );
	}
	return false;
}
