
function NaosDialog(moduleId, dialogId, type, position, output) {

	this.moduleId = moduleId;
	this.dialogId = dialogId;
	this.type = type;
	this.position = position;
	this.output = output;
	
	this.cacheTypeReplaceElement = null;
	this.cacheTypeReplaceChildren = null;

	this.show = function() {
		
		if(this.type == 'replace') {
			
			var id = this.position.split(':');
			if(id.length != 2 || id[0] != 'id') {
				NaosDebug.debug('[NaosDialog]: Invalid position for dialog defined. Note: id mode supported only at the moment.');
				return;
			}
			
			this.cacheTypeReplaceElement = $(id[1]);
			if(!this.cacheTypeReplaceElement) {
				NaosDebug.debug('[NaosDialog]: Element id not found.');
				return;
			}
			
			this.cacheTypeReplaceChildren = new Array();
			
			$A(this.cacheTypeReplaceElement.childNodes).each(function(item) {
					this.cacheTypeReplaceElement.removeChild(item);
					this.cacheTypeReplaceChildren.push(item);
				}.bind(this));
			
			this.cacheTypeReplaceElement.innerHTML = this.output;
			
		} else if(this.type == 'modalbox') {
			Modalbox.show('<div>'+this.output+'</div>', {title: naosConfig.application.title});
		}
	}
	
	this.hide = function(temporary) {
		
		if(this.type == 'replace') {
			
			this.cacheTypeReplaceElement.innerHTML = '';
			
			this.cacheTypeReplaceChildren.each(function(item) {
					this.cacheTypeReplaceElement.appendChild(item);
				}.bind(this));
				
			this.cacheTypeReplaceChildren = null;
			this.cacheTypeReplaceElement = null;
			
		} else if(this.type == 'modalbox') {
			if(temporary == false && Modalbox.initialized == true)
				Modalbox.hide();
		}
	}
}

NaosDialog._dialogs = new Array();

NaosDialog.openDialog = function(moduleId, dialogId, type, position, output) {
	
	for(var i=0; i < NaosDialog._dialogs.length; ++i)
	{
		if(NaosDialog._dialogs[i].moduleId == moduleId && NaosDialog._dialogs[i].dialogId == dialogId)
		{
			NaosDialog._dialogs[i].hide(true);
			NaosDialog._dialogs[i] = new NaosDialog(moduleId, dialogId, type, position, output);
			NaosDialog._dialogs[i].show();
			
			return NaosDialog._dialogs[i];
		}
	}
	
	NaosDialog._dialogs.push(new NaosDialog(moduleId, dialogId, type, position, output));
	NaosDialog._dialogs[NaosDialog._dialogs.length-1].show();
	
	return NaosDialog._dialogs[NaosDialog._dialogs.length-1];
}

NaosDialog.closeDialog = function(moduleId, dialogId) {
	for(var i=0; i < NaosDialog._dialogs.length; ++i)
	{
		if(NaosDialog._dialogs[i].moduleId == moduleId && NaosDialog._dialogs[i].dialogId == dialogId)
		{
			NaosDialog._dialogs[i].hide(false);
			NaosDialog._dialogs.splice(i, 1);
		}
	}
}
