$(function() {
	dialog = new Object();
	
	$("#dialog_holder").dialog({
		bgiframe: true,
		modal: true,
		autoOpen: false,
		resizable: false,
		stack: true,
		width: 550,
		buttons: {
			Ok: function() {
				dialog.close();
			}
		},
		open: function(event, ui) {
			$(this).parents('.ui-dialog-buttonpane button:eq(0)').focus();
		}
	});
	
	dialog.update = function(message) {
		$("#dialog_holder").html(message);
		$('#dialog_holder').dialog('option', 'position', 'center');
	};
	
	dialog.message = function(title, message) {
		dialog.setWidth("550px");
		dialog.close();
		$("#dialog_holder").html(message);
		$("#dialog_holder").dialog("option", "title", title);
		$('#dialog_holder').dialog('option', 'buttons', { 
			"Ok": function() { 
				dialog.close();
			}
		});
		$("#dialog_holder").dialog('open');
	};
	
	dialog.close = function() {
		$("#dialog_holder").dialog('close');
	};
	
	dialog.open = function() {
		$("#dialog_holder").dialog('open');
	};
	
	dialog.setTitle = function(title) {
		$("#dialog_holder").dialog("option", "title", title);
	};
	
	dialog.setButtons = function(buttons) {
		$('#dialog_holder').dialog('option', 'buttons', buttons);
	};
	
	dialog.setWidth = function(width) {
		$('#dialog_holder').dialog('option', 'width', width);
		$('#dialog_holder').dialog('option', 'position', 'center');
	};
	
	dialog.setHeight = function(height) {
		$('#dialog_holder').height(height);
		$('#dialog_holder').dialog('option', 'position', 'center');
	};
	
	dialog.autoWidth = function(method) {
		if (jQuery.browser.msie && jQuery.browser.version == 6) {
			dialog.setWidth(100);
			dialog.setWidth($("#dialog_holder").width() + 24);
		}
		else if (jQuery.browser.msie && jQuery.browser.version >= 7) {
			dialog.setWidth("auto");
			dialog.setWidth($("#dialog_holder").width() + 24);
		} 
		else {
			dialog.setWidth("auto");
		}
	};
	
	dialog.autoSize = function(method) {
		if (jQuery.browser.msie && jQuery.browser.version == 6) {
			dialog.setWidth(100);
			dialog.setWidth($("#dialog_holder").width() + 24);
		}
		else if (jQuery.browser.msie && jQuery.browser.version >= 7) {
			dialog.setWidth("auto");
			dialog.setWidth($("#dialog_holder").width() + 24);
		} 
		else {
			var width = $("#dialog_holder > img").width();
			var height = $("#dialog_holder").height();
			
			$("#dialog_holder").animate({
				width: width,
				height: $("#dialog_holder > img").height()
			}, 'slow');

			if ($("#dialog_holder").parent(".ui-dialog").position().left != $(document).width() / 2 - width / 2) {
				$("#dialog_holder").parent(".ui-dialog").animate({
					left: $(document).width() / 2 - width / 2
				}, 'slow');
			}
			
			//dialog.setWidth(width);
			//dialog.setHeight($("dialog_holder").height());
		}
	};

	dialog.confirm = function(title, message, args, yesCallback, noCallback) {
		$("#dialog_holder").html(message);
		$("#dialog_holder").dialog("option", "title", title);
		$('#dialog_holder').dialog('option', 'buttons', { 
			"Cancel" : function() {
				dialog.close();
				
				if (noCallback != undefined) {
					noCallback(args);
				}
			},
			"Ok": function() { 
				dialog.close();
				
				if (yesCallback != undefined) {
					yesCallback(args);
				} 
			}
		});
		$("#dialog_holder").dialog('open');
	};
});