var commutatorStay = true;
var error = 'Chyba: ';
var errorParametrWrong = error + 'Neplatný parametr funkce greyBoxShow';
var img = new Image();

function getBrowserWidth()
{
	if (window.innerWidth)
	{
		return window.innerWidth;
	}
	else if (document.documentElement && document.documentElement.clientWidth != 0)
	{
		return document.documentElement.clientWidth;
	}
	else if (document.body)
	{
		return document.body.clientWidth;
	}

	return 0;
};

function getBrowserHeight()
{
	if (window.innerHeight)
	{
		return window.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight != 0)
	{
		return document.documentElement.clientHeight;
	}
	else if (document.body)
	{
		return document.body.clientHeight;
	}

	return 0;
};

function greyBoxShow (elementType, element, border)
{
	function greyBoxPosition (element)
	{
		document.getElementById(element).style.position = 'relative';
		document.getElementById(element).style.top = (getBrowserHeight() - document.getElementById(element).offsetHeight) / 2 + 'px';
		
		if (border == 1)
		{
			document.getElementById(element).style.border = '1px solid #ffffff';
		}
		else
		{
			document.getElementById(element).style.border = 'none';
		}
	}
	
	function greyBoxPositionLoading (element)
	{
		document.getElementById(element).style.position = 'relative';
		document.getElementById(element).style.top = (getBrowserHeight() - document.getElementById(element).offsetHeight) / 2 + 'px';
	}
	
	document.getElementById('greyBox').style.width = getBrowserWidth() + 'px';
	document.getElementById('greyBox').style.height = getBrowserHeight() + 'px';
	document.getElementById('greyBox').style.display = 'block';
	document.getElementById('greyBoxEditSpace').style.display = 'none';
	document.getElementById('greyBoxLoading').style.display = 'none';
	document.getElementById('greyBoxImg').style.display = 'none';
	
	if (elementType == 1)
	{
		document.getElementById('greyBoxEditSpace').style.display = 'block';
		document.getElementById(element).style.display = 'block';
		
		greyBoxPosition(element);
	}
	else if (elementType == 2)
	{
		function fProcess()
		{
			document.getElementById('greyBoxLoading').style.display = 'none';
			document.getElementById('greyBoxImg').style.display = 'block';
			document.getElementById('greyBoxImg').src = img.src;
			
			if (img.width <= getBrowserWidth() && img.height <= getBrowserHeight())
			{
				document.getElementById('greyBoxImg').style.width = img.width + 'px';
				document.getElementById('greyBoxImg').style.height = img.height + 'px';
			}
			else if (img.width <= getBrowserWidth() && img.height > getBrowserHeight())
			{
				var ratio;
				
				ratio = img.height / getBrowserHeight();
				
				document.getElementById('greyBoxImg').style.width = img.width / ratio + 'px';
				document.getElementById('greyBoxImg').style.height = getBrowserHeight() + 'px';
			}
			else if (img.width > getBrowserWidth() && img.height <= getBrowserHeight())
			{
				var ratio;
				
				ratio = img.width / getBrowserWidth();
				
				document.getElementById('greyBoxImg').style.width = getBrowserWidth() + 'px';
				document.getElementById('greyBoxImg').style.height = img.height / ratio + 'px';
			}
			else if (img.width > getBrowserWidth() && img.height > getBrowserHeight())
			{
				if (img.width - getBrowserWidth() < img.height - getBrowserHeight())
				{
					var ratio;
				
					ratio = img.height / getBrowserHeight();
					
					document.getElementById('greyBoxImg').style.width = img.width / ratio + 'px';
					document.getElementById('greyBoxImg').style.height = getBrowserHeight() + 'px';
				}
				else
				{
					var ratio;
				
					ratio = img.width / getBrowserWidth();
					
					document.getElementById('greyBoxImg').style.width = getBrowserWidth() + 'px';
					document.getElementById('greyBoxImg').style.height = img.height / ratio + 'px';
				}
			}
			
			greyBoxPosition('greyBoxImg');
		}
		
		greyBoxPositionLoading('greyBoxLoading');
		
		document.getElementById('greyBoxLoading').style.display = 'block';
		
		if (element.src == null)
		{
			img.src = element;
			img.onload = fProcess;
		}
		else
		{
			img.src = element.src;
			
			fProcess();
		}
	}
	else alert(errorParametrWrong);
}

function greyBoxHide (elements)
{
	document.getElementById('greyBox').style.display = 'none';

	if (elements != 'null')
	{
		Array.prototype.count = function()
		{
			return this.length;
		};
		
		var elementsArray = elements . split ('~');
		
		for (i = 0; i < elementsArray . count (); i++)
		{
			document . getElementById(elementsArray[i]) . style . display = 'none';
		}
	}
}

function greyBoxStay (stayType)
{
	if (stayType == 0)
	{
		commutatorStay = true;
	}
	else if (stayType == 1)
	{
		commutatorStay = false;
	}
}

