/**
 *	MessageBox
 *	Alternative for alert()
 *
 *	Requires: jQuery.fn.center, jQuery.mask
 *
 *	@author: Jerry V. Sietsma
 *	@creationdate: 18-06-2008
 */
jQuery.MessageBox = function()
{
	var isActive = false;
	var isInit = false;
	
	var wrapEl, titleEl, msgEl, footerEl;
	
	var modal = (typeof($.mask.show) == 'function' && (!$.browser.msie)) ? $.mask : ($.browser.msie && ($.browser.version.number > 6)) ? $.mask : false;
	
	var defaults = {
		title: 'MessageBox',
		msg: '<b>Message</b>',
		modal: true
	};
	
	/**
	 *
	 */
	var initMessageBox = function()
	{
		$(document.body).append('<div id="messagebox-wrap"><h3 class="message-box-header">' + defaults.title + '</h3><div id="messagebox-content">' + defaults.msg + '</div><div id="messagebox-footer">&nbsp;</div></div>');
		
		wrapEl = $('div#messagebox-wrap');
		titleEl = $('div#messagebox-wrap .message-box-header');
		msgEl = $('div#messagebox-content');
		footerEl = $('div#messagebox-footer');
					
		wrapEl.center();
		wrapEl.hide();
		
		isInit = true;
	};
	
	/**
	 *	Set MessageBox options to defaults
	 *	Used when messagebox is hidden so default
	 *	settings will be used for the next instance
	 */
	var resetMessageBox = function()
	{
		titleEl.html(defaults.title);
		msgEl.html(defaults.msg);
		footerEl.html('&nbsp;');
	};

	return {
		alert: function(options)
			{
				var options = $.extend({}, defaults, options);
				
				if (isInit && !isActive)
				{
					titleEl.html(options.title);
					msgEl.html(options.msg);
					footerEl.html('<button type="button" onclick="$.MessageBox.hide();">Ok</button>');
					if (modal && options.modal)	modal.show();
					wrapEl.show();
					isActive = true;
				}
				else
				{
					if (!isInit) initMessageBox();
					if (!isActive) this.alert();
					
					if (isInit && options)
					{
						titleEl.html(options.title);
						msgEl.html(options.msg);
						footerEl.html('<button type="button" onclick="$.MessageBox.hide();">Ok</button>');
						wrapEl.show();		
					}
				}
			},
			
		wait: function(options)
			{
				var options = $.extend({}, defaults, options);
				
				if (isInit && !isActive)
				{
					titleEl.html(options.title);
					msgEl.html(options.msg);
					footerEl.html('&nbsp;');
					if (modal && options.modal)	modal.show();
					wrapEl.show();
					isActive = true;
				}
				else
				{
					if (!isInit) initMessageBox();
					if (!isActive) this.wait();
					
					if (isInit && options)
					{
						titleEl.html(options.title);
						msgEl.html(options.msg);
						wrapEl.show();		
					}
				}
			},
		
		hide: function()
			{
				resetMessageBox();
				wrapEl.hide();
				if (modal) modal.hide();
				isActive = false;
			}
	};
}();