/*
 * dialog(Title, Text, Type)
 * 	Title:	Defines the title of the dialog
 * 	Text:	Defines the text to display. Regular HTML expected
 * 	Type:	Defines the dialog type (changes the icon)
 * 		default = none
 * 		0		= information
 * 		1		= warning
 * 		2		= error
 * 		3		= wondering smiley
 * 		4		= exclamation smiley
 * 		5		= "ehm..." smiley
 * 		6		= no image ;)
 * 		
 */

jQuery(document).ready(function() {
	jQuery('body').append(
			'<div id="dialog">' +
				'<img style="float: left; margin: 10px 5px 0pt 0pt; height: 48px; width: 48px;" id="dialog_image">' +
				'<p id="dialog_text"></p>' +
			'</div>'
	);
	jQuery('body>div#dialog').hide();
})

function simple_dialog (pTitle, pMessage, pType) {
	var jDialog = dialog(pTitle, pMessage, pType);
	jDialog.dialog({
		buttons:{
			"Ok":function(){
				jQuery(this).dialog("close");
			}
		}
	});
}

function parse_dialog_error (pError) {
	var message = pError.message;
	if (typeof(pError.code) != "undefined" && pError.code != "") {
		message += " (Errcode: " + pError.code + ")";
		simple_dialog(pError.title, message, pError.type);
	}
}

function dialog(pTitle, pText, pType) {
	var jObject = jQuery('div#dialog');
	var image = '#';
	
	if (pType == 0) {
		image = '/osr/icons/kde/crystalclear/64x64/actions/messagebox_info.png'
	} else if (pType == 1) {
		image = '/osr/icons/kde/crystalclear/64x64/actions/messagebox_warning.png'
	} else if (pType == 2) {
		image = '/osr/icons/kde/crystalclear/64x64/actions/messagebox_critical.png';
	} else if (pType == 3) {
		image = '/osr/icons/kde/other/oops.png';
	} else if (pType == 4) {
		image = '/osr/icons/kde/other/oops_1.png';
	} else if (pType == 5) {
		image = '/osr/icons/kde/other/oops_2.png';
	}
	
	if (pType == 6) {
		var result = jObject
		.clone()
			.attr('id','dialog_open')
			.attr('title',pTitle)
			.children('p#dialog_text')
				.append(pText)
				.end()
			.bind('dialogclose', function() {
				jQuery(this).remove();
			});
		result.find('img#dialog_image').remove();
	} else {
		var result = jObject
			.clone()
				.attr('id','dialog_open')
				.attr('title',pTitle)
				.children('p#dialog_text')
					.append(pText)
					.end()
				.children('img#dialog_image')
					.attr('src',image)
					.end()
				.bind('dialogclose', function() {
					jQuery(this).remove();
				});
	}
	
	return result;
}
