
function googleTranslateElementInit() {
  new google.translate.TranslateElement({
    pageLanguage: 'pt-PT',
    layout: google.translate.TranslateElement.InlineLayout.SIMPLE,
    autoDisplay: false,
    includedLanguages: 'pt,de,zh-TW,es,fr,en'
  }, 'google_translate_element');
}
function abre_login_mob() {
    document.getElementById("recuperar_mob").style.display = "none";
    $("#login_mob").slideToggle( "slow" );
}

function abre_pesquisa_mob() {
    document.getElementById("recuperar_mob").style.display = "none";
    document.getElementById("login_mob").style.display = "none";
    $("#pesquisa_mob").slideToggle( "slow" );
}

function recuperar_mob() {
    $("#login_mob").slideToggle( "slow" );
    $("#recuperar_mob").slideToggle( "slow" );
}

function recuperar2_mob() {
    jQuery('html, body').animate({scrollTop: 0}, 500);
    $("#recuperar_mob").slideToggle( "slow" );
}

var estado_janela_carrinho = 0;
function abre_carrinho_lateral(v) {
    if (estado_janela_carrinho == 1) {
        if (!v) {
             $("#escurece3").show();
             $('#janela_carrinho_lateral').css({ 'left': '-800px' },250).animate({ 'left' : '0px' },250);
        }
        document.getElementById("janela_carrinho_content").src = "../blocos/carrinho_compras_lateral.php?proc="+v+"&res="+$(window).width();
    } else mensagem("Anomalia","Carrinho Vazio!");
}

function fechar_carrinho_lateral(v) {
   $('#janela_carrinho_lateral').css({ 'left': '0px' },250).animate({ 'left' : '-800px' },250);
   $("#escurece3").hide();
   if (v) document.getElementById("btextcarri").style.display = "none";
}

var janela_compara = 0;var in_comparador = 0;

function abre_comparador() {
    if (janela_compara == 1) {
        pat = document.getElementById("janela_comparador");
        if ($(window).width() > 600) {
            pat.style.top = (($(window).height() - 740)/2 + $(document).scrollTop())+'px';
    	    pat.style.left = (($(window).width() - 1000)/2)+'px';
            document.getElementById("janela_comparador_content").scrolling = 'no';
        } else {
            pat.style.top = (($(window).height() - 640)/2 + $(document).scrollTop())+'px';
    	    pat.style.left = '0px';
            document.getElementById("janela_comparador_content").scrolling = 'yes';
        }
        $("#janela_comparador").slideDown( "slow" );
        $("#escurece").show( "slow", function() {
            document.getElementById("janela_comparador_content").src = "../blocos/comparador.php?efipro=1";
        });
    } else mensagem("COMPARADOR VAZIO","Não existem produtos no comparador. Adicione primeiro os produtos que deseja comparar clicando no botão verde com o mesmo nome e de seguida entre no Comparador.");
}

function fechar_comparador() {
   var container = $("#janela_comparador");
   $("#escurece").hide( "slow" );
   container.slideUp(350);
}

function fechar_comparador2(qtd) {
   var container = $("#janela_comparador");
   $("#escurece").hide( "slow" );
   altera_botao_exterior_comparador(qtd);
   container.slideUp(350);
}

$(document).mouseup(function(e) {
   var container = $("#janela_comparador");
   if (!container.is(e.target) && container.has(e.target).length === 0) {
        $("#escurece").hide( "slow" );
        container.slideUp(350);
   }
});

function altera_botao_exterior_comparador(qtd) {
   if (qtd!='0') {
        janela_compara = 1;
        $("#btextcompa").show( "slow" );
        document.getElementById("btextcomp_num").innerHTML = qtd;
   } else $("#btextcompa").hide( "slow" );
}

/* ############ Grupo de Funções que são executadas no fim da página carregar totalmente ############## */
$(document).ready(function() {

/* ################################# Menu Horizontal na versão mobile ##################################### */
    var ultimoElementoAberto = null;
    $('li').click(function(event) {
        var larguraViewport = $(window).width();
        var hasSubmenu = $(this).find('.submenu').length > 0;

        if (larguraViewport <= 800) {
            if (($(event.target).closest('li')[0] === this) && (hasSubmenu)) {
                event.preventDefault();
                $(this).find('a').first().attr('href', '#');
            }
            if ($(this).find('a').attr('href') == "#") {
                if ($(this).has('ul').length != '0') {
                    event.stopPropagation();
                    $('ul').not($(this).parents()).not($("[class^='bxslider']")).slideUp();
                    if (ultimoElementoAberto) {
                        if (ultimoElementoAberto.text() != $("ul",this).first().text()) {
                            $("ul",this).first().slideDown();
                            $(".dropdown_2columns ul",this).slideDown();
                            $('.submenu',this).animate({marginTop:'10px','marginBottom':'-10px'},400);
                            ultimoElementoAberto = $("ul",this).first();
                        } else ultimoElementoAberto = null;
                    } else {
                        $('.submenu',this).animate({'marginTop':'10px','marginBottom':'-10px'},100);
                        $("ul",this).first().slideDown(250);
                        $(".dropdown_2columns ul",this).slideDown();
                        ultimoElementoAberto = $("ul",this).first();
                    }
                }
            }
        }
    });

    $("#dmenu_conteudos").click(function() {
        $("#content-wrap").slideToggle({
            duration: 0,   // Duração da animação em milissegundos (600ms)
            easing: "swing"
        });
    });

    $(window).resize(function() {
        var larguraViewport = $(window).width();
        if (larguraViewport > 800) {
            $("#content-wrap").show();
            $("#content-wrap ul").show();
            $("ul#css3menu .submenu").css("marginTop", "-1px");
        } else {
/*
            $(".column ul").slideUp();
            $("#content-wrap").hide();
            $("ul#css3menu .submenu").css("marginBottom", "0px");
            ultimoElementoAberto = null;
*/
        }
    });

// ################################# Menu Horizontal Fixo #####################################
    var posicaoy_menu_topo = $("#menu_topo_fixo_out").offset();
});

var sactivo;
var dactivo;

function fp_muda_separador(n,d) {
    $("#area8").hide();
    document.getElementById("sepa"+sactivo).className = "sepa_off";
    document.getElementById("sepa"+n).className = "sepa_on";
    $("#"+dactivo).hide(500);
    $("#"+d).show(500);
    sactivo = n;
    dactivo = d;
    if (d == "div_identicos") abre_identicos('0');
}

function abre_mapa_espec() {
    $("#div_tabela").hide(500);
    if ($(".col_big").width() < 600) $("#area8").height(350); else $("#area8").height(600);
    $("#area8").show(500);
}

var sliderHeight = "120px";
var slide_desc = 0;

function iniciaSlider(c1) {
    var current = $("."+c1);
    current.attr("box_h", current.height()+40);
    $("."+c1).css("height", sliderHeight);
}

function openSlider(c1) {
    if (!slide_desc) {
        var open_height = $("."+c1).attr("box_h") + "px";
        $("."+c1).animate({"height": open_height}, {duration: "slow" });
        $("#"+c1+"_mi").html('<img border="0" src="../imagens/menos_desc.png">');
        slide_desc = 1;
    } else {
        $("."+c1).animate({"height": sliderHeight}, {duration: "slow" });
        $("#"+c1+"_mi").html('<img border="0" src="../imagens/mais_desc.png">');
        slide_desc = 0;
    }
}


var estado_janela_favoritos = 0;function abre_favoritos(v) {
    var div_btextfavor_num = document.getElementById("btextfavor_num").textContent.trim();
    if (div_btextfavor_num === '' || div_btextfavor_num === '0') mensagem("Anomalia","Favoritos Vazio!"); else {
        if (!v) {
             $("#escurece4").show();
             $('#janela_favoritos').css({ 'left': '-800px' },250).animate({ 'left' : '0px' },250);
        }
        document.getElementById("janela_favoritos_content").src = "../blocos/favoritos.php?proc="+v;
    }
}

function remove_botao_like() {
    document.getElementById("btextlike").style.display = "none";
}

function actualiza_botao_like(n) {
    document.getElementById("btextfavor_num").innerHTML = n;
}

function fechar_favoritos(v) {
   $('#janela_favoritos').css({ 'left': '0px' },250).animate({ 'left' : '-800px' },250);
   $("#escurece4").hide();
   if (v) remove_botao_like();
}
function limpa_likes_produtos(n) { mudar_img_favorito(n); }

var corrige_barra_topo = 0;

$(document).ready(function() {
    function getElementAtPosition(x, y) { return document.elementFromPoint(x, y); }

    function containsGoogleExtensionContent(divId,tipo) {
        var content = $(tipo + divId).html();
        return content.includes('chrome-extension://');
    }

    function adjustForExternalBar() {
        var element = getElementAtPosition(5, 5);
        var classe_final = element.className;
        var id_final = element.id;

        if (classe_final) var classe_final_tem = containsGoogleExtensionContent(classe_final,".");
        if (id_final) var id_final_tem = containsGoogleExtensionContent(id_final,"#");

        if (classe_final_tem || id_final_tem) {
            document.getElementById('all').style.paddingTop = element.offsetHeight+"px";
            document.body.style.backgroundPosition = "left top " + element.offsetHeight + "px";
            corrige_barra_topo = element.offsetHeight;
        }
    }
    window.addEventListener('load', () => { setTimeout(adjustForExternalBar, 500); });
});
