var draggingPopup = null;

function popupWindow(url, caption, className)
{
	this.url = url;
	this.caption = caption;
	this.className = className;
	this.container = null;
	this.onClosing = null;
	this.onClose = null;
	
	this.show = function (id, bodyHtml, width, height, top, left)
	{
		this.container = document.createElement( "DIV" );
		this.container.style.position = "absolute";
		this.container.id = id;
		
		var captionControl = document.createElement( "DIV" );
		captionControl.id = "Caption";
		captionControl.container = this.container;	
		this.container.appendChild( captionControl );
		
		var captionText = document.createElement( "DIV" );
		captionText.id = "CaptionText";
		captionText.innerHTML = this.caption;
		captionControl.appendChild( captionText );

		var closeButton = document.createElement( "DIV" );
		closeButton.id = "CloseButton";
		closeButton.innerHTML = "X";
		closeButton.onclick = this.close;
		closeButton.container = this.container;
		closeButton.object = this;
		closeButton.onClosing = this.onClosing;
		closeButton.onClose = this.onClose;
		captionControl.appendChild( closeButton );
		

		captionControl.onmousedown = function(evt)
		{
			evt = (evt)? evt : event;
			draggingPopup = new Object();
			draggingPopup.container = this.container;
			draggingPopup.startScreenX = evt.screenX;
			draggingPopup.startScreenY = evt.screenY;
			draggingPopup.startX = this.container.style.left.replace( "px", "" );
			draggingPopup.startY = this.container.style.top.replace( "px", "" );
			draggingPopup.originalCursor = captionControl.style.cursor;
			captionControl.style.cursor = "move";
		}
		
		captionControl.onmouseup = function()
		{
			captionControl.style.cursor = draggingPopup.originalCursor;
			draggingPopup = null;
		}

		var contentContainer = document.createElement( "DIV" );
		contentContainer.id = "Content";
		if (bodyHtml != null) 
		{
			contentContainer.innerHTML = bodyHtml;
		}
		else 
		{
			contentContainer.innerHTML = popupWindow.loadResource( this.url );
		}	
	
		this.container.appendChild( contentContainer );
		
		if (width != null)
		{
			this.container.style.width = width;
		}
		
		if (height != null)
		{
			this.container.style.height = height;
		}
				
		if (top != null)
		{
			this.container.style.top = top;
		}
		else
		{
			this.container.style.top = this.container.style.height == ""
				? document.body.clientHeight / 2
				: eval( "document.body.clientHeight / 2 - (" + this.container.style.height.replace( "px", "" ) + ") / 2" );
		}
		
		if (left != null)
		{
			this.container.style.left = left;
		}
		else
		{
			this.container.style.left = eval( "document.body.clientWidth / 2 - (" + this.container.style.width.replace( "px", "" ) + ") / 2" );
		}		

		if (this.className != null)
		{
			this.container.className = this.className;
		}
		
		document.body.appendChild( this.container );
		this.container.style.zIndex = 1;
	}
	
	this.close = function()
	{
		if (this.onClosing != null)
		{
			if (this.onClosing( this.object ) == false)
			{
				return;
			}
		}

		document.body.removeChild( this.container );
		if (this.onClose != null)
		{
			this.onClose( this.object );
		}
	}
		
	document.onmousemove = function(evt)
	{
		evt = (evt)? evt : event;
		if (draggingPopup != null)
		{
			var relativeX = evt.screenX - draggingPopup.startScreenX;
			var relativeY = evt.screenY - draggingPopup.startScreenY;
			draggingPopup.container.style.left = eval( "relativeX + " + draggingPopup.startX ) + "px";
			draggingPopup.container.style.top = eval( "relativeY + " + draggingPopup.startY ) + "px";
			evt.cancelBubble = true;
		}
	}
}

popupWindow.loadResource = function( url )
{
	var resourceData = "";

	var xmlHttp = new XMLHttpRequest();
	xmlHttp.open( "GET", url, false);
	xmlHttp.onreadystatechange = function()
	{
		if (xmlHttp.readyState == 4)
		{
			if (xmlHttp.status == 200)
			{
				resourceData = xmlHttp.responseText;
			}
			else
			{
				resourceData = "couldn't load page: " + url;
			}
		}
	}
	
	xmlHttp.send();
	
	return resourceData;
}
