function InsertEmptySpace(element)
{
	if (!element || element.innerHTML)
	{
		return;
	}
	
	element.innerHTML = '&nbsp;';
}

function UpdatePopupShadowSideHeight(side)
{
	side = $(side);
	side.css("height", side.height() + "px");
	side.find("table td").each(function () { InsertEmptySpace(this); });
	
	topHeight = 6;
	bottomHeight = 6;
	centerHeight = side.height() - topHeight - bottomHeight;
	side.find("table td.center").css("height", centerHeight + "px");
}

function UpdatePopupShadowSide(side)
{
	side = $(side);
	side.find("td.left, td.right, td.left_corner, td.right_corner, td.center").each(function () { InsertEmptySpace(this); });
	
	if (side.find("td.right table").size())
	{
		side.find("td.right table").css("height", side.height() + "px");
		topHeight = 8;
		centerHeight = side.height() - topHeight;
		side.find("td.right td.center").css("height", centerHeight + "px");
	}
}

function RecursiveShowElement(element, maxDepth)
{
	var visibilityMap = new Array();
	
	var curElement = element;
	var currentDepth = 0;
	while (curElement && currentDepth <= maxDepth)
	{
		if (curElement.css("display") == "none")
		{
			curElement.show();
			visibilityMap.push({element: curElement, visible:false});
		}
		curElement = curElement.parent();
		++currentDepth;
	}
	
	return visibilityMap;
}

function HideElementsByVisibilityMap(visibilityMap)
{
	for (var i = 0; i < visibilityMap.length; ++i)
	{
		if (visibilityMap[i] && visibilityMap[i].element && visibilityMap[i].element.hide && !visibilityMap[i].visible)
		{
			visibilityMap[i].element.hide();
		}
	}
}

function ResetPopupShadowSideHeight(side)
{
	side = $(side);
	side.css("height", "");
	
	side.find("table td.center").css("height", "");
}

function ResetPopupShadowSide(side)
{
	side = $(side);
	if (side.find("td.right table").size())
	{
		side.find("td.right table").css("height", "");
		side.find("td.right td.center").css("height", "");
	}
}

function UnFixPopupShadowSides(popup)
{
	popup = $(popup);
	
	popup.find("tr.center td.left, tr.center td.right").each
	(
		function ()
		{
			ResetPopupShadowSideHeight(this);
		}
	);
	
	popup.find("tr.top, tr.bottom").each
	(
		function ()
		{
			ResetPopupShadowSide(this);
		}
	);
}

function FixPopupShadowSides(popup)
{
	popup = $(popup);
	
	UnFixPopupShadowSides(popup);
	
	var visibilityMap = RecursiveShowElement(popup, 1);
	
	// fix left right sides
	popup.find("tr.center td.left, tr.center td.right").each
	(
		function ()
		{
			UpdatePopupShadowSideHeight(this);
		}
	);
	
	// fix top and bottom sides
	popup.find("tr.top, tr.bottom").each
	(
		function ()
		{
			UpdatePopupShadowSide(this);
		}
	);
	
	HideElementsByVisibilityMap(visibilityMap);
}

$(document).ready
(
	function ()
	{
		$(".popup").each
		(
			function ()
			{
				FixPopupShadowSides(this);
			}
		);
		
		$(".shadow").each
		(
			function ()
			{
				FixPopupShadowSides(this);
			}
		);
	}
);
