/**
* Interface para dialogos de erro. Esta interface foi implementada em jscript ao invés de 
* php para permitir que a lógica do lado do cliente também possa emitir erros sem precisar
* de outra interface para isto.
*/

function ErrorMessage(message, type)
{
	this.message = message;
	this.type = type;
}

function ErrorDialog()
{
	this.numberOfMessages = 0;
	this.messages = new Array();
	
	/**
	 * Adiciona um erro, oriundo do Form_Ajax_Response
	 * @param Object errorObject 
	 * @param String type Error/Warning
	 */
	this.addError = function(errorObject)
	{
		this.addMessage(errorObject.message, errorObject.type);
	} 
	
	/**
	 * Adiciona uma exceção de java script
	 * @param Exception exception 
	 * @param String type Error/Warning
	 */
	this.addException = function(exception, type)
	{
		if(!type){
			type='Error';
		}
		this.addMessage(exception, type);
	}
		
	/**
	 * Adiciona uma mensagem
	 * @param String message 
	 * @param String type Error/Warning
	 */
	this.addMessage = function(message, type)
	{
		if(!type){
			type='Error';
		}
		this.messages[this.numberOfMessages] = new ErrorMessage(message, type);
		this.numberOfMessages++;
	}
	
	/**
	 * Exibe o dialogo de erro
	 */
	this.show = function()
	{
		this.div = this.getDivErrorDialog();
		this.renderMessages();
		//this.appendButton();
		
		$(this.div).dialog({
			 open: error_ErrorDialogOnOpen,
			 close: error_ErrorDialogOnClose,
			 focus: error_ErrorDialogOnFocus,
			 modal: true
			 });

		if(this.numberOfMessages<5){
			newHeight='auto';
		}else{
			newHeight=301;
		}
		
		if(loginManager.isMobileView()){
			newWidth = 'auto';
		}else{
			newWidth = 666;
		}
		
		$(this.div).dialog( "option", "buttons", { "Fechar": function() { dialogManager.close(this.id); } } );
		$(this.div).dialog( "option", "title", 'Relação de erros:');
		$(this.div).dialog( "option", "height", newHeight);
		$(this.div).dialog( "option", "maxHeight", 301);
		$(this.div).dialog( "option", "width", newWidth);
		$(this.div).dialog( "option", 'position', 'center');
		dialogManager.open(this.div.id);
		$(this.div).resize();
		
		
			
		//this.button.focus();
	}
	
	this.getDivErrorDialog = function()
	{
		div = document.getElementById('errorDialog');
		if(!div){
			div = document.createElement('div');
			div.name='errorDialog';
			div.id='errorDialog';		
		}
		
		div.innerHTML="";
		return div;
	}
		
	this.renderMessages= function()
	{
		listaErros = document.createElement('div');
		listaErros.id='listaErros';
		
		//Babaquice da microsoft 001
//		if(document.all){
//			listaErros.className='listaErros';
//		}else{
//			listaErros.class='listaErros';
//		}
		$(listaErros).addClass('listaErros');
		
		this.div.appendChild(listaErros);		
		
		
		listaErros.innerHTML = "";
		this.listaErros = listaErros;
				
		contaErros=0;
		for(var erro in this.messages){
			contaErros++;
			divErro = document.createElement('div');
			divErro.id = this.messages[erro].type + 'type';
						
			if(contaErros==1){
				divErro.className='erroImpar';				
			}else{
				divErro.className='erroPar';
				contaErros=0;
			}
			
			if(this.messages[erro].type=='Error'){
				imagem = EW_IMG_URL + 'form/error_error.png';
			}else{
				imagem = EW_IMG_URL + 'form/error_warning.png';
			}
			
			divErro.innerHTML="<div class='"+this.messages[erro].type +"type'><div class='ImgError'><img src='"+ imagem +"'/></div><div class='MsgError'>" + this.messages[erro].message + "</div>";
			listaErros.appendChild(divErro);
		}
		
	}
	
	this.appendButton = function()
	{
		
		return;
		button = document.createElement('input');
		button.type='button';
		button.value='Fechar';
		button.id='FechaErrorDialog';
		button.onclick=function(){		
			//$('#errorDialog').dialog('close');
			dialogManager.close('errorDialog');
			return false;
		}
		this.div.appendChild(button);
		this.button = button;
	}
}

function error_ErrorDialogOnFocus()
{
		
}

function error_ErrorDialogOnOpen()
{
	form_registerDialog('errorDialog');
	$('.ui-dialog-content').each(function(key, value)
	{		
		if($('#'+value.id).dialog('isOpen') && value.id != 'errorDialog'){
			$('#'+value.id).dialog('disable');	
			$('#'+value.id).attr('disbledBy','ErrorDialog');
		}
	});
}

function error_ErrorDialogOnClose()
{
	form_unregisterDialog('errorDialog');
	
	/*Reabilita todos os dialogs que estavam abertos*/
	$('.ui-dialog-content').each(function(key, value)
	{		
		if($('#'+value.id).dialog('isOpen') && value.id != 'errorDialog'){
			if($('#'+value.id).attr('disbledBy') == 'ErrorDialog'){
				$('#'+value.id).dialog('enable');
			}
		}
	});
}

function error_showMessage(message, type)
{	
	errorDialog = new ErrorDialog();
	errorDialog.addMessage(message, type);
	errorDialog.show(); 
}

