(function vestaUIHelpers($) {

	$.vestaUI = {
		
		openDialog : function(panelName, buttons) {
			if ($(panelName).hasClass('ui-dialog-content')) {
				$(panelName).dialog('open');
			} else {
				$(panelName).dialog({
					stack: true,
					bgiframe: true,
					height: 200,
					modal: true,
					closeOnEscape: false,
					draggable: false,
					resizable: false,
					buttons: buttons
				});	
			}
		
			return false;
		},
	
		closeDialog : function(panelName) {
			$(panelName).dialog('close');
		
			return false;
		},

		startLoading : function() {
			return $.vestaUI.openDialog('#loadingDialog');
		},
	
		stopLoading : function() {
			return $.vestaUI.closeDialog('#loadingDialog');
		},
	
		doGenericError : function(errorMessage, callback) {
			$('#genericRecoverableErrorDialog .message').text(errorMessage);
		
			if ($("#genericRecoverableErrorDialog").hasClass('ui-dialog-content')) {
				$("#genericRecoverableErrorDialog").dialog('open');
			} else {
				$('#genericRecoverableErrorDialog').dialog({
					stack: true,
					bgiframe: true,
					height: 180,
					modal: true,
					closeOnEscape: true,
					draggable: false,
					resizable: false,
					buttons: {
						'OK' : function() {
							$(this).dialog('close');
						
							if (callback) {
								callback();
							}
						}
					}
				});	
			}
		},
	
		doGenericNotice : function(message, callback) {
			$('#genericMessageDialog .message').text(message);
		
			if ($("#genericMessageDialog").hasClass('ui-dialog-content')) {
				$("#genericMessageDialog").dialog('open');
			} else {
				$('#genericMessageDialog').dialog({
					stack: true,
					bgiframe: true,
					height: 180,
					modal: true,
					closeOnEscape: true,
					draggable: false,
					resizable: false,
					buttons: {
						'OK' : function() {
							$(this).dialog('close');
						
							if (callback) {
								callback();
							}
						}
					}
				});	
			}
		},
	
		manageJsonResponse : function(data, textStatus) {
			if (data.isError) {
				if (data.errorType == 'sessionExpired') {
					$('#sessionErrorDialog').dialog({
						stack: true,
						bgiframe: true,
						height: 180,
						modal: true,
						closeOnEscape: false,
						draggable: false,
						resizable: false
					});	
				} else if (data.errorType == 'formError') {
					$(document.body).highlightFormErrors(data);
					$.vestaUI.doGenericError("The operation was not successful. Please correct the errors reported on the page and try again.");
				} else {
					$('#genericErrorDialog .message').text(data.errorMessage);
					$('#genericErrorDialog').dialog({
						stack: true,
						bgiframe: true,
						height: 180,
						modal: true,
						closeOnEscape: false,
						draggable: false,
						resizable: false
					});	
				}
			
				return false;
			}
		
			return true;
		},
	
		doJsonCall : function(callback, operation, data) {
			$.vestaUI.startLoading();

			if (document.location.href.indexOf("www") >= 0 && jsonUrl.indexOf("www") === -1) {
				jsonUrl = jsonUrl.replace(/\/phparch.com\//, "/www.phparch.com/");
			}
		
			if (!data) {
				data = {};
			}
		
			data.operation = operation;
			data.nonce = $("#inferno-nonce").text();
			
			$.post(jsonUrl, 

				data,
			
				function(data, textStatus) {
					$.vestaUI.stopLoading();
				
					if ($.vestaUI.manageJsonResponse(data, textStatus)) {
						callback(data);
					}
				},
			
				"json");
		}
		
	}
})(jQuery);

