var myTheme = {
    init: function () {
        if (this.inIframe()) $('body').addClass('in-iframe');
        if (!$('body').hasClass('exe-web-site')) return;
        var togglers =
            '\
            <button type="button" id="siteNavToggler" class="toggler" title="' +
            $exe_i18n.menu +
            '">\
                <span class="sr-av">' +
            $exe_i18n.menu +
            '</span>☰ Menú\
            </button>\
            <button type="button" id="searchBarTogger" class="toggler" title="' +
            $exe_i18n.search +
            '">\
                <span class="sr-av">' +
            $exe_i18n.search +
            '</span>\
            </button>\
        ';
		const divSearch=document.querySelector('#exe-client-search');
		const divMain = document.querySelector('.main-header');
        const divNav = document.querySelector('.nav-buttons');
		const divNavRight=document.querySelector('.nav-button.nav-button-right');
		const divFooter = document.querySelector('#siteFooter');
		const divPageContent = document.querySelector('.page-content');
		const divSiteNav=document.querySelector('#sidebar-nav');
		const pPageCounter= document.querySelector('.page-counter');
		if (pPageCounter){
			document.querySelectorAll('.page-counter-sep').forEach(el => {
				el.textContent = ' de ';
			});
			divNavRight.parentNode.insertBefore(pPageCounter, divNavRight);
		}

        divMain.parentNode.insertAdjacentHTML('afterbegin', togglers);
        const divPxt=document.querySelector('.page-header');
       if (!document.querySelector('.nav-buttons-inf')){
        const divNavInf = divNav.cloneNode(true);
        //const divCountInf=pPageCounter.cloneNode(true);
		divPageContent.after(divFooter);
		divNavInf.className = 'nav-buttons-inf';
		divFooter.after(divNavInf);
	   }

        var url = window.location.href;
        url = url.split('?');
        if (url.length > 1) {
            if (url[1].indexOf('nav=false') != -1) {
                $('body').addClass('siteNav-off');
                myTheme.params('add');
            }
        }
        // Menu toggler
        $('#siteNavToggler').on('click', function () {
            if (myTheme.isLowRes()) {
                $('#exe-client-search').hide();
                if ($('body').hasClass('siteNav-off')) {
                    $('body').removeClass('siteNav-off');
                } else {
                    if ($('#siteNav').isInViewport()) {
                        $('body').addClass('siteNav-off');
                        myTheme.params('add');
                    }
                }
                window.scroll(0, 0);
            } else {
                $('body').toggleClass('siteNav-off');
                myTheme.params(
                    $('body').hasClass('siteNav-off') ? 'add' : 'remove'
                );
            }
        });
        // Search bar toggler
        $('#searchBarTogger').on('click', function () {
            var bar = $('#exe-client-search');
            if (bar.is(':visible')) {
                bar.hide();
            } else {
                if (myTheme.isLowRes()) {
                    $('body').addClass('siteNav-off');
                }
                bar.show();
                $('#exe-client-search-text').focus();
                window.scroll(0, 0);
            }
        });
        if (!this.inIframe()) {
            // Fixed navigation
            $('#siteNav').wrap('<div id="sidebar-nav"></div>');
            myTheme.checkNav();
            $(window).bind('resize', function () {
                myTheme.checkNav();
            });
        }

				
        // Search form
        this.searchForm();
        
        this.fases();
    },
    inIframe: function () {
        try {
            return window.self !== window.top;
        } catch (e) {
            return true;
        }
    },
    searchForm: function () {
        $('#exe-client-search-text').attr('class', 'form-control');
    },
    isLowRes: function () {
        return $('#siteNav').css('float') == 'none';
    },
    checkNav: function () {
        var wrapper = $('#sidebar-nav');
        var navH = $('#siteNav > ul').height(); // Menu height
        navH = navH + 50;
        if (navH < $(window).height()) wrapper.addClass('fixed');
        else wrapper.removeClass('fixed');
    },
    param: function (e, act) {
        if (act == 'add') {
            var ref = e.href;
            var con = '?';
            if (ref.indexOf('.html?') != -1) con = '&';
            var param = 'nav=false';
            if (ref.indexOf(param) == -1) {
                ref += con + param;
                e.href = ref;
            }
        } else {
            // This will remove all params
            var ref = e.href;
            ref = ref.split('?');
            e.href = ref[0];
        }
    },
    params: function (act) {
        $('.nav-buttons a').each(function () {
            myTheme.param(this, act);
        });
    },
     
    fases: function(){
			const hbase =document.querySelector('.page-header');
			const title = document.querySelector('.page-title');
			const cadena = title.innerText;
			const firstChar = cadena[0];
			const patron = /^\d\.\d/;			
			if (!isNaN(firstChar) && firstChar !== " ") {
			if (cadena.match(patron)) {
			  hbase.classList.add("nodeSubSection-" + firstChar);
			} else {
			  hbase.classList.add("nodeSection-" + firstChar);
			}
		}

	

	}
};

$.fn.isInViewport = function () {
    var elementTop = $(this).offset().top;
    var elementBottom = elementTop + $(this).outerHeight();
    var viewportTop = $(window).scrollTop();
    var viewportBottom = viewportTop + $(window).height();
    return elementBottom > viewportTop && elementTop < viewportBottom;
};
$(function(){
	if ($("body").hasClass("exe-web-site")) {
		myTheme.init();
	}

});

document.addEventListener("DOMContentLoaded", (event) => {
document.querySelectorAll('.udl-character-1 img').forEach(function(imx) {
	imx.setAttribute('title', 'Arela ');
	imx.setAttribute('alt', 'Arela');
});
document.querySelectorAll('.udl-character-2 img').forEach(function(imx) {
	imx.setAttribute('title', 'Bado ');
	imx.setAttribute('alt', 'Bado');
});
document.querySelectorAll('.udl-character-3 img').forEach(function(imx) {
	imx.setAttribute('title', 'Cheda ');
	imx.setAttribute('alt', 'Cheda');
});
document.querySelectorAll('.udl-character-4 img').forEach(function(imx) {
	imx.setAttribute('title', 'Luzada ');
	imx.setAttribute('alt', 'Luzada');
});

if (!document.body.classList.contains('exe-export')){
	
const personaxes = ["",	" Arela  "," Bado  ",	" Cheda  "," Luzada  "];
 var textlabel = null;
 for (var i=1;i<5;i++){
	 const labels = document.querySelectorAll('.udlContentFormBlockButtonTxtOptions  label[for$="-'+i+'"]');  
     labels.forEach((textlabel) => {
     textlabel.lastChild.textContent = personaxes[i];
  });
}

}
});
$.fn.isInViewport = function () {
    var elementTop = $(this).offset().top;
    var elementBottom = elementTop + $(this).outerHeight();
    var viewportTop = $(window).scrollTop();
    var viewportBottom = viewportTop + $(window).height();
    return elementBottom > viewportTop && elementTop < viewportBottom;
};


