var $dialog;

function displayNotification(text) {
	$dialog.html(text);
	$dialog.dialog("option", "dialogClass", 'notification' )
			.dialog( "option", "buttons", { "Ok": function() { $(this).dialog("close"); } } )
			.dialog( "option", "modal", true )
			.dialog( "option", "title", 'Systémová notifikace' );
	$dialog.dialog('open');
}

function displayWarning(text) {
	$dialog.html(text);
	$dialog.dialog("option", "dialogClass", 'warning' )
			.dialog( "option", "buttons", { "Ok": function() { $(this).dialog("close"); } } )
			.dialog( "option", "modal", true )
			.dialog( "option", "title", 'Systémové varování' );
	$dialog.dialog('open');
}

function displayError(text) {
	$dialog.html(text);
	$dialog.dialog("option", "dialogClass", 'error' )
			.dialog( "option", "buttons", { "Ok": function() { $(this).dialog("close"); } } )
			.dialog( "option", "modal", true )
			.dialog( "option", "title", 'Systémová chyba' );
	$dialog.dialog('open');
}


function displayWait(text) {
	$dialog.html(text+'<br /><br /><img src="http://new.xred.cz/images/loading_2.gif" style="margin-left:70px;"/>');
	$dialog.dialog("option", "dialogClass", 'error' )
			.dialog( "option", "buttons", {} )
			.dialog( "option", "modal", true )
			.dialog( "option", "title", 'Prosím čekejte...' );
	$dialog.dialog('open');
}

function standardJqueryAjaxError (XMLHttpRequest, textStatus, errorThrown) {
	switch (textStatus) {
		case 'timeout':
			displayError('Server neodpovídá');
			break;
		case 'error':
			displayError('Při zasílání požadavku nastala chyba');
			break;
		case 'parsererror':
			displayError('Odpověď serveru se nepovedlo zpracovat');
			break;
	}
}

function standardXredAjaxError (event, xhr, options) {
	var response;
	
	// rozprasrujeme vysledek volani
	try {
		response = $.parseJSON(xhr.responseText);
	} catch (e){
		try {
			response = $.parseJSON(xhr.responseText);
		} catch(e) {}
	}

	// pokud byla chyba, vypiseme ji
	if (response._status == 1) {
		var msg = '';
		for (i in response.data) {
			msg += response.data[i]+'<br />';
		}
		displayError(msg);
	}
}

$(document).ready(function(){
	$.ajaxSetup({
		dataType: 'json',
		method: 'GET',
		error : standardJqueryAjaxError
	});
	
	$(document).ajaxSuccess(standardXredAjaxError);
	
	
	$dialog = $('<div></div>', {id: 'dialog_box'})
			.dialog({
				autoOpen: false,
				title: 'Dialogové okno',
				resizable: false
		});
});
