window.PROGRESS_DIALOGS = [];
ProgressDialog = function (title, message) {
	this.title = title;
	this.message = message;
	this.id = window.PROGRESS_DIALOGS.length;
	window.PROGRESS_DIALOGS[this.id] = this;
	this.progress = 0;
	this.allowHide = false;
	this.element = null;
};

ProgressDialog.prototype.buildDiv = function () {
	if (this.element) {
		return this.element;
	}
	var div = document.createElement('div');
	div.innerHTML = "<div style='margin-top: 20px;' id='progress_" + this.id + "'></div>";
	return this.element = div;
};

ProgressDialog.prototype.randomStop = function () {
	if (this.process) {
		clearInterval(this.process);
		this.process = null;
	}
};

ProgressDialog.prototype.randomRun = function () {
	if (!this.process) {
		this.process = setInterval("window.PROGRESS_DIALOGS["+this.id+"].addRandom();" , 500);
	}
};

ProgressDialog.prototype.addRandom = function () {
	this.progress += Math.round(Math.random() * 10);
	this.setProgress(this.progress);
};


ProgressDialog.prototype.setProgress = function (progress) {
	this.progress = progress;
	jQuery("#progress_" + this.id).progressbar({ value: progress });
};

ProgressDialog.prototype.hide = function () {
	this.allowHide = true;
	jQuery(this.buildDiv()).dialog('close');
};

ProgressDialog.prototype.show = function () {
	var self = this;
	jQuery(this.buildDiv()).dialog({
		open: function(event, ui) { try {$(".ui-dialog-titlebar-close").hide();} catch(e){} },
		modal: true,
		autoOpen: true,
		title: this.title, 
		resizable : false,
		draggable : false,
		width: 400 ,
		beforeclose: function(){ return self.allowHide; }
	});
	this.setProgress(0);
};
