//Objeto padrao greencred
var app = {
    init: function() {
        app.loadSticky();
        app.anchor();
        app.slideToDiv();
        window.onscroll = function() {app.loadSticky();}; 
        app.loadToUp();
        app.adjustsForm();

        window.resize = function() {app.menuMarcaAtivo();};
    },
    adjustsForm: function(){
        if(jQuery('form')) {
            jQuery('form').attr("autocomplete","off");
        }
        if (jQuery(window).width() <= 991) {
            jQuery('#menu-principal nav .elementor-nav-menu li.menu-item-has-children>a').on('click',function(e){
                e.preventDefault();
                jQuery(this).closest('li.menu-item-has-children').toggleClass('open');
            });
        }
        jQuery('.elementor-tabs .elementor-tab-content .leia-mais').on('click',function(e){
            e.preventDefault();
            jQuery(this).closest('.elementor-tab-content').addClass('open');
        })
    },
    loadSticky: function () {
        var height = jQuery(window).scrollTop();
        if(height > 200 ){
            jQuery('.to-top').addClass('show');
        }else{
            jQuery('.to-top').removeClass('show');
        }
    },
    anchor : function(link){
        var desconto = 0,
            width = jQuery(window).width();
        if(link!=undefined) {
            var link = link.replace("#", "");
        }
        var linkT = link;
        desconto += jQuery('section.header').height();
        if(jQuery('body').hasClass('admin-bar')){
            desconto += jQuery('#wpadminbar').height();
        }
        if(link) {
            var scroll = (jQuery("." + link).offset().top)+1;
            console.log(link+'-'+scroll);
            jQuery('html, body').animate({
                scrollTop: (scroll - desconto)
            }, 100);
            history.pushState(
                '', '',
                window.location.origin + window.location.pathname + "#" + linkT
            );
        };
        return false;
    },
    slideToDiv : function(){
        var hash = window.location.hash;
        var link = hash.replace("#", "");
        if((hash.length && jQuery('.'+link).length)){
            setTimeout(function(){
                app.anchor(hash);
            }, 1000);
        };
        jQuery('.click-anchor').click( function(e) {
            e.preventDefault();
            var link = jQuery(this).attr('href'),
                pathlink = jQuery(this)[0].pathname,
                hash = jQuery(this)[0].hash,
                endereco = window.location.pathname;
            if(pathlink==endereco){
                if(hash) {
                    app.anchor(hash);
                }else{
                    window.location.href = link;
                }
            }else{
                window.location.href = link;
            }
        });
        jQuery('#menu-menu-footer a,.navbar-nav a').click( function(e) {
            e.preventDefault();
            var link = jQuery(this).attr('href'),
                pathlink = jQuery(this)[0].pathname,
                hash = jQuery(this)[0].hash,
                endereco = window.location.pathname,
                s = getUrlParameter('s');
            if(pathlink==endereco && s===false){
                if(hash) {
                    app.anchor(hash);
                    jQuery('.navbar-toggler').click();
                }else{
                    window.location.href = link;
                }
            }else{
                window.location.href = link;
            }
        });
        //jQuery('.link-contato').removeClass('active');
        var desconto = jQuery('section.header').height(),
            width = jQuery(window).width();
        if(jQuery('body').hasClass('admin-bar')){
            desconto += jQuery('#wpadminbar').height();
        }
    },
    loadToUp : function(){
        jQuery('.to-top').click( function(e) {
            e.preventDefault();
            document.body.scrollIntoView({behavior: 'smooth', block: 'start'});
            return false;
        });
    },
    setCookie:function(name,value,days) {
        console.log('setcookie');
        var expires = "";
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days*24*60*60*1000));
            expires = "; expires=" + date.toUTCString();
        }
        document.cookie = name + "=" + (value || "")  + expires + "; path=/";
    },
    getCookie :function(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for(var i=0;i < ca.length;i++) {
            var c = ca[i];
            while (c.charAt(0)==' ') c = c.substring(1,c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
        }
        return null;
    },
    eraseCookie: function (name) {
        document.cookie = name +'=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
    },
};
jQuery("document").ready(function ($) {
    app.init();
    $('.phone-mask,input[name="form_fields[phone]"]').mask('(00) 0000-00009');
    $('.phone-mask,input[name="form_fields[phone]"]').blur(function(event) {
        if($(this).val().length == 15){ // Celular com 9 dígitos + 2 dígitos DDD e 4 da máscara
            $(this).mask('(00) 00000-0009');
        } else {
            $(this).mask('(00) 0000-00009');
        }
    });
});