///*
//	#################################################
//	#    Auto: Thiago Silva (Pedreiro Digital)      #
//	#          www.pedreirodigital.com.br           #       
//	#################################################
//*/
//
////definindo variaveis
var modulo;
var folha;
var idIMG;
var idIMGFoto;
var id;
var idComentario;
////classe Mural 
var Mural = function()
{
}
//
////listar
Mural.prototype.listar = function(folha)
{
	document.getElementById("navegacao").innerHTML = (folha+1)+" / "+limite;
	objAjax.include(Host + 'listar.php','folha='+folha,'dados');
}	

////status
Mural.prototype.status = function(_id)
{
	idIMG = _id;
	objAjax.executar(Host + 'status.php','id='+_id,Mural.prototype.ReturnStatus);
}
////atualizar staus
Mural.prototype.ReturnStatus = function()
{
	if (xhReq.readyState == 4)
	{
		resposta = xhReq.responseXML;
		if (resposta)
		{
			var obj = resposta.getElementsByTagName('dados');
			nova = obj[0].getElementsByTagName('imagem')[0].firstChild.nodeValue;
			document.getElementById("status"+idIMG).src = 'imagens/icones/'+nova;
		}
		else 
		{ 
			//alerta campo vazios
			objAlerta.config('Erro no sistema','não foi posivél abrir o <b>modúlo</b>!',200,'vermelho',false);
			objAlerta.alert('objUtil.ok()','','OK');
			objAlerta.exibir();
		}
	}
}


//excluir
Mural.prototype.excluir = function(_id)
{
	objAlerta.config('Atenção','Deseja excluir este recado?!',200,'amarelo',true);
	objAlerta.alert('Mural.prototype.remover('+_id+')','Mural.prototype.cancelar()','SIM|NÂO');
	objAlerta.exibir();
}

//atualizar staus
Mural.prototype.remover = function(_id)
{
	//alerta campo vazios
	objAjax.executar(Host + 'excluir.php','id='+_id,Mural.prototype.atualizaTime);
}	

//cadastrar novo
Mural.prototype.salvar = function()
{
	var form = document.getElementById("acao");
	if(!id)
	{
		if(!form.de.value || !form.para.value || !form.texto.value)
		{
			objAlerta.config('Campo vazio','preencha os campos e tente novamente!',200,'vermelho',false);
			objAlerta.alert('objUtil.ok()','','OK');
			objAlerta.exibir();
		}
		else
		{
			objAjax.enviar(Host + 'salvar.php','acao',Mural.prototype.ReturnSalvar);
		}
	}
	else
	{
		Mural.prototype.editar(id);
	}
}

//retorno a funcao salvar
Mural.prototype.ReturnSalvar = function()
{
	if (xhReq.readyState == 4)
	{
		resposta = xhReq.responseXML;
		var obj = resposta.getElementsByTagName('dados');
		if (resposta)
		{
			var error = obj[0].getElementsByTagName('msg')[0].firstChild.nodeValue;
			if(error == 'existe')
			{
					//alerta campo vazios
					objAlerta.config('Atenção','Já foi cadastrado!',200,'vermelho',false);
					objAlerta.alert('objUtil.ok()','','OK');
					objAlerta.exibir();
			}
			else
			{
				if(error == 'aceito' || error == 'editado')
				{
					Mural.prototype.atualizaTime();
				}
				else
				{
					//alerta campo vazios
					objAlerta.config('Erro no cadastro','Não foi posivel cadastrar, tente mais tarde!',200,'vermelho',false);
					objAlerta.alert('objUtil.ok()','','OK');
					objAlerta.exibir();
				}
			}
		}
		else 
		{ 
			//alerta campo vazios
			objAlerta.config('Erro no sistema','não foi posivél abrir o <b>modúlo</b>!',200,'vermelho',false);
			objAlerta.alert('objUtil.ok()','','OK');
			objAlerta.exibir();
		}
	}
}

Mural.prototype.salvar_site = function()
{
	var form = document.getElementById("acao");
	if(!form.de.value || !form.para.value || !form.texto.value)
	{
		document.getElementById('status').innerHTML = 'preencha os campos';
	}
	else
	{
		objAjax.enviar('painel/modulos/mural/salvar.php','acao',Mural.prototype.ReturnSalvarSite);
	}
}

//retorno a funcao salvar
Mural.prototype.ReturnSalvarSite = function()
{
	if (xhReq.readyState == 4)
	{
		resposta = xhReq.responseXML;
		var obj = resposta.getElementsByTagName('dados');
		if (resposta)
		{
			var error = obj[0].getElementsByTagName('msg')[0].firstChild.nodeValue;
			location.reload(true);
		}
		else 
		{ 
			document.getElementById('status').innerHTML = 'error';
		}
	}
}

//pegar dados selecionado
Mural.prototype.get = function(_id)
{
	objAjax.executar(Host + 'get.php','id='+_id,Mural.prototype.setget);
}

//exibir os dado selecionado
Mural.prototype.setget = function()
{
	if (xhReq.readyState == 4)
	{
		resposta = xhReq.responseXML;
		if (resposta)
		{
			var form = document.getElementById('acao');
			obj         = resposta.getElementsByTagName('dados');
			xid         = unescape(obj[0].getElementsByTagName('id')[0].firstChild.nodeValue);
			xde     = unescape(obj[0].getElementsByTagName('de')[0].firstChild.nodeValue);
			xpara     = unescape(obj[0].getElementsByTagName('para')[0].firstChild.nodeValue);
			xtexto      = unescape(obj[0].getElementsByTagName('texto')[0].firstChild.nodeValue);
			
			form.id.value    = xid;
			form.de.value    = xde;
			form.para.value  = xpara;
			form.texto.value  = xtexto;
			id               = xid;
		}
		else 
		{ 
			objAlerta.config('Sucesso','Dados adicionar com suceso!',200,'vermelho',false);
			objAlerta.alert('objUtil.ok()','','OK');
			objAlerta.exibir();
		}
	}
}

//funcao editar dados
Mural.prototype.editar = function ()
{
	objAjax.enviar(Host + 'editar.php','acao',Mural.prototype.ReturnSalvar);
}

//atualzia a lista com tempo
Mural.prototype.atualizaTime = function()
{
	if(xhReq.readyState==4)
	{
		setTimeout('Mural.prototype.atualizar()',100);
	}
}

//atualiza a lista com clique
Mural.prototype.atualizar = function()
{
	Mural.prototype.listar(0);
	document.getElementById('de').value= '';
	document.getElementById('para').value= '';
	document.getElementById('texto').value 	  = '';
	document.getElementById('alerta').innerHTML 	  = '';
	document.getElementById('mascara').style.display  = 'none';
	document.getElementById('alerta').style.display   = 'none';
	id                                                = '';
}

//cancela uma ação de um alert
Mural.prototype.cancelar = function()
{
	document.getElementById('de').value= '';
	document.getElementById('para').value= '';
	document.getElementById('texto').value 	  = '';
	document.getElementById('alerta').innerHTML 	  = '';
	document.getElementById('mascara').style.display  = 'none';
	document.getElementById('alerta').style.display   = 'none';
	id                                                = '';
}

Mural.prototype.limparCampos = function()
{
	document.getElementById('de').value= '';
	document.getElementById('para').value= '';
	document.getElementById('texto').value 	  = '';
	id = '';
}

Mural.prototype.ok = function(tipo)
{
	document.getElementById('alerta').innerHTML 	  = '';
	document.getElementById('mascara').style.display  = 'none';
	document.getElementById('alerta').style.display   = 'none';
	
	if(tipo == 1)
	{
		document.getElementById('acao').texto.value = '';
		document.getElementById('acao').email.value = '';
		document.getElementById('acao').comentario.value = '';
	}
}
