/* dialogManager v 1.1 */
function dialogWindow(id) {
	this.id = id;

	this.open = function(jason) {
		if (typeof jason == 'undefined') {
			jason = {
				autoOpen: false,
				modal:true,
				width: 580
			};
		} else {
			jason.autoOpen = false;
			if (typeof jason.modal == 'undefined') {
				jason.modal = true;
			}

			if (!jason.width) {
				jason.width = 580;
			}
		}

		if ($('#'+this.id).length == 0) {
			$('body').append('<div id="'+this.id+'" class="dialog" style="text-align: left;">'+jason.content+'</div>');
			$('#'+this.id).dialog(jason);
		} else {
			$('#'+this.id).html(jason.content);
		}

		$('#'+this.id).dialog('open');

		return $('#'+this.id);
	};

	this.close = function() {
		$('#'+this.id).dialog('close');
		$('#'+this.id).dialog('destroy');
		$('#'+this.id).remove();
	};

	this.error = function(title, content) {
		this.open({
			title: title,
			content: '<div class="fps_error ui-corner-all">'+content+'</div>',
			width: 500,
			buttons: {
				"Ok": function() {
						$(this).dialog('close');
					}
			}
		});

		return $('#'+this.id);
	};

	this.success = function(title, content) {
		this.open({
			title: title,
			content: '<div class="fps_success ui-corner-all">'+content+'</div>',
			width: 500,
			buttons: {
				"Ok": function() {
						$(this).dialog('close');
					}
			}
		});

		return $('#'+this.id);
	};

	this.notice = function(title, content) {
		this.open({
			title: title,
			content: '<div class="fps_notice ui-corner-all">'+content+'</div>',
			width: 500,
			buttons: {
				"Ok": function() {
						$(this).dialog('close');
					}
			}
		});

		return $('#'+this.id);
	};

	this.confirm = function(obj, content, yes, no) {
		var myhref;
		if (obj.href && obj.href.search(/void/i) == -1) {
			myhref = obj.href;
		} else {
			myhref = false;
		}

		if (!yes) yes = '';
		if (!no) no = '';

		this.open({
			title: '<img src="'+fps_globalurl+'image/redcaution_small.png" style="vertical-align: middle;" /> '+fps_confirm_warning,
			content: '<div class="fps_caution">'+content+'</div>'+
				'<div style="text-align: center;">'+
					'<input type="button" class="fps_button2" value="'+fps_confirm_yes+'" onclick="dialogManager.get(\''+this.id+'\').close();'+yes+(myhref ? 'window.location.href=\''+myhref+'\';' : '')+'" /> '+
					'<input type="button" class="fps_button" value="'+fps_confirm_no+'" onclick="dialogManager.get(\''+this.id+'\').close();'+no+'" />'+
				'</div>',
			width: 500
		});

		return false;
	};

	this.moveToTop = function() {
		$('#'+this.id).dialog('moveToTop');
		return $('#'+this.id);
	};

	this.center = function() {
		$('#'+this.id).dialog('option', 'position', 'center');
		return $('#'+this.id);
	};

	this.position = function(x, y) {
		$('#'+this.id).dialog('option', 'position', [x, y]);
		return $('#'+this.id);
	};
}

function classDialogManager() {
	this.lastDialogID = 0;
	this.windows = {};

	this.get = function(name) {
		if (typeof name == 'undefined') {
			// hanemattaknevet...
			var tempID = this.lastDialogID+1;
			name = 'dialog_'+tempID;
			//console.log('Undefined window gets a new name: '+name);
		}

		if (typeof this.windows[name] == 'undefined') {
			this.lastDialogID++;
			this.windows[name] = new dialogWindow('dialog_'+this.lastDialogID);
			//console.log('Dialog '+name+' does not exist, new ID: dialog_'+this.lastDialogID);
		} else {
			//console.log('Dialog '+name+' exists');
		}
		return this.windows[name];
	};

	this.getLast = function() {
		for (i in this.windows) {
			// pass
		}

		if (typeof this.windows[i] == 'undefined') {
			return false;
		} else {
			return this.windows[i];
		}
	};

	this.getAll = function() {
		return this.windows;
	};

	// bezarja az UTOLSO ablakot!
	this.closeLast = function() {
		for (i in this.windows) {
			// pass
		}

		if (typeof this.windows[i] == 'undefined') {
			return false;
		} else {
			this.windows[i].close();
			this.disztroj(i);
			return true;
		}
	};

	// ez meg az osszeset
	this.closeAll = function() {
		var gotWindow = false;
		for (i in this.windows) {
			this.windows[i].close();
			this.disztroj(i);
			gotWindow = true;
		}
		return gotWindow;
	};

	this.error = function(title, content) {
		return this.get().error(title, content);
	};

	this.success = function(title, content) {
		return this.get().success(title, content);
	};

	this.notice = function(title, content) {
		return this.get().notice(title, content);
	};

	this.confirm = function(obj, content, yes, no) {
		return this.get().confirm(obj, content, yes, no);
	};

	this.destroy = function(name) {
		return this.disztroj(name);
	};

	this.disztroj = function(name) {
		if (typeof this.windows[name] == 'undefined') {
			return false;
		} else {
			delete this.windows[name];
			return true;
		}
	};
}

var dialogManager = new classDialogManager();


