var fAnim;
var fAnimHide;

$(document).ready(function(e){
	posicionaSubMenu();
	
	$('marquee').marquee();
	
	$("#itMenu-empreendimentos").mouseover(function(e){
		posicionaSubMenu();
		$(this).addClass("ativo");
		
		clearTimeout(fAnimHide);
		clearTimeout(fAnim);
		fAnim = setTimeout(function(){
			$(".menuEmp").show();
			$(".menuEmpInterna").show("slide", { direction: "up" }, 500, function(e){
			});
		},0);
	});
	$(".menuEmp").mouseover(function(e){
		clearTimeout(fAnimHide);
		clearTimeout(fAnim);
	});
	$(".menuEmp").mouseleave(function(e){
		clearTimeout(fAnimHide);
		clearTimeout(fAnim);
		fAnim = setTimeout(function(){
			fAnimHide = setTimeout(function(){
				$(".menuEmpInterna").hide("slide", { direction: "up" }, 500, function(e){
					$(".menuEmp").hide();
					if(!$("#itMenu-empreendimentos").hasClass("fixo")){
						$("#itMenu-empreendimentos").removeClass("ativo");
					}
				});
			},3000);
		},0);
	});
	
	if($(".menuEmp li").length > 0){
		$("#itMenu-empreendimentos a").attr("href","").click(function(e){
			e.preventDefault();
			if($(this).parent().hasClass("ativo")){
				$(".menuEmp").trigger("mouseleave");
			}else{
				$(this).parent().trigger("mouseover");
			}
		});
	}
	
	$(".progresso li").mouseover(function(e){
		showTooltipEmp($(this));
	});
	$(".progresso li").mouseleave(function(e){
		$(".tooltipEmp").hide();
		$(".tooltipEmp .percent").html("");
	});
});

$(window).resize(function() {
	posicionaSubMenu();
});

function showTooltipEmp(elAux){
	$(".tooltipEmp").hide();
	
	var pos = elAux.offset();
	var width = elAux.width();
	var arrayAux = elAux.attr("id").split("-");			
	/*$(".tooltipEmp").css("left",(parseInt(pos.left) + 80).toString() + "px");
	$(".tooltipEmp").css("top",(parseInt(pos.top) - 24).toString() + "px");*/
	$(".tooltipEmp").css("left",(parseInt(pos.left) - 34).toString() + "px");
	$(".tooltipEmp").css("top",(parseInt(pos.top) - 4).toString() + "px");
	$(".tooltipEmp .percent").html(elAux.attr("rel"));
	$(".tooltipEmp").show();
}

function posicionaSubMenu(){		
	var offsetSubMenuAux = 0;
	if($(".menuEmp").length > 0){
		$("#itMenu-empreendimentos").each(function(){
			var pos = $(this).offset();
			var width = $(this).width();
			var offsetFF = 20;
			var arrayAux = $(this).attr("id").split("-");			
			$(".menuEmp").css("left",(parseInt(pos.left) - offsetFF - parseInt(offsetSubMenuAux)).toString() + "px");
			$(".menuEmp").css("top",(parseInt(pos.top)).toString() + "px").css("padding-top","80px");
		});
	}
}

function showLoading(){
	if($(".divLoading").length>0){
		//$(".divLoading").fadeIn(500);
		$(".divLoading").show();
	}
}

function hideLoading(){
	if($(".divLoading").length>0){
		//$(".divLoading").fadeOut(500);
		$(".divLoading").hide();
	}
}

(function($) {
	  var cache = [];
	  // Arguments are image paths relative to the current page.
	  $.preLoadImages = function() {
	    var args_len = arguments.length;
	    for (var i = args_len; i--;) {
	      var cacheImage = document.createElement('img');
	      cacheImage.src = arguments[i];
	      cache.push(cacheImage);
	    }
	  }
})(jQuery)

function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds){
      break;
    }
  }
}

function getEndereco() {
	if($.trim($("#txtCEP_cad").val()) != ""){
		$.getScript("http://cep.republicavirtual.com.br/web_cep.php?formato=javascript&cep="+$("#txtCEP_cad").val(), function(){
		if (resultadoCEP["tipo_logradouro"] != '') {
			if (resultadoCEP["resultado"]) {
				//$("#txtEndereco").val(unescape(resultadoCEP["tipo_logradouro"]) + " " + unescape(resultadoCEP["logradouro"]));
				//$("#txtBairro").val(unescape(resultadoCEP["bairro"]));
				$("#txtCidade_cad").val(unescape(resultadoCEP["cidade"]));
				$("#txtEstado_cad").val(unescape(resultadoCEP["uf"]));
				//$("#txtPais").val('Brasil');
				//$("#txtNumero").focus();
				$("#txtTelefone_cad").focus();
			}
			else
			{
				$("#txtCidade_cad").val("");
				$("#txtEstado_cad").val("");
			}
		}	
		else
		{
			$("#txtCidade_cad").val("");
			$("#txtEstado_cad").val("");
		}
			
		});
	}
}

function ValidaCPF(campo)
{
	if(VerificaCPF(campo.value))
	{}
	else
	{
		//alert('CPF inv√°lido');
		$("#"+campo.id).focus();
	}
}

function ValidaCNPJ(campo)
{
	if(VerificaCNPJ(campo.value))
	{}
	else
	{
		//alert('CNPJ inv√°lido');
		$("#"+campo.id).focus();
	}
}

function VerificaCPF(cpf)
{
	if(cpf == "" || cpf == "___.___.___-__")
	{return true;}
	cpf = cpf.replace(/\_/g,"").replace(/\-/g,"").replace(/\./g,"");	
	if(cpf == "00000000000" ||
	   cpf == "11111111111" ||
	   cpf == "22222222222" ||
	   cpf == "33333333333" ||
	   cpf == "44444444444" ||
	   cpf == "55555555555" ||
	   cpf == "66666666666" ||
	   cpf == "77777777777" ||
	   cpf == "88888888888" ||
	   cpf == "99999999999")
	{return false;}
	
	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 VerificaCNPJ(cnpj)
{
	if(cnpj == "" || cnpj == "__.___.___/____-__")
	{return true;}	
	cnpj = cnpj.replace(/\_/g,"").replace(/\-/g,"").replace(/\./g,"").replace(/\//g,"");
	if(cpf == "00000000000000" ||
	   cpf == "11111111111111" ||
	   cpf == "22222222222222" ||
	   cpf == "33333333333333" ||
	   cpf == "44444444444444" ||
	   cpf == "55555555555555" ||
	   cpf == "66666666666666" ||
	   cpf == "77777777777777" ||
	   cpf == "88888888888888" ||
	   cpf == "99999999999999")
	{return false;}
	
	var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais;
    digitos_iguais = 1;
    if (cnpj.length < 14 && cnpj.length < 15)
          return false;
    for (i = 0; i < cnpj.length - 1; i++)
          if (cnpj.charAt(i) != cnpj.charAt(i + 1))
                {
                digitos_iguais = 0;
                break;
                }
    if (!digitos_iguais)
          {
          tamanho = cnpj.length - 2
          numeros = cnpj.substring(0,tamanho);
          digitos = cnpj.substring(tamanho);
          soma = 0;
          pos = tamanho - 7;
          for (i = tamanho; i >= 1; i--)
                {
                soma += numeros.charAt(tamanho - i) * pos--;
                if (pos < 2)
                      pos = 9;
                }
          resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
          if (resultado != digitos.charAt(0))
                return false;
          tamanho = tamanho + 1;
          numeros = cnpj.substring(0,tamanho);
          soma = 0;
          pos = tamanho - 7;
          for (i = tamanho; i >= 1; i--)
                {
                soma += numeros.charAt(tamanho - i) * pos--;
                if (pos < 2)
                      pos = 9;
                }
          resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
          if (resultado != digitos.charAt(1))
                return false;
          return true;
          }
    else
          return false;
}

function validarData(data)
{
	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))
	{
		return false;
	}
	else{
		return true;
	}
}

function validarMail(mail){
    var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
    if(typeof(mail) == "string"){
            if(er.test(mail)){ return true; }
    }else if(typeof(mail) == "object"){
            if(er.test(mail.value)){ 
                                    return true; 
                            }
    }else{
            return false;
            }
}

function removeHTMLTags(htmlString){
    if(htmlString){
      var mydiv = document.createElement("div");
       mydiv.innerHTML = htmlString;

        if (document.all) // IE Stuff
        {
            return mydiv.innerText;
           
        }   
        else // Mozilla does not work with innerText
        {
            return mydiv.textContent;
        }                           
  }
}
