jQuery(function($){
	"use strict";
	jQuery('.main-menu-navigation > ul').superfish({
		delay:       500,
		animation:   {opacity:'show',height:'show'},  
		speed:       'fast'
	});
});

function charity_welfare_funds_menu_open() {
	jQuery(".menu-brand.primary-nav").addClass('show');
}
function charity_welfare_funds_menu_close() {
	jQuery(".menu-brand.primary-nav").removeClass('show');
}

var charity_welfare_funds_Keyboard_loop = function (elem) {
    var charity_welfare_funds_tabbable = elem.find('select, input, textarea, button, a').filter(':visible');

    var charity_welfare_funds_firstTabbable = charity_welfare_funds_tabbable.first();
    var charity_welfare_funds_lastTabbable = charity_welfare_funds_tabbable.last();

    /*redirect last tab to first input*/
    charity_welfare_funds_lastTabbable.on('keydown', function (e) {
        if ((e.which === 9 && !e.shiftKey)) {
            e.preventDefault();
            charity_welfare_funds_firstTabbable.focus();
        }
    });

    /*redirect first shift+tab to last input*/
    charity_welfare_funds_firstTabbable.on('keydown', function (e) {
        if ((e.which === 9 && e.shiftKey)) {
            e.preventDefault();
            charity_welfare_funds_lastTabbable.focus();
        }
    });

    /* allow escape key to close insiders div */
    elem.on('keyup', function (e) {
        if (e.keyCode === 27) {
            elem.hide();
        }
        ;
    });
};

// scroll
jQuery(document).ready(function () {
	jQuery(window).scroll(function () {
	    if (jQuery(this).scrollTop() > 0) {
	        jQuery('#scrollbutton').fadeIn();
	    } else {
	        jQuery('#scrollbutton').fadeOut();
	    }
	});
	jQuery(window).on("scroll", function () {
	   document.getElementById("scrollbutton").style.display = "block";
	});
	jQuery('#scrollbutton').click(function () {
	    jQuery("html, body").animate({
	        scrollTop: 0
	    }, 600);
	    return false;
	});
});

jQuery(function($){
	$('.mobiletoggle').click(function () {
        charity_welfare_funds_Keyboard_loop($('.menu-brand.primary-nav'));
    });
});

// preloader
jQuery(function($){
    setTimeout(function(){
        $(".frame").delay(1000).fadeOut("slow");
    });
});

jQuery('document').ready(function($){
    $('.main-search span a').click(function(){
        $(".searchform_page").slideDown(500);
        charity_welfare_funds_Keyboard_loop($('.searchform_page'));
    });

    $('.close a').click(function(){
        $(".searchform_page").slideUp(500);
    });
}); 

jQuery(document).ready(function () {
    
    function charity_welfare_funds_search_loop_focus(element) {
        var charity_welfare_funds_focus = element.find('select, input, textarea, button, a[href]');
        var charity_welfare_funds_firstFocus = charity_welfare_funds_focus[0];  
        var charity_welfare_funds_lastFocus = charity_welfare_funds_focus[charity_welfare_funds_focus.length - 1];
        var KEYCODE_TAB = 9;

        element.on('keydown', function charity_welfare_funds_search_loop_focus(e) {
            var isTabPressed = (e.key === 'Tab' || e.keyCode === KEYCODE_TAB);

            if (!isTabPressed) { 
              return; 
            }

            if ( e.shiftKey ) /* shift + tab */ {
              if (document.activeElement === charity_welfare_funds_firstFocus) {
            charity_welfare_funds_lastFocus.focus();
              e.preventDefault();
            }
          } else /* tab */ {
            if (document.activeElement === charity_welfare_funds_lastFocus) {
                charity_welfare_funds_firstFocus.focus();
              e.preventDefault();
            }
          }
        });
    }
});

// Title Color
jQuery( document ).ready(function() {
  jQuery("#slider h1.slider-title").each(function() {
    var t = jQuery(this).text();
    var splitT = t.split(" ");
    console.log(splitT);
    var halfIndex = 1;
    var newText = "";
    for(var i = 0; i < splitT.length; i++) {
      if(i == halfIndex) {
        newText += "<span style='color:var(--first-theme-color)'>";
        newText += splitT[i] + " ";
        newText += "</span>";
      }else{
        newText += splitT[i] + " ";
      }      
    }    
    jQuery(this).html(newText);
  });
});

// Donation
document.addEventListener('DOMContentLoaded', function() {
    var progressBars = document.querySelectorAll('.campaign-progress-bar');
    
    progressBars.forEach(function(progressBar) {
        var bar = progressBar.querySelector('.bar');
        
        var percentage = progressBar.getAttribute('aria-valuenow');
        
        var percentageText = document.createElement('span');
        percentageText.className = 'percentage-text';
        percentageText.textContent = Math.min(100, Math.round(percentage)) + '%';
        
        progressBar.appendChild(percentageText);
    });
});

/*sticky copyright*/
window.addEventListener('scroll', function() {
  var charity_welfare_funds_sticky = document.querySelector('.copyright-sticky');
  if (!charity_welfare_funds_sticky) return;

  var charity_welfare_funds_scrollTop = window.scrollY || document.documentElement.scrollTop;
  var charity_welfare_funds_windowHeight = window.innerHeight;
  var charity_welfare_funds_documentHeight = document.documentElement.scrollHeight;

  var charity_welfare_funds_isBottom = charity_welfare_funds_scrollTop + charity_welfare_funds_windowHeight >= charity_welfare_funds_documentHeight - 100;

  if (charity_welfare_funds_scrollTop >= 100 && !charity_welfare_funds_isBottom) {
    charity_welfare_funds_sticky.classList.add('copyright-fixed');
  } else {
    charity_welfare_funds_sticky.classList.remove('copyright-fixed');
  }
});
/* Progress Bar */
document.addEventListener("DOMContentLoaded", function () {
    const charity_welfare_funds_progressBar =
        document.getElementById("charity_welfare_funds_elemento_progress_bar");
    if (!charity_welfare_funds_progressBar) return;
    window.addEventListener("scroll", function () {
        const charity_welfare_funds_scrollTop =
            document.documentElement.scrollTop || document.body.scrollTop;
        const charity_welfare_funds_height =
            document.documentElement.scrollHeight -
            document.documentElement.clientHeight;
        const charity_welfare_funds_scrolled =
            (charity_welfare_funds_scrollTop / charity_welfare_funds_height) * 100;
        charity_welfare_funds_progressBar.style.width =
            charity_welfare_funds_scrolled + "%";
    });
});
