// JavaScript Document
var emailopen = false;

function initatacado(){
	
	$(document).pngFix();
	
	$('.atacadoContainer').fadeOut(0);
	//$('.atacadoLoginContainer').fadeOut(0);
	$('#mensagemEnvio').fadeOut(0);
	
	$('.enviarAT, .buttonCloseAtacado, .cadastroBox, .enviarlogin, .enviarEmail').hover(
	
		function() {
			
			$(this).stop();
			$(this).animate({"backgroundColor": "#FFF", "color" : "#000"}, timeOver);
			
		},
	
		function() {
			
			$(this).stop();
			$(this).animate({"backgroundColor": "#000", "color" : "#fff"}, timeOver);
			
		}
	
	);
	
	$('.senhaBox').hover(
	
		function() {
			
			if(emailopen==false){
				
				$(this).stop();
				$(this).animate({"backgroundColor": "#000", "color" : "#fff"}, timeOver);
				
			}
			
		},
	
		function() {
			
			if(emailopen==false){
				
				$(this).stop();
				$(this).animate({"backgroundColor": "#fff", "color" : "#000"}, timeOver);
				
			}
			
		}
	
	);
	
	$('#enviarEmail').click(function(){
		
		$('#formSenha').submit();
		
	});
	
	$('#formSenha').submit(function() {
		
		$.post('php/enviar_senha.php', $("#formSenha").serialize(), function(data) {
			
			$("#msgEmail").html("Enviando e-mail...");
			$("#formSenha").stop(true, true).fadeOut(300);
			$("#msgEmail").stop(true, true).fadeIn(300);
			
			if(data=="false"){
				
				$("#msgEmail").html("Erro no Envio.");
				$("#msgEmail").stop(true, true).delay(2000).fadeOut(300);
				$("#formSenha").stop(true, true).delay(2000).fadeIn(300);
				
			}else{
				
				$("#msgEmail").html("Senha Enviada.");
				document.getElementById("email").value = "email";
				$("#msgEmail").stop(true, true).delay(2100).fadeOut(0);
				$("#formSenha").stop(true, true).delay(2100).fadeIn(0);
				
				window.setTimeout(function() {
				
					closeEmail();
					$('.senhaBox').animate({"backgroundColor": "#fff", "color" : "#000"}, timeOver);
				
				}, 2000);
			
			}
			
		});
		
		return false;
		
	});
	
	$('#enviarlogin').click(function()
	{	
		$('#formLogin').submit();	
	});
	
	$('#formLogin').submit(function() 
	{	
		$("#msgLogin").html("Fazendo login...");
		$("#msgLogin").stop(true, true).fadeIn(300);
		$.post('php/login.php', $("#formLogin").serialize(), function(data) 
		{		
			if(data=="false")
			{	
				$("#msgLogin").html("Usuário ou senha incorretos.");
				$("#msgLogin").stop(true, true).delay(10000).fadeOut(300);	
			}else
			{	
				var str = data.split(";");
				$('#dados_nome').val(str[0]);
				$('#dados_email').val(str[1]);
				$('#dadosLogin').submit();
				$("#msgLogin").html("Usuário autorizado.");
				$("#msgLogin").stop(true, true).delay(10000).fadeOut(300);
				$("#internaAtacado").css("width", getWindowWidth());
				$("#internaAtacado").stop(true, true).fadeIn(500);	
			}
		});
		return false;
	});
}

function openEmail(){
	
	if(emailopen==true){
	
		closeEmail();
	
	}else{
		
		$('.emailSenhaBox').stop(true, true).show(timeOver);
		emailopen = true;
		
	}
	
}

function closeEmail(){
	
	$('.emailSenhaBox').stop(true, true).hide(timeOver);
	emailopen = false;
	
}

function openCadastro(){
	
	$('.atacadoLoginContainer').fadeOut(500);
	$('.atacadoContainer').delay(500).fadeIn(500);
	
}


function closeCadastro(){
	
	$('.atacadoContainer').fadeOut(500);
	$('.atacadoLoginContainer').delay(500).fadeIn(500);
	
}

function enviaAtacado(form, formFecha) {
	
	$('#'+ form).submit();
	enviaDadosAtacado(formFecha);
	
}

function enviaDadosAtacado(formFecha){
	
	document.getElementById('msgcadastrologin').innerHTML = "";

	if(document.getElementById('senhacadastro').value == document.getElementById('confirme sua senha').value){
	
		document.getElementById('mensagemEnvio').innerHTML = 'Efetuando Cadastro.<div id="mensagemEnvioLinha2">AGUARDE...</div>';
		
		$(formFecha).fadeOut().delay(8500).fadeIn();
		$('#mensagemEnvio').fadeIn().delay(8000).fadeOut();
		
		window.setTimeout(function() {
			
			var mensagem = document.getElementById('contatoPHP').contentWindow.document.getElementById('mensagem').innerHTML;
	
			if(mensagem==1){
		
				document.getElementById('mensagemEnvio').innerHTML = 'Cadastro efetuado com sucesso.<div id="mensagemEnvioLinha2">OBRIGADO PELO INTERESSE.</div>';
				
				document.getElementById('nome').value = 'nome';
				document.getElementById('razão social').value = 'razão social';
				document.getElementById('apelido').value = 'apelido';
				document.getElementById('cpf/cnpj').value = 'cpf/cnpj';
				document.getElementById('ie/rg').value = 'ie/rg';
				document.getElementById('tel').value = 'tel';
				document.getElementById('cel').value = 'cel';
				document.getElementById('endereço').value = 'endereço';
				document.getElementById('cidade').value = 'cidade';
				document.getElementById('e-mail').value = 'e-mail';
				document.getElementById('radion').checked="checked";
				document.getElementById('como conheceu a primart?').value = 'como conheceu a primart?';
				document.getElementById('login').value = 'login';
				document.getElementById('senhacadastro').value = 'senha';
				document.getElementById('senhacadastro').type = "text";
				document.getElementById('confirme sua senha').value = 'confirme sua senha';
				document.getElementById('confirme sua senha').type = "text";
				document.getElementById('informações adicionais').value = 'informações adicionais';
				
			}else{
		
				document.getElementById('mensagemEnvio').innerHTML = 'Erro no cadastro.<div id="mensagemEnvioLinha2">TENTE NOVAMENTE.</div>';
		
			}
		
		}, 2000);
	
	}else{
		
		document.getElementById('senhacadastro').value = '';
		document.getElementById('senhacadastro').type = "text";
		document.getElementById('confirme sua senha').value = 'confirme sua senha';
		document.getElementById('confirme sua senha').type = "text";
		document.getElementById('senhacadastro').focus();
		
		document.getElementById('msgcadastrologin').innerHTML = "senhas não coincidem";
		
	}

}

$(function(){
	logoutAtacado = function()
	{	
		$("#msgLogin").html("");
		$('#usuario').val('usuario');
		$('#senha').val('');
		$('#senha').hide();
		$('#fakesenha').show();
		$("#internaAtacado").html('');
		$("#internaAtacado").stop(true, true).fadeOut(300);	
	}
});
