
//
// Popup class definition
//

function Popup()
{
	//
	// Add the popup object to the instance array
	//

	var m_instance_count = Popup.prototype.popup_instances.length;

	Popup.prototype.popup_instances[m_instance_count] = this;

	//
	// Create the popup's container element
	//

	var m_container_element = document.createElement('div');

	m_container_element.className = 'popup_container';

	//
	// IE6 doesn't understand the 'position: fixed' style property
	//

	if(IE && (getIEVersion() < 7))
	{
		m_container_element.style.position = 'absolute';
	}

	//
	// Create the popup's background element
	//

	var m_background_element = document.createElement('div');

	m_background_element.className = 'popup_background';

	m_container_element.appendChild(m_background_element);

	//
	// Create the popup's content element
	//

	var m_content_element = document.createElement('div');

	m_content_element.className = 'popup_content';

	m_container_element.appendChild(m_content_element);

	//
	// Shows the popup window
	//

	this.show = function()
	{
		m_container_element.style.display = '';
		
		document.body.appendChild(m_container_element);

		this.centerContent();
	}

	//
	// Hides the popup window
	//

	this.hide = function()
	{
		m_container_element.style.display = 'none';
	}

	//
	// Sets the popup window's absolute position
	//

	this.setAbsolutePosition = function(x, y)
	{
		m_container_element.style.left = x + 'px';
		m_container_element.style.top = y + 'px';
	}

	//
	// Sets the popup window's content
	//

	this.setContent = function(element)
	{
		//
		// Replace the popup's content
		//

		while(m_content_element.hasChildNodes())
		{
			m_content_element.removeChild(m_content_element.firstChild);
		}

		if(element)
		{
			m_content_element.appendChild(element);
		}
	}

	//
	// Centers the content element within the container element
	//

	this.centerContent = function()
	{
		var x = (m_container_element.offsetWidth - m_content_element.offsetWidth) / 2;
		var y = (m_container_element.offsetHeight - m_content_element.offsetHeight) / 2;

		m_content_element.style.left = ((x > 0) ? x : 0) + 'px';
		m_content_element.style.top = ((y > 0) ? y : 0) + 'px';
	}
}

//
// Used to store an array of popup instances
//

Popup.prototype.popup_instances = new Array();

//
// Emulates the 'position: fixed' style property in IE6
//

Popup.prototype.onWindowScroll = function()
{
	if(IE && (getIEVersion() < 7))
	{
		var document_element = getDocumentElement();

		for(var i = 0; i < Popup.prototype.popup_instances.length; ++i)
		{
			Popup.prototype.popup_instances[i].setAbsolutePosition
			(
				document_element.scrollLeft,
				document_element.scrollTop
			);
		}
	}
}

//
// Keeps the popup content centered when the browser window is resized
//

Popup.prototype.onWindowResize = function()
{
	for(var i = 0; i < Popup.prototype.popup_instances.length; ++i)
	{
		Popup.prototype.popup_instances[i].centerContent();
	}
}

