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

	// for maximize/restore
	this.maximized = false; 	// FIXME, .data-ba, dialog-o boviteni
	this.prevWidth = 200;
	this.prevHeight = 200;
	this.prevPosition = [100, 100];

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

	// general dialog open method
	this.open = function(jason) {
		if (typeof jason == 'undefined') {
			// default params
			jason = {
				autoOpen: false,
				modal:true,
				width: 580,
				maximizeable: true,
				closeOnEscape: false,
				close: function() {
					$(this).dialog('destroy');
					$(this).remove();
				}
			};
		} else {
			// add further default, customized params
			jason.autoOpen = false;
			if (typeof jason.modal == 'undefined') {
				jason.modal = true;
			}

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

			if (typeof jason.maximizeable == 'undefined') {
				jason.maximizeable = true;
			}

			if (typeof jason.closeOnEscape == 'undefined') {
				jason.closeOnEscape = false;
			}

			var oj = jason.close;
			jason.close = function() {
				$(this).dialog('destroy');
				$(this).remove();
				if (oj) {
					oj();
				}
			}
		}

		if (typeof dialogManager == 'undefined' || !dialogManager.enableMaximize) {
			jason.maximizeable = false;
		}

		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('option', 'title', jason.title);

			if (!$('#'+this.id).data('maximized')) {
				$('#'+this.id).dialog('option', 'width', jason.width);
			}
		}

		$('#'+this.id).dialog('open');
		//$('#'+this.id+' input[type=text]:not(.hasDatepicker):first').focus();
		$('#'+this.id+' div.widgetWrapper').not('div.dateWrapper').find('input[type=text]').first().focus();
		$('#'+this.id).dialog('option', 'position', 'center');

		if (jason.maximizeable && $('#'+this.id).dialog('widget').find('.ui-dialog-titlebar a.fc-maximize').length == 0) {
			var m = $('#'+this.id).dialog('widget').find('.ui-dialog-titlebar .ui-dialog-titlebar-close').
				clone().
				addClass('fc-maximize').
				css('right', 30).
				html('<span class="ui-icon ui-icon-newwin"></span>').
				appendTo($('#'+this.id).dialog('widget').find('.ui-dialog-titlebar')).
				data('dmElement', $('#'+this.id)).
				hover(function() {
					$(this).addClass('ui-state-hover');
				}, function() {
					$(this).removeClass('ui-state-hover');
				}).click(function(e) {
					e.preventDefault();
					if ($(this).data('dmElement').data('maximized')) {
						$(this).data('dmElement').dialog('restore');
					} else {
						$(this).data('dmElement').dialog('maximize');
					}
				});
		}

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

	this.close = function() {
		//$('#'+this.id).dialog('close');
		// most kivettem, mert ha nincs bent akkor is hibat okoz: nem szunik meg bezaraskor a dialog, a HTML sem, es ujranyitaskor a HTML betoltodik, az eventek meg jol elvesznek
		// de most visszakartam mindketott es kivettem fentrol a closet, hatha ez a kombinacio zavart be a modal layernek, mert vegulis close amugy is meghivodik - a mostani problema az volt, hogy a HTML a body vegen maradt remove hianyaban es az ID-zott close gombok nem mentek

		//$('#'+this.id).dialog('destroy');	// hibat okozott valaminel, de minel is?, lehet a modal layer tunt el?
		//$('#'+this.id).remove();

		$('#'+this.id).dialog('close');
	};

	this.error = function(title, content, onclose) {
		this.open({
			title: '<span class="gradient_lightness ui-corner-all x3x-operation" style="margin: 0;"><span class="fc-icon-16-grey fc-icon-16-delete"></span></span>&nbsp;&nbsp;'+title,
			content: '<div class="error"><i class="ico"><!-- e --></i><div class="c">'+content+'</div><div class="clear"><!-- c --></div></div>',
			width: 500,
			close: function(event, ui) {
				if(onclose) eval(onclose);
			},
			buttons: {
				"Ok": function() {
						$(this).dialog('close');
					}
			}
		});
		return $('#'+this.id);
	};

	this.success = function(title, content, onclose) {
		this.open({
			title: '<span class="gradient_lightness ui-corner-all x3x-operation" style="margin: 0;"><span class="fc-icon-16-grey fc-icon-16-successful"></span></span>&nbsp;&nbsp;'+title,
			content: '<div class="success"><i class="ico"><!-- e --></i><div class="c">'+content+'</div><div class="clear"><!-- c --></div></div>',
			width: 500,
			close: function(event, ui) {
				if(onclose) eval(onclose);
			},
			buttons: {
				"Ok": function() {
						$(this).dialog('close');
					}
			}
		});
		return $('#'+this.id);
	}

	this.notice = function(title, content, onclose) {
		this.open({
			title: '<span class="gradient_lightness ui-corner-all x3x-operation" style="margin: 0;"><span class="fc-icon-16-grey fc-icon-16-warning"></span></span>&nbsp;&nbsp;'+title,
			content: '<div class="notice"><i class="ico"><!-- e --></i><div class="c">'+content+'</div><div class="clear"><!-- c --></div></div>',
			width: 500,
			close: function(event, ui) {
				if(onclose) eval(onclose);
			},
			buttons: {
				"Ok": function() {
						$(this).dialog('close');
					}
			}
		});

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

	this.wait = function(title, content) {
		this.open({
			title: '<span class="gradient_lightness ui-corner-all x3x-operation" style="margin: 0;"><span class="fc-icon-16-grey fc-icon-16-history"></span></span>&nbsp;&nbsp;'+title,
			content: '<div class="wait">'+content+'</div>',
			width: 500,
			beforeclose: function(event, ui) {
				$(this).effect('bounce', {times: 2, direction: 'down'}, 300);
				return false;
			}
		});

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

	this.confirm = function(obj, content, yes, no) {
		if (typeof obj == 'string') {
			// NEW confirm, obj = content, content = yes, yes = no :D

			if (typeof yes == 'undefined') yes = function() {};
			if (typeof content == 'undefined') content = function() {};

			this.open({
				title: '<span class="gradient_lightness ui-corner-all x3x-operation" style="margin: 0;"><span class="fc-icon-16-grey fc-icon-16-question"></span></span>&nbsp;&nbsp;'+FClocale.warning,
				close: yes,	// I mean no...
				width: 500,
				content: '<div class="caution"><i class="ico"><!-- e --></i><div class="c">'+obj+'</div><div class="clear"><!-- c --></div></div>',
				buttons: [{
					text: FClocale.no,
					click: function() {
						// ki volt kommentezve a yes, de miert?
						yes();		// YES AGAIN! (I mean, no) (the dialog close calls it anyway)
						$(this).dialog('destroy');
					}
				}, {
					text: FClocale.yes,
					click: function() {
						content();	// YES!
						$(this).dialog('destroy');
					}
				}],
				// FIXME + close, nem minidig fut le?
				open: function() {
					var buttons = $(this).parent('.ui-dialog').find('.ui-dialog-buttonpane').find('button');
					buttons.removeClass('ui-button-text-only').addClass('ui-button-text-icon');
					$(buttons).css({
						padding: '0.4em'	// soz
					}).find('span').each(function() {
						var h = $(this).html();
						$(this).replaceWith(h);
					});
					$(buttons[1]).addClass("fc-save-button").prepend("<span class='fc-icon-16-grey fc-icon-16-successful inline-block'></span>");
					$(buttons[0]).prepend("<span class='fc-icon-16-grey fc-icon-16-delete inline-block'></span>");
				}
			});

			return false;
		} else {
			// OLD confirm
			var myhref;

			if (typeof obj != 'undefined' && obj.href && obj.href.search(/void/i) == -1 && obj.href.search(/#/i)+1 != obj.href.length) {
				myhref = obj.href;
			} else {
				myhref = false;
			}

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

			this.open({
				title: '<span class="gradient_lightness ui-corner-all x3x-operation" style="margin: 0;"><span class="fc-icon-16-grey fc-icon-16-question"></span></span>&nbsp;&nbsp;'+FClocale.warning,
				width: 500, 
				close: function(event, ui) {
					if(no) eval(no);
				},
				/*content: '<div class="caution"><i class="ico"><!-- e --></i><div class="c">'+content+'</div><div class="clear"><!-- c --></div></div>'+
					'<div style="text-align: center;">'+
						'<input type="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon" value="'+FClocale.yes+'" onclick="dialogManager.get(\''+this.id+'\').close();'+yes+(myhref ? 'window.location.href=\''+myhref+'\';' : '')+'" /> '+
						'<input type="button" class="button" value="'+FClocale.no+'" onclick="dialogManager.get(\''+this.id+'\').close();'+no+'" />'+
					'</div>',  EZ A RÉGI TIPUSÚ CONFIRM, CSÚNYA, EZÉRT LECSERÉLTEM! */
				content: '<div class="caution"><i class="ico"><!-- e --></i><div class="c">'+content+'</div><div class="clear"><!-- c --></div></div>',
				buttons: [{
					text: FClocale.no,
					click: function() {
						no;
						$(this).dialog('destroy');
					}
				}, {
					text: FClocale.yes,
					click: function() {
						eval(yes);
						$(this).dialog('destroy');
						if(myhref != ''){
							window.location=myhref;
						}
					}
				}],
			});

			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);
	};

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

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

	this.size = function(w, h) {
		this.width(w);
		this.height(h);
		return $('#'+this.id);
	};

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

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

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

	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, onclose) {
		return this.get().error(title, content, onclose);
	};

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

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

	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 {
			this.windows[name].getUIobject().dialog('destroy');
			delete this.windows[name];
			return true;
		}
	};
}

var dialogManager = new classDialogManager();

// maximize/restore plugin extension
$(function() {
	$.fn.extend($.ui.dialog.prototype, {
		maximize: function() {
			var maximizePrev = {
				position: $(this.element).dialog('option', 'position'),
				width: $(this.element).dialog('option', 'width'),
				height: $(this.element).dialog('option', 'height')
			};

			$(this.element).parents('.ui-dialog').css('position', 'fixed');
			$(this.element).parents('.ui-dialog').find('.ui-resizable-handle').not('.fc-resizable-pane').hide();

			$(this.element).dialog('option', 'position', [0, 0]).
				dialog('option', 'width', $(window).width()).
				dialog('option', 'height', $(window).height()).
				dialog('option', 'draggable', false).
				data('maximizePrev', maximizePrev).
				data('maximized', true);

			this.uiDialog.removeClass('ui-corner-all');

			return this;
		},
		restore: function() {
			var mprev = $(this.element).data('maximizePrev');

			$(this.element).parents('.ui-dialog').css('position', 'absolute');
			$(this.element).parents('.ui-dialog').find('.ui-resizable-handle').show();

			$(this.element).
				dialog('option', 'width', mprev.width).
				dialog('option', 'height', mprev.height).
				dialog('option', 'position', mprev.position).
				dialog('option', 'draggable', true).
				data('maximized', false);

			this.uiDialog.addClass('ui-corner-all');
		}
	});
}(jQuery));

