var modal =
{
	popupScreen : undefined,

	popup       : undefined,

	show        : function (html, title, options)
	{
		var yCenter;

		// only one modal dialog can be opened at any time... (MCB)
		if(modal.popupScreen)
		{
			return false;
		}

		// black out the rest of the interface (MCB 4/1/2009)
		modal.popupScreen=document.createElement('DIV');
		modal.popupScreen.style.position        = 'fixed';
		modal.popupScreen.style.top             = '0px';
		modal.popupScreen.style.left            = '0px';
		modal.popupScreen.style.width           = 'auto';
		modal.popupScreen.style.height          = '100%';
		modal.popupScreen.style.backgroundColor = '#000000';
		modal.popupScreen.style.filter          = 'alpha(opacity=60)';
		modal.popupScreen.style.opacity         = 0.6;
		modal.popupScreen.style.zIndex          = 4;

		document.body.insertBefore(modal.popupScreen, null);
		var titleBar = "<div class='clear' style='position: relative; top: 0px; left: 0px; background-color: blue; width: 100%; height: 23px; color: white; font-weight: bold; text-align: left;'><p id='modalTitleBar' style='margin-top: 4px; margin-left: 3px; float: left; white-space: nowrap;'>" + title + "</p><img src='/images/close.gif' style='margin: 1px; margin-right: 0px; float: right;' onclick='modal.close ()' /></div>\n";

		modal.popup = document.createElement('DIV');

		modal.popup.innerHTML             = titleBar + "<div id='modalContentContainer' style='padding: 5px;'>" + html + '</div>';
 
		modal.popup.style.paddingBottom    = '5px';
		modal.popup.style.backgroundColor = 'white';
		modal.popup.style.position        = 'fixed';
		modal.popup.style.borderStyle     = 'groove';
		modal.popup.style.zIndex          = 5;

		if(window.innerWidth)
		{
			xCenter = window.innerWidth;
		}
		else
		{
			xCenter = screen.availWidth;
		}

		if(window.innerHeight)
		{
			yCenter = window.innerHeight;
		}
		else
		{
			yCenter = screen.availHeight;
		}

		if (options && typeof options['width'] != 'undefined')
		{
			modal.popup.style.left            = parseInt((xCenter / 2) - (options['width'] / 2)) + 'px';
			modal.popup.style.width           = options['width'] + 'px';
		}
		else
		{
			modal.popup.style.left = parseInt(xCenter / 5) + 'px';
		}

		if (options && typeof options['height'] != 'undefined')
		{
			modal.popup.style.top             = parseInt((yCenter / 2) - (options['height'] / 2)) + 'px';
			modal.popup.style.height          = options['height'] + 'px';
		}
		else
		{
			modal.popup.style.top = parseInt(yCenter / 2.5) + 'px';
		}

		modal.popup.style.zIndex          = 12;
		modal.popup.style.margin          = '0 auto 0 auto';
		document.body.insertBefore(modal.popup, modal.popupScreen);

		modal.popup.style.visibility='hidden';

		setTimeout("modal.adjustPosition();", 300);

		// it seems that IE does not evaluate scripts when they are inserted into the DOM (MCB)
		if(document.all)
		{
			if(html.match(/<script[^>]+?>([\S\s]*?)<\/script>/ig))
			{
				eval(RegExp.$1);
			}
		}
	},

	adjustPosition : function ()
	{
		var yCenter;

                if(window.innerHeight)
                {
                        yCenter = window.innerHeight;
                }
                else
                {
                        yCenter = screen.availHeight;
                }

		if (modal.popup.offsetHeight > 200)
		{
			modal.popup.style.top = parseInt((yCenter / 2) - (modal.popup.offsetHeight / 2)) + 'px';
		}

		// I wonder if we even really need this... (2/17/2009 MCB)
		if (modal.popup.offsetWidth > 900)
		{
			modal.popup.style.width = '900px';
		}

		modal.popup.style.visibility = 'visible';

		// don't let the close box wrap, make room for it (2/9/2009 MCB)
		var bar = document.getElementById('modalTitleBar')
//		bar.style.width = (bar.parentNode.offsetWidth - 30) + 'px';
	},

	close : function ()
	{
		modal.popup.parentNode.removeChild(modal.popup);
		modal.popupScreen.parentNode.removeChild(modal.popupScreen);
		modal.popup = undefined;
		modal.popupScreen = undefined;
	}
}
