
// Display message box with specified text, like standard alert (default) or with specified options.
// Use MessageBox.close() to close dialog.
// Syntax:
// 	MessageBox.show(msg, buttons, options)
// 	MessageBox.confirm(msg, function)
// Parameters
// 	text: Text to display.
// 	buttons(default: 'OK'): Specifies which buttons should be displayed on the dialog. The property key is the text of the button.
// 													The value is the callback function for when the button is clicked.
// 	options:
// 		title(default: empty) Set the MessageBox title.
// 		icon(default: 'info'): Set the MessageBox icon. Must be one of  ['warning', 'info', 'success', 'error']
// Examples
// 	MessageBox.show('text', {'title': 'tytul', 'icon': 'warning'});
// 	MessageBox.show('some text', {_('OK'): function() {//do something}}, {'icon': 'error' });
// 	MessageBox.show('foo', {'icon': 'info'});
// 	MessageBox.confirm('Are you sure?', function(){//do something});
// 	MessageBox.alert('Hello world!');
// 	MessageBox.close();

var MessageBox = {
	show: function(msg, buttons, options) {
		// buttons is optional
		if (options === undefined && $j.isObject(buttons)) {
			options = buttons;
			buttons = undefined;
		}
		options = options || {};
		if(!buttons){
			buttons = {};
			buttons[_('OK')] = function(){MessageBox.close()};
		}
		buttons = buttons || {button_ok: function(){MessageBox.close()}};

		var title = options['title'] || '';
		var icon = options['icon'] || 'info';
		var confirm = options['confirm'] || false;
		var width = options['width'] || 400;
		var height = options['height'] || 200;
		var flash = false;
		var iconClass = icon[0].toUpperCase() + icon.substr(1);
		
		var iconPath = '/pub/layout/images/icons/';
		var iconSrc = iconPath + 'message_' + icon + '.png';

		// Remove existing messageBox (if exist) and add new one.
		$j('#message_box').remove();
		$j("body").append($j("<div>").attr({'id': "message_box", 'class': "MessageBox"}));

		// Set content
		var iconDiv = $j("<div>").addClass('MessageBoxIcon').addClass(iconClass)//.append($j("<img>").attr('src', iconSrc));
		var textDiv = $j("<div>").addClass('MessageBoxText').append(msg.toString());

		$j('#message_box').append(iconDiv);
		$j('#message_box').append(textDiv);
		$j('#message_box').dialog({
			minWidth: 300,
			minHeight: 150,
			width: width,
			height: height,
			buttons: buttons,
			modal: true,
			resizable: false,
			title: title,
			autoOpen: false,
			close: function(event, ui) {
				if (flash = $j('object[type=application/x-shockwave-flash]')) {flash.show();}
			},
			open: function(event, ui) {
				if (flash = $j('object[type=application/x-shockwave-flash]')) {flash.hide();}
			}
		});
		$j('#message_box').dialog('open');
	},
	confirm: function(msg, fun) {
		// Add $j(this).dialog('close') in +fun+ param if you want to close dialog after 'Yes' was choosen;
		buttons = {};
		buttons[_('No')] = function(){ MessageBox.close(); };
		buttons[_('Yes')] = function(){ fun.call(this); MessageBox.close() };
		MessageBox.show(msg, buttons, {'icon': 'warning'});
	},
	alert: function(msg) {
		MessageBox.show(msg);
	},
	close: function() {
		$j('#message_box').dialog('close');
	}
};

standardAlert = window.alert;
window.alert = function(msg) {
	MessageBox.show(msg, {'icon': 'info'});
};
