/**
 * AÇÕES PARA A SEÇÃO CONTATO
 * 
 * @author Gibran (gibran@gadbrivia.com.br)
 */

function Contato()
{
	$("#contato a.bt-fechar").click(Contato.exibir);
	$("a.lk-contato").click(Contato.exibir);
	
	// FOCO
	
	$("#" + Contato.campos.ids.join(", #"), "#frmContato").unbind().blur(function()
	{
		if ($.trim($(this).val()).length == 0)
		{
			$(this).val(Contato.campos.valores[$.inArray($(this).attr("id"), Contato.campos.ids)]);
		}
	}).focus(function()
	{
		if ($.inArray($.trim($(this).val()), Contato.campos.valores) > -1)
		{
			$(this).val("");
		}
	});
	
	
	$('#txtContatoTelefone').blur(function () 
	{
		var valor = $(this).val();
		
		if(valor.replace('(', '').replace(')', '').replace(' ', '').replace('.', '').replace('_','').length < 10)
		{
			$(this).val('');
			$('#txtContatoTelefone').unmask();
			$('#txtContatoTelefone').val(SITE_FORM_FONE);
		}		
	});
	
	$('#txtContatoTelefone').focus(function () 
	{
		$('#txtContatoTelefone').unmask();
		$('#txtContatoTelefone').mask('(99) 9999.9999');
	});
	
	// MÁSCARA
	
	
	
	// OPÇÕES DE PAÍS, ESTADO E CIDADE
	
	$("#selContatoPais", "#frmContato").change(function()
	{		
		if ($.trim($(this).val()) == SITE_BRASIL_ID)
		{
			Contato.brasil = true;
			
			$("#selContatoEstado, #selContatoCidade", "#frmContato").parent("li").removeClass("hidden");
			
			$("#txtContatoEstado", "#frmContato").parent("li").addClass("hidden");
			
			Contato.carregarEstados($(this).val());
		}
		else
		{
			Contato.brasil = false;
			
			$("#selContatoEstado", "#frmContato").attr({
				disabled: true
			}).html("<option value=\"\" selected=\"selected\">Estado:</option>");
			
			$("#selContatoCidade", "#frmContato").attr({
				disabled: true
			}).html("<option value=\"\" selected=\"selected\">Cidade:</option>");
			
			$("#selContatoEstado, #selContatoCidade", "#frmContato").parent("li").addClass("hidden");
			
			$("#txtContatoEstado", "#frmContato").parent("li").removeClass("hidden");
		}
	});
	
	$("#selContatoEstado", "#frmContato").change(function()
	{
		if ($.trim($(this).val()).length > 0)
		{
			Contato.carregarCidades($(this).val());
		}
		else
		{
			$("#selContatoCidade", "#frmContato").attr({
				disabled: true
			}).html("<option value=\"\" selected=\"selected\">Cidade:</option>");
		}
	});
	
	// VALIDAÇÃO
	
	$("#frmContato").validate({
		submitHandler: function()
		{
			Contato.bloquear(true);
			
			$("#mensagemContato").html("Enviando...").attr({
				"class": ""
			});
			
			$.ajax({
				cache: false,
				data: {
					txtContatoNome: $("#txtContatoNome", "#frmContato").val(),
					txtContatoEmail: $("#txtContatoEmail", "#frmContato").val(),
					txtContatoTelefone: $("#txtContatoTelefone", "#frmContato").val(),
					selContatoPais: $("#selContatoPais", "#frmContato").val(),
					selContatoEstado: $("#selContatoEstado", "#frmContato").val(),
					txtContatoEstado: $("#txtContatoEstado", "#frmContato").val(),
					selContatoCidade: $("#selContatoCidade", "#frmContato").val(),
					selContatoSetor: $("#selContatoSetor", "#frmContato").val(),
					txaContatoMensagem: $("#txaContatoMensagem", "#frmContato").val()
				},
				dataType: "json",
				success: function(resposta)
				{
					Contato.bloquear(false);
					
					$("#mensagemContato").html(resposta.mensagem);
					
					if (resposta.sucesso)
					{
						$("#mensagemContato").attr({
							"class": "sucesso"
						});
						
						Contato.limpar();
					}
					else
					{
						$("#mensagemContato").attr({
							"class": "erro"
						});
					}
				},
				type: "post",
				url: $("#frmContato").attr("action")
			});
		},
		errorPlacement: function(error, element)
		{
			$("#mensagemContato").attr("class", "aviso-erro");
			$("#mensagemContato").html(error);
		},
	    errorElement: "span",
		highlight: function(element, errorClass)
		{
			$(element).css("border", "1px solid #FF0000");
		},
		unhighlight: function(element, errorClass)
		{
			$(element).css("border", "1px solid #E4E4E4");
		},
		rules: {
			txtContatoNome: {
				required: function()
				{
					if ($.inArray($.trim($("#txtContatoNome", "#frmContato").val()), Contato.campos.valores) > -1)
					{
						$("#txtContatoNome", "#frmContato").val("");
					}
					
					return true;
				}
			},
			txtContatoEmail: {
				required: function()
				{
					if ($.inArray($.trim($("#txtContatoEmail", "#frmContato").val()), Contato.campos.valores) > -1)
					{
						$("#txtContatoEmail", "#frmContato").val("");
					}
					
					return true;
				},
				email: true
			},
			selContatoSetor: {
				required: true
			},
			txaContatoMensagem: {
				required: function()
				{
					if ($.inArray($.trim($("#txaContatoMensagem", "#frmContato").val()), Contato.campos.valores) > -1)
					{
						$("#txaContatoMensagem", "#frmContato").val("");
					}
					
					return true;
				}
			}
		},
		messages: {
			txtContatoNome: {
				required: MSG04
			},
			txtContatoEmail: {
				required: MSG04,
				email: MSG27.replace(/\{CAMPO\}/gim, "e-mail")
			},
			selContatoSetor: {
				required: MSG04
			},
			txaContatoMensagem: {
				required: MSG04
			}
		}
	});
}

Contato.brasil = false;

Contato.campos = {
	ids: ["txtContatoNome", "txtContatoEmail", "txtContatoEstado", "txaContatoMensagem"],
	valores: [SITE_FORM_NOME, SITE_FORM_EMAIL, SITE_FORM_ESTADO, SITE_FORM_ESTADO]
}

Contato.bloquear = function(acao)
{
	$(":input", "#frmContato").attr({
		disabled: acao
	});
}

Contato.carregarEstados = function(pais)
{
	$("#selContatoEstado", "#frmContato").attr({
		disabled: true
	}).html("<option value=\"\" selected=\"selected\">Carregando...</option>");
	
	$.ajax({
		cache: false,
		success: function(opcoes)
		{
			$("#selContatoEstado", "#frmContato").html("<option value=\"\" selected=\"selected\">Estado:</option>" + opcoes).attr({
				disabled: false
			});
		},
		data: {
			PaisID: pais
		},
		type: "post",
		url: "acao/Estado.php"
	});
}

Contato.carregarCidades = function(estado)
{
	$("#selContatoCidade", "#frmContato").attr({
		disabled: true
	}).html("<option value=\"\" selected=\"selected\">Carregando...</option>");
	
	$.ajax({
		cache: false,
		success: function(opcoes)
		{
			$("#selContatoCidade", "#frmContato").html("<option value=\"\" selected=\"selected\">Cidade:</option>" + opcoes).attr({
				disabled: false
			});
		},
		data: {
			UnidadeFederativaID: estado
		},
		type: "post",
		url: "acao/Cidade.php"
	});
}

Contato.exibir = function()
{
	Contato.limpar();
	
	$("a.lk-contato").parent("li").toggleClass("ativo");
	$("div#contato").toggleClass("hidden");
}

Contato.limpar = function()
{
	$("#" + Contato.campos.ids.join(", #")).each(function()
	{
		if ($.inArray($.trim($(this).val()), Contato.campos.valores) > -1)
		{
			$(this).val(Contato.campos.valores[$.inArray($(this).attr("id"), Contato.campos.ids)]);
		}
		else
		{
			$(this).val("");
		}
	});
}