$(document).ready(function() {     $('#btd_carrinho,#lista_carrinho').hover(function() {
        var pos_carrinho = $("#btd_carrinho").offset();
        $("#btd_carrinho").css({'color': '#FFFFFF', 'background-color': '#7A3030','border-color': '#7A3030'});
        $("#lista_carrinho").css({'top': pos_carrinho.top + $(".table_row").height() - 1});
        $("#lista_carrinho").css({'left': pos_carrinho.left + ($("#btd_carrinho").width()+20+4) - 400});
        $('#lista_carrinho').slideDown(200);
    }, function() {
           setTimeout(function () {
                if ((!($('#lista_carrinho:hover').length > 0)) && (!($('#btd_carrinho:hover').length > 0))) $('#lista_carrinho').slideUp(0);
           }, 0);
           $("#btd_carrinho").css({'color': '#DBDBDB', 'background-color': '#4F0000','border-color': '#4F0000'});
    });

    $("#ccompras_topo").load("../blocos/carrinho_topo.php?tira_setas=0&tamseta=6&tamletra=12&sep=linha");
    $('#btd_login,#bltp_login').hover(function() {
        var pos_login = $("#btd_login").offset();
        $("#btd_login").css({'color': '#FFFFFF', 'background-color': '#7A3030','border-color': '#7A3030'});
        $("#bltp_login").css({'top': pos_login.top + $(".table_row").height() - 1 - corrige_barra_topo});
        $("#bltp_login").css({'left': pos_login.left + ($("#btd_login").width()+20+4) - 262});
        $('#bltp_login').slideDown(200);
    }, function() {
         if (!$("#xlogin").is(':focus') && !$("#xpassword").is(':focus')) {
           setTimeout(function () {
                if ((!($('#bltp_login:hover').length > 0)) && (!($('#btd_login:hover').length > 0))) $('#bltp_login').slideUp(0);
           }, 0);
           $("#btd_login").css({'color': '#DBDBDB', 'background-color': '#4F0000','border-color': '#4F0000'});
         }
    });
    $("#bltloga_id").load("../blocos/login_topo.php?width_bllogin=208&btlognome="+encodeURIComponent('ENTRAR')+"&local=int");
});

function carrega_login_topo(login,password) {
    $("#bltloga_id").load("../blocos/login_topo.php?width_bllogin=208&btlognome="+encodeURIComponent('ENTRAR')+"&xlogin="+encodeURIComponent(login)+"&xpassword="+encodeURIComponent(password)+"&local=int&dologin=1&ck="+encodeURIComponent('%2Fcomum%2Fjscripts.php%3Fccompras_final%3D0%26nova_ficha_produto%3D1%26rsess%3D6980384'));
}

        function googleTranslateElementInit() {
            new google.translate.TranslateElement({
                pageLanguage: 'pt-PT',
                includedLanguages: 'pt,de,zh-TW,es,fr,en',
                layout: google.translate.TranslateElement.InlineLayout.SIMPLE,
                autoDisplay: false
            }, 'google_translate_element');
        }

        $('document').ready(function () {
            $('#google_translate_element').on("click", function () {
                $('.VIpgJd-ZVi9od-xl07Ob-OEVmcd').css('height','170px');
                $('.VIpgJd-ZVi9od-xl07Ob-OEVmcd').css('width','180px');
                $("iframe").contents().find('.VIpgJd-ZVi9od-vH1Gmf').css('border', 'none');
                $("iframe").contents().find('.VIpgJd-ZVi9od-vH1Gmf').css('height', '160px');
                $("iframe").contents().find('.VIpgJd-ZVi9od-vH1Gmf').css('width', '200px');
                $("iframe").contents().find('.indicator').css('display', 'none');
                $("iframe").contents().find(".VIpgJd-ZVi9od-vH1Gmf-ibnC6b div, .VIpgJd-ZVi9od-vH1Gmf-ibnC6b:link div, .VIpgJd-ZVi9od-vH1Gmf-ibnC6b:visited div, .VIpgJd-ZVi9od-vH1Gmf-ibnC6b:active div") //, .goog-te-menu2 *
                    .css({
                        'color': '#544F4B',
                        'font-family': 'Helvetica,Arial,sans-serif',
                        'padding': '6px 10px'
                    });
                $("iframe").contents().find(".VIpgJd-ZVi9od-vH1Gmf-ibnC6b div").hover(function () {
                    $(this).css('width', '150px');
                    $(this).css('background-color', '#EB9100').find('span.text').css('color', '#ffffff');
                    $(this).css('border-radius', '6px').find('span.text').css('-moz-border-radius', '6px');
                }, function () {
                    $(this).css('background-color', '#ffffff').find('span.text').css('color', '#544F4B');
                });
            });
        });
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").offset();
    menuh_topo_sempre_presente = 1;
    function menu_scroll_format() {
        if ($(window).width() >= 801) {
            var scroll = $(window).scrollTop();
            if (scroll > posicaoy_menu_topo.top) {
                menu_topo_fixo.setAttribute("style", "position:relative; z-index:100; display: flex; box-shadow: none; justify-content: center; height:49px; max-width:1378px;");
                menu_topo_fixo_o.setAttribute("style", "position:fixed; z-index:100; width:100%; top:0px; left:0px; box-shadow: none; height:49px; background-color:#6C252C;border-bottom: 1px solid #6C252C;");
                // antes display: flex; align-items: center; justify-content: center; estava no menu_topo_fixo_o também retirei do menu_topo_fixo depois para centrar verticalmente as opções
            } else {
                if ($(window).width() < 801) {
                    menu_topo_fixo.setAttribute("style", "position:relative; z-index:100; border-radius:0px; border:0px; background-image:none; background-color:none;");
                    menu_topo_fixo_o.setAttribute("style", "position:relative; margin:0px;");
                } else {
                    menu_topo_fixo.setAttribute("style", "position:relative; z-index:100;  height:49px; z-index:10000; width:100%; box-sizing: border-box; background-color:#6C252C; ");
                    menu_topo_fixo_o.setAttribute("style", "position:relative; z-index:100;"); // antes tinha margin:0px; position:relative; e margin:8px 0px 0px;
                }
            }
        }
    }
    $(window).scroll(function (event) { menu_scroll_format(); });
});

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); }
    function refresh_cc() {
         $("#ccompras_topo").load("../blocos/carrinho_topo.php?tira_setas=0&tamseta=6&tamletra=12&sep=linha");
    }

    function quantidade_carrinho_topo(tipo,id) {
         $("#ccompras_topo").load("../blocos/carrinho_topo.php?tira_setas=0&tamseta=6&tamletra=12&sep=linha&"+tipo+"=1&id="+id);
    }
    function apagar_carrinho_topo(id) {
         $("#ccompras_topo").load("../blocos/carrinho_topo.php?tira_setas=0&tamseta=6&tamletra=12&sep=linha&apagar=1&id="+id);
    }
    function cctopo_input(valor,id) {
         $("#ccompras_topo").load("../blocos/carrinho_topo.php?tira_setas=0&tamseta=6&tamletra=12&sep=linha&qtdv="+valor+"&id="+id);
    }

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); });
});
