
var AEWindow = function(zIndex, width, height, overlayClass, modalboxClass, img, error)
{
	this.zIndex = zIndex;
	this.width = width;
	this.height = height;
	this.overlayClass = overlayClass;
	this.modalboxClass = modalboxClass;
	this.img = img;
	this.error = error;


	this.overlay = null;
	this.modalbox = null;

	this.loadingUrl = null;
	this.loadedUrl = null;
}



AEWindow.prototype.open = function(url, body, width, height)
{
	if (width && height)
		this.resize(width, height)

	this.hideModalbox();
	this.showOverlay();
	this.getContents(url, body);
}



AEWindow.prototype.close = function()
{
	this.loadedUrl = null;
	this.hideModalbox(true);
	this.hideOverlay();
}



AEWindow.prototype.resize = function(width, height)
{
	this.width = width;
	this.height = height;

	if (this.modalbox)
	{
		this.modalbox.style.width = width;
		this.modalbox.style.height = height;
	}
}



AEWindow.prototype.submit = function(form)
{
	var url = form.action ? form.action : this.loadedUrl;
	var body = '';
	for (var i=0; i<form.elements.length; i++)
	{
		e = form.elements[i];
		if ((e.type=='checkbox' || e.type=='radio') && !e.checked)
			continue;
		body += e.name+'='+encodeURIComponent(e.value)+'&';
	}
	this.open(url, body);
}



AEWindow.prototype.createCenteredBlock = function(o, bOnlyVerticalCenter)
{
	var table = document.createElement('table');
	o.appendChild(table);
	var tr = table.insertRow(0);
	var td = tr.insertCell(0);
	table.setAttribute('cellspacing', '0');
	table.setAttribute('cellpadding', '0');
	table.setAttribute('width',       '100%');
	table.setAttribute('height',      '100%');
	td.setAttribute('valign',         'middle');
	if (!bOnlyVerticalCenter)
		td.setAttribute('align', 'center');
	return td;
}



AEWindow.prototype.showOverlay = function()
{
	if (!this.overlay) {
		this.overlay = document.createElement('div');
		document.body.appendChild(this.overlay);
		this.overlay.id = '__overlay';
		this.overlay.style.position   = 'fixed';
		this.overlay.style.visibility = 'hidden';
		this.overlay.style.zIndex     = this.zIndex;
		this.overlay.style.left       = '0px';
		this.overlay.style.top        = '0px';
		this.overlay.style.width      = '100%';
		this.overlay.style.height     = '100%';
		this.overlay.style.opacity    = '0.50';
		this.overlay.style.filter     = 'alpha(opacity=50)';
		this.overlay.className = this.overlayClass;
		var cb = this.createCenteredBlock(this.overlay);
		var img = document.createElement('img');
		img.setAttribute('src', this.img);
		cb.appendChild(img);
	}
	this.overlay.style.visibility = 'visible';
}



AEWindow.prototype.hideOverlay = function()
{
	if (this.overlay)
		this.overlay.style.visibility = 'hidden';
}



AEWindow.prototype.showModalbox = function(contents)
{
	if (!this.modalbox) {
		var o2 = document.createElement('div');
		document.body.appendChild(o2);
		o2.style.position   = 'fixed';
		o2.style.visibility = 'hidden';
		o2.style.zIndex     = this.zIndex;
		o2.style.left       = '0px';
		o2.style.top        = '0px';
		o2.style.width      = '100%';
		o2.style.height     = '100%';
		var cb = this.createCenteredBlock(o2, true);
		this.modalbox = document.createElement('div');
		cb.appendChild(this.modalbox);
		this.modalbox.style.width      = this.width;
		this.modalbox.style.height     = this.height;
		this.modalbox.style.margin     = 'auto';
		this.modalbox.style.visibility = 'hidden';
		this.modalbox.className = this.modalboxClass;
	}
	this.modalbox.style.visibility = 'visible';
	this.modalbox.parentNode.parentNode.parentNode.parentNode.style.visibility = 'visible';

	if (contents)
	{
		this.modalbox.innerHTML = contents;
//			.replace(/href\=\"([^\"]+)\"/ig, 'href="javascript:AEWindow.open(\''+this.name+'\', \'$1\')"')
//			.replace(/\<form/ig, '<form onsubmit="AEWindow.submit(\''+this.name
//				+'\', this); return false;" onreset="AEWindow.close(\''+this.name+'\'); return false;"');

		var scripts = this.modalbox.getElementsByTagName('script');
		for (var i=0; i<scripts.length; i++)
			eval(scripts[i].innerHTML);
	}
}



AEWindow.prototype.hideModalbox = function(clear)
{
	if (this.modalbox) {
		this.modalbox.style.visibility = 'hidden';
		this.modalbox.parentNode.parentNode.parentNode.parentNode.style.visibility = 'hidden';
		if (clear)
			this.modalbox.innerHTML = '';
	}
}



AEWindow.prototype.getContents = function(url, body)
{
	this.loadingUrl = url;
	var method = body ? 'POST' : 'GET';
	var req = new XMLHttpRequest();
	req.window = this;
	req.open(method, this.loadingUrl, true);
	
	if (method=='POST')
		req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
	if (document.cookie)
		req.setRequestHeader('Cookie', document.cookie);

	req.onreadystatechange = function()
	{
		if (this.readyState!=4)
			return;
		if (this.status==200) {
			this.window.loadedUrl = this.window.loadingUrl;
			this.window.loadingUrl = null;
			this.window.showModalbox(this.responseText);
		} else {
			if (this.window.error)
				alert(this.window.error);
			else
				alert("Error\nreadyState: "
					+this.readyState+"\nstatus: "
					+this.status+"\nheaders:\n"
					+this.getAllResponseHeaders());

			if (this.window.loadedUrl)
				this.window.showModalbox();
			else
				this.window.hideOverlay();
		}
	}
	req.send(body);
}

