
// Set the visibility state of the currency list
function ShowCurrencySelection(state)
{
	var elem = document.getElementById('currencyList');
	if (!elem)
		return false;
	if (state == 'toggle')
		elem.style.display = (elem.style.display != 'block') ? 'block' : 'none';
	else
		elem.style.display = (state == 'on') ? 'block' : 'none';
	return true;
};

// Handle hiding of the currency list upon mouseclick
function BodyClickHandler(click)
{
	var node = (click) ? click.target : event.srcElement;
	while (node)
	{
		if ((node.id == 'currencyFlag') || (node.id == 'currencyList'))
		{
			return;
		}
		node = node.parentNode;
	}
	ShowCurrencySelection('off');
};

// Register mouseclick event with window/document
if (window.captureEvents)
{
	window.captureEvents(Event.CLICK);
	window.onclick = BodyClickHandler;
}
else
{
	document.onclick = BodyClickHandler;
}


//-------------------------------------------------------------------------------------

var _mt_overlaySheet = 0;
var _mt_windowPosition = 0;
var _mt_windowInterval = null;

// Return a reference to the overlay sheet, or create one as necessary
function GetOverlaySheet(state)
{
	if (_mt_overlaySheet)
		return _mt_overlaySheet;
	
	var wrapper = document.getElementById('layout_wrapper');
	if (!wrapper)
		return null;
	
	var wrapperHeight = document.getElementById('layout_header').offsetHeight + document.getElementById('layout_body').offsetHeight;
	
	_mt_overlaySheet = document.createElement('div');
	_mt_overlaySheet.className = 'overlay_sheet';
	_mt_overlaySheet.style.height = wrapperHeight+'px';
	//_mt_overlaySheet.onclick = function() { DisplayCountryOverlay(false); };
	wrapper.appendChild(_mt_overlaySheet);
	return _mt_overlaySheet;
};

// Set the display status of the overlay sheet
function DisplayCountryOverlay(enable)
{
	var sheet = GetOverlaySheet();
	var options = document.getElementById('overlay_window');
	if (!sheet || !options)
		return;
	
	var alpha = (enable) ? 70 : 0;
	var display = (enable) ? 'block' : 'none';
	
	sheet.style.display = display;
	sheet.style.opacity = alpha * 0.01;
	sheet.style.MozOpacity = alpha * 0.01;
	sheet.style.filter = 'alpha(opacity='+alpha+')';
	
	options.style.display = display;
	if (enable)
	{
		_mt_windowPosition = null;
		PositionCountryOverlay(options);
	}
	
	//window.onresize = function() { PositionCountryOverlay(null); };
	
	if (enable && !_mt_windowInterval)
		_mt_windowInterval = window.setInterval(PositionCountryOverlay, 25);
	else if (!enable && _mt_windowInterval)
	{
		window.clearInterval(_mt_windowInterval);
		_mt_windowInterval = null;
	}
};

// Position the locale-selection window
function PositionCountryOverlay(popup)
{
	if (!popup || (typeof popup != Object))
		popup = document.getElementById('overlay_window');
	if (!popup)
		return;
	
	var wrapper = document.getElementById('layout_wrapper');
	if (!wrapper)
		return;
	
	popup.style.left = Math.round((wrapper.offsetWidth - popup.offsetWidth) * 0.5) + 'px';
	
	var targetHeight = GetVerticalScroll() + 200;
	if (!_mt_windowPosition)
	{
		popup.style.top = targetHeight + 'px';
		_mt_windowPosition = targetHeight;
		return;
	}
	
	if (_mt_windowPosition == targetHeight)
		return;
	
	var diff = targetHeight - _mt_windowPosition;
	diff = (diff > 0) ? Math.ceil(diff*0.25) : Math.floor(diff*0.25);
	_mt_windowPosition += diff;
	popup.style.top = _mt_windowPosition + 'px';
};

// Return the browser's vertical scroll position
function GetVerticalScroll()
{
	if (document.documentElement)
		return document.documentElement.scrollTop;
	else if (window.pageYOffset)
		return window.pageYOffset;
	else if (document.body.parentElement)
		return document.body.parentElement.scrollTop;
	return 0;
}


