﻿function GetMessageBox() {
	if (typeof _msgBox != "undefined")
		return _msgBox;
	
	_msgBox = $(document.createElement('div'));
	_msgBox.addClass('messages');
	$('body').eq(0).append(_msgBox);
	
	return new MessageBox(_msgBox);
}

var MessageBox = function(msgDiv) {
	var _msgs = [];
	
	function e(type) { return $(document.createElement(type)); }
	
	this.showMessage = function(content, opts) {
		if (!opts)
			opts = {};

		if (opts.id && _msgs[opts.id]) { // If ID and ID'd message exists, hide it.
			this.hideMessage(opts.id);
		}

		var p = e('p').append(content).hide(); // Create content
		msgDiv.prepend(p);
		if (opts.id) { // save ID
			_msgs[opts.id] = p;
			p.data('msgid', opts.id);
		}

		p.slideDown('normal', function() {
			if (opts.timeout) {
				setTimeout(function() {
					if (p.data('msgid'))
						_msgs[p.data('msgid')] = null;
					p.slideUp('normal', function() {
						p.remove();
					});
				}, opts.timeout);
			}
		});
	};
	
	this.hideMessage = function(id) {
		var p = _msgs[id];
		if (p) {
			_msgs[id] = null;
			p.slideUp('normal', function() { p.remove(); });
		}
	}
};

