// JavaScript Document
var posAtual = 0;
$(document).ready(function()
{
	window.onresize = resize;
	posicionaContainer(0);
	
	$('.twitter, .facebook').hover(
		function()
		{
			$(this).children('.over').stop(true, true).fadeIn();
		},
		function()
		{
			$(this).children('.over').stop(true, true).fadeOut();
		}
	);
	
	$('.botaoAdesaoHome, .botaoPlanosHome, .botaoRede, .botaoEnviar, .botaoEnviarAdesao').hover(
		function()
		{
			$(this).children('.over').stop(true, true).fadeIn();
		},
		function()
		{
			$(this).children('.over').stop(true, true).fadeOut();
		}
	);
	$('.botaoAdesaoHome, .botaoPlanosHome').click(
		function()
		{
			var nr = Number($(this).attr("data-nr"));	
			posAtual = nr;
			movetopage(nr);
			return false;
		}
	);
	
	$('.botaoEnviar').click(function()
	{	
		$('#contact_form').submit();	
	});
	$('#contact_form').submit(function() 
	{	
		$(".containerContatoMensagem").html("Enviando Mensagem.<br /><span>Aguarde...</span>");
		$(".containerContatoMensagem").stop(true, true).animate({"left": 0}, 500);
		$(".containerContatoForm").stop(true, true).animate({"left": -450}, 500);
		
		$.post('php/contact_mail.php', $("#contact_form").serialize(), function(data) 
		{	
			if(data=="false")
			{	
				$(".containerContatoMensagem").html("Erro no Envio da Mensagem!<br /><span>tente Novamente.</span>");
				$(".containerContatoMensagem").stop(true, true).delay(6000).animate({"left": -450}, 500);
				$(".containerContatoForm").stop(true, true).delay(6000).animate({"left": 0}, 500);
			}else
			{	
				$(".containerContatoMensagem").html("Mensagem Enviada!<br /><span>Obrigado pelo Contato.</span>");
				document.getElementById("nome").value = "";
				document.getElementById("email").value = "";
				document.getElementById("telefone").value = "";
				document.getElementById("mensagem").value = "";
				$(".containerContatoMensagem").stop(true, true).delay(6000).animate({"left": -450}, 500);
				$(".containerContatoForm").stop(true, true).delay(6000).animate({"left": 0}, 500);
			}
		});
		return false;
	});
	
	$('.botaoEnviarAdesao').click(function()
	{	
		alert("envia");	
	});
	
	$('.contato_input, .contato_select').blur(function()
	{	
		fieldBlur(this);
		
		tipoValidacao = $(this).attr("data-val");
		if(tipoValidacao!=undefined)
		{
			tipoValidacao = tipoValidacao.toLowerCase();
			var text = $(this).val();
			
			switch(tipoValidacao)
			{
				case "name":
					var words = text.split(" ");
					if(words.length<2)
					{
						$(this).parent('.campoContainer').stop(true, true).animate({borderColor:"#ef0000"}, 200);	
					}else
					{
						for(var i=0; i<words.length; i++)
						{
							if(words[i].length<2)
							{
								$(this).parent('.campoContainer').stop(true, true).animate({borderColor:"#ef0000"}, 200);
							}
						}
					}
					break;
				case "date":
					if(validaData(text)==false)
					{
						$(this).parent('.campoContainer').stop(true, true).animate({borderColor:"#ef0000"}, 200);
					}
					break;
				case "cpf":
					if(validaCPF(text)==false)
					{
						$(this).parent('.campoContainer').stop(true, true).animate({borderColor:"#ef0000"}, 200);
					}
					break;
				case "email":
					if(validaEmail(text)==false)
					{
						$(this).parent('.campoContainer').stop(true, true).animate({borderColor:"#ef0000"}, 200);
					}
					break;
				default:
			}
		}
	});
	$('.contato_input, .contato_select').focus(function()
	{	
		fieldFocus(this);	
	});
	
	$('.contato_input').keydown(function(event)
	{
		tipoValidacao = $(this).attr("data-val");
		tipoValidacao = tipoValidacao.toLowerCase();	
		
		var code = (event.keyCode ? event.keyCode : event.which);
		code = Number(code);
		
		switch(tipoValidacao)
		{
			case "name":
			 	if((code>=48&&code<=57)||(code>=96&&code<=105))
				{
					event.preventDefault();	
				}else
				{
					var vl = $(this).val();
					if(vl.length==1)
					{
						$(this).val(vl.toUpperCase());
					}
				}
			  	break;
			case "date":
			  	if((code>=48&&code<=57)||(code>=96&&code<=105)||code==13||code==8||code==9)
				{
					var vl = $(this).val();
					if((vl.length==2||vl.length==5)&&(code>=48&&code<=57)||(code>=96&&code<=105))
					{
						$(this).val(vl+"/");	
					}
				}else
				{
					event.preventDefault();	
				}
			  	break;
			case "datecard":
			  	if((code>=48&&code<=57)||(code>=96&&code<=105)||code==13||code==8||code==9)
				{
					var vl = $(this).val();
					if((vl.length==2)&&(code>=48&&code<=57)||(code>=96&&code<=105))
					{
						$(this).val(vl+"/");	
					}
				}else
				{
					event.preventDefault();	
				}
			  	break;
			case "number":
				if((code>=48&&code<=57)||(code>=96&&code<=105)||code==13||code==8||code==9)
				{
				}else
				{
					event.preventDefault();	
				}
			  	break;
			case "alphanumber":
			  	if((code>=48&&code<=90)||(code>=96&&code<=105)||code==13||code==8||code==9)
				{
					
				}else
				{
					event.preventDefault();	
				}
			  	break;
			case "cpf":
				if((code>=48&&code<=57)||(code>=96&&code<=105)||code==13||code==8||code==9)
				{
				}else
				{
					event.preventDefault();	
				}
			 	break;
			case "email":
			  	break;
			default:
		}
	});
	
	$('.menuitem').hover(
		function()
		{
			$(this).children('.itembg').stop(true, true).fadeIn();
			$(this).children('span').stop(true, true).animate({color:"#2c427a"}, 300);
		},
		function()
		{
			var nr = Number($(this).attr("data-nr"));
			if(nr!=posAtual)
			{
				$(this).children('.itembg').stop(true, true).fadeOut();
				$(this).children('span').stop(true, true).animate({color:"#FFF"}, 300);
			}
		}
	);
	
	$('.menumap').hover(
		function()
		{
			var id = $(this).attr("data-menu");
			$("#"+id).children('.itembg').stop(true, true).fadeIn();
			$("#"+id).children('span').stop(true, true).animate({color:"#2c427a"}, 300);
		},
		function()
		{
			var id = $(this).attr("data-menu");
			var nr = Number($(this).attr("data-nr"));
			if(nr!=posAtual)
			{
				$("#"+id).children('.itembg').stop(true, true).fadeOut();
				$("#"+id).children('span').stop(true, true).animate({color:"#FFF"}, 300);
			}
		}
	);
	$('.menumap, .menuitem').click(
		function()
		{
			var nr = Number($(this).attr("data-nr"));	
			posAtual = nr;
			movetopage(nr);
			return false;
		}
	);
	
	$(function()
	{	
		var settings = 
		{
			showArrows: false,
			verticalDragMaxHeight: 60,
			autoReinitialise: true
		};
		$('.containerConteudoSlide').jScrollPane(settings);
		$('.termosContainer').jScrollPane(settings);
	});
	
	movetopage(0);
});

function movetopage(id)
{
	for(var i=0; i<$(".menuitem").length; i++)
	{
		if(i==id)
		{
			$("#menu"+i).children('.itembg').stop(true, true).fadeIn();
			$("#menu"+i).children('span').stop(true, true).animate({color:"#2c427a"}, 300);
		}else
		{
			$("#menu"+i).children('.itembg').stop(true, true).fadeOut();
			$("#menu"+i).children('span').stop(true, true).animate({color:"#FFF"}, 300);
		}
	}
	var dif = 1;//Math.abs(id-posAtual);
	var pos = -id * 1225;
	$('.contentItens').stop(true, true).animate({left: pos}, {queue:false, duration: 800, easing:"quartEaseInOut"});
}

function fieldBlur(obj)
{
	$(obj).parent('.campoContainer').stop(true, true).animate({borderColor:"#aaa"}, 200);
}

function fieldFocus(obj)
{
	$(obj).parent('.campoContainer').stop(true, true).animate({borderColor:"#2c427a"}, 200);
}

function validaData(data)
{
	var valido = false;
    if(data.length<10)
	{
    	valido = false;
	}else
	{
		var hoje = new Date();	
      	var dia = data.split("/")[0];
      	var mes = data.split("/")[1];
     	var ano = data.split("/")[2];
     	var myData = new Date(ano, mes - 1, dia);
		
      	if((myData.getMonth() + 1 != mes)||(myData.getDate() != dia)||(myData.getFullYear() != ano)||myData>=hoje)
	  	{
			valido = false;
	  	}else
	  	{
        	valido = true;
	  	}
    }
    return valido;
}

function validaEmail(email)
{
	var txt = email;
	if ((txt.length != 0) && ((txt.indexOf("@") < 1) || (txt.indexOf('.') < 7)))
	{
		return false;
  	}else
	{
		return true;	
	}
}

function validaCPF(cpf)
{
	var numeros, digitos, soma, i, resultado, digitos_iguais;
	digitos_iguais = 1;
	
	if (cpf.length < 11) 
		return false;
	for (i = 0; i < cpf.length - 1; i++)
            if (cpf.charAt(i) != cpf.charAt(i + 1))
                  {
                  digitos_iguais = 0;
                  break;
                  }
      if (!digitos_iguais)
            {
            numeros = cpf.substring(0,9);
            digitos = cpf.substring(9);
            soma = 0;
            for (i = 10; i > 1; i--)
                  soma += numeros.charAt(10 - i) * i;
            resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
            if (resultado != digitos.charAt(0))
                  return false;
            numeros = cpf.substring(0,10);
            soma = 0;
            for (i = 11; i > 1; i--)
                  soma += numeros.charAt(11 - i) * i;
            resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
            if (resultado != digitos.charAt(1))
                  return false;
            return true;
            }
      else
            return false;
}

function posicionaContainer(time)
{	
	var widthW = getWindowWidth();
	widthW = (widthW<1300) ? 1300: widthW;
	
	var heightW = getWindowHeight();
	heightW = (heightW<830) ? 830: heightW;
	
	var ml = (widthW - 1300) / 2;
	var mt = (heightW - 830) / 3;
	
	$('.containerGeral').css({top: mt, left:ml});
}

function resize()
{	
	posicionaContainer(0);
}

function getWindowWidth() 
{	
	var myWidth = 0;	  
	if( typeof( window.innerWidth ) == 'number' )
	{
		myWidth = window.innerWidth;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
	{
		myWidth = document.documentElement.clientWidth;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
	{
		myWidth = document.body.clientWidth;
	}
	return myWidth;
}

function getWindowHeight() 
{	
 	var myHeight = 0; 
  	if( typeof( window.innerWidth ) == 'number' )
	{
		myHeight = window.innerHeight;	
  	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
	{	  
		myHeight = document.documentElement.clientHeight;
  	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
	{
		myHeight = document.body.clientHeight;	
  	}
  	return myHeight;
}
