window.alert = function(msg, options) {
	var div = $(document.createElement('div'));

	div.addClass('ui-state-error');
	div.html('<span class="ui-icon ui-icon-alert" style="float:left; margin-right:3px"></span>');
   	div.append(String(msg).replace(/\r?\n/g, '<br/>'));

	var opts = $.extend({
		title:'Alert',
		width:'auto'
	}, options);

	var p = $.extend({
		title:opts.title,
		width:opts.width,
		minWidth:opts.width,
		height:'auto',
		minHeight:'auto',
		resizable:false,
		modal:true,
		buttons:{
			OK:function(){
				$(this).dialog('close');
				div.remove();
			}
		}
	}, options);

	div.dialog(p);
	fix_dialog(div, p);
};

window.info = function(msg, options) {
	var div = $(document.createElement('div'));

	div.addClass('ui-state-highlight');
	div.html('<span class="ui-icon ui-icon-info" style="float:left; margin-right:3px"></span>');
	div.append(String(msg).replace(/\r?\n/g, '<br/>'));

	var opts = $.extend({
		title:'Info',
		width:'auto'
	}, options);

	var p = $.extend({
		title:opts.title,
		width:opts.width,
		minWidth:opts.width,
		height:'auto',
		minHeight:'auto',
		resizable:false,
		modal:true,
		buttons:{
			OK:function(){
				$(this).dialog('close');
				div.remove();
			}
		}
	}, options);

	div.dialog(p);
	fix_dialog(div, p);
};

window.confirm = function(msg, options) {
	var div = $(document.createElement('div'));

	div.addClass('ui-state-highlight');
	div.html('<span class="ui-icon ui-icon-help" style="float:left; margin-right:3px;"></span>');
	div.append(String(msg).replace(/\r?\n/g, '<br/>'));

	var opts = $.extend({
		title:'Confirm',
		width:'auto',
		onConfirm:function(r){}
	}, options);

	var p = $.extend({
		title:opts.title,
		width:opts.width,
		minWidth:opts.width,
		height:'auto',
		minHeight:'auto',
		resizable:false,
		modal:true,
		buttons:{
			Yes:function() {
				$(this).dialog('close');
				div.remove();
				opts.onConfirm(true);
			},
			No:function() {
				$(this).dialog('close');
				div.remove();
				opts.onConfirm(false);
			}
		}
	}, options);

	div.dialog(p);
	fix_dialog(div, p);
};

function fix_dialog(d, o) {
	var ua = $.browser;
	
	if (ua.msie) {
		if (o.width == 'auto') {
			switch(Number(ua.version.slice(0, 1))) {
			case 7:
				d.dialog('option', 'width', d.outerWidth());
				d.dialog('option', 'position', 'center');
				break;
			case 6:
			case 5:
				d.dialog('option', 'width', 300);
				d.dialog('option', 'position', 'center');
				break;
			default:
				break;
			}
		}
	}
}

