jQuery(function($) {

    /* -----------------------------------------
    Preloader
    ----------------------------------------- */
    $('#preloader').delay(1000).fadeOut();
    $('#loader').delay(1000).fadeOut("slow");

    /* -----------------------------------------
    Navigation
    ----------------------------------------- */
    $('.menu-toggle').click(function() {
        $(this).toggleClass('open');
    });

    /* -----------------------------------------
    Keyboard Navigation
    ----------------------------------------- */
    $(window).on('load resize', aster_portfolio_navigation)

    function aster_portfolio_navigation(event) {
        if ($(window).width() < 1200) {
            $('.main-navigation').find("li").last().bind('keydown', function(e) {
                if (e.shiftKey && e.which === 9) {
                    if ($(this).hasClass('focus')) {
                    }

                } else if (e.which === 9) {
                    e.preventDefault();
                    $('#masthead').find('.menu-toggle').focus();
                }                
            })
        } else {
            $('.main-navigation').find("li").unbind('keydown')
        }
    }

    aster_portfolio_navigation()

    var aster_portfolio_primary_menu_toggle = $('#masthead .menu-toggle');
    aster_portfolio_primary_menu_toggle.on('keydown', function(e) {
        var tabKey = e.keyCode === 9;
        var shiftKey = e.shiftKey;

        if (aster_portfolio_primary_menu_toggle.hasClass('open')) {
            if (shiftKey && tabKey) {
                e.preventDefault();
                const $the_last_li = $('.main-navigation').find("li").last()
                $the_last_li.find('a').focus()
                if (!$the_last_li.hasClass('focus')) {

                    const $is_parent_on_top = true
                    let $the_parent_ul = $the_last_li.closest('ul.sub-menu')

                    let count = 0

                    while (!!$the_parent_ul.length) {
                        ++count

                        const $the_parent_li = $the_parent_ul.closest('li')

                        if (!!$the_parent_li.length) {
                            $the_parent_li.addClass('focus')
                            $the_parent_ul = $the_parent_li.closest('ul.sub-menu')

                            // Blur the cross
                            $(this).blur()
                            $the_last_li.addClass('focus')
                        }

                        if (!$the_parent_ul.length) {
                            break;
                        }
                    }

                }

            };
        }
    })

    // Determine if the document is RTL
    var isRtl = $('html').attr('dir') === 'rtl';
    
  /* -----------------------------------------
  Slider Section
  ----------------------------------------- */

  jQuery(document).ready(function($){
    var $aster_portfolio_carousel = $(".slider-section .owl-carousel");
    $aster_portfolio_carousel.owlCarousel({
      items: 1,
      loop: true,
      autoplay: true,
      autoplayTimeout: 5000,
      nav: true,
      dots: false,
      navText : ['<i class="fas fa-angle-up"></i>','<i class="fas fa-angle-down"></i> '],
    });
  });
  
  //Services Section
    jQuery(document).ready(function ($) {
        var aster_portfolio_owl = $('#topservices .owl-carousel');
        aster_portfolio_owl.owlCarousel({
        margin: 70,
        nav: false,
        dots: false,
        loop: false,
        autoplay: false,
        lazyLoad: true,
        responsive: {
            0: { items: 1},
            768: { items: 2 },
            1024: { items: 4 },
            1200: { items: 4 }
        },
        onInitialized: aster_portfolio_setExpand,
        onTranslated: aster_portfolio_setExpand
        });

        function aster_portfolio_setExpand() {
        $('#topservices .owl-item').removeClass('expand');
        $('#topservices .owl-item.active').eq(1).addClass('expand');
        }

        // Hover effect
        $('#topservices').on('mouseenter', '.owl-item', function () {
            $('#topservices .owl-item').removeClass('expand');
            $(this).addClass('expand');
        });

        $('#topservices').on('mouseleave', function () {
            aster_portfolio_setExpand();
        });
    });
    
    /* -----------------------------------------
    Scroll Top
    ----------------------------------------- */
    var aster_portfolio_scrollToTopBtn = $('.aster-portfolio-scroll-to-top');

    $(window).scroll(function() {
        if ($(window).scrollTop() > 400) {
            aster_portfolio_scrollToTopBtn.addClass('show');
        } else {
            aster_portfolio_scrollToTopBtn.removeClass('show');
        }
    });

    aster_portfolio_scrollToTopBtn.on('click', function(e) {
        e.preventDefault();
        $('html, body').animate({
            scrollTop: 0
        }, '300');
    }); 
    
});

//Sticky Header
document.addEventListener('DOMContentLoaded', function() {
  const aster_portfolio_header = document.querySelector('.sticky-header');
  if (aster_portfolio_header) { // Check if header exists
    window.addEventListener('scroll', function() {
      if (window.scrollY > 0) {
        aster_portfolio_header.classList.add('is-sticky');
      } else {
        aster_portfolio_header.classList.remove('is-sticky');
      }
    });
  }
});

//Slider Image Text
document.addEventListener('DOMContentLoaded', function () {
	document.querySelectorAll('.slider-title').forEach(function (title) {

		const aster_portfolio_inner = title.querySelector('.slider-title-inner');
		if (!aster_portfolio_inner) return;

		const aster_portfolio_text = aster_portfolio_inner.textContent.trim();
		if (!aster_portfolio_text) return;

		const aster_portfolio_words = aster_portfolio_text.split(/\s+/);

		let aster_portfolio_imgOne = title.dataset.asterPortfolioImgOne;
		let aster_portfolio_imgTwo = title.dataset.asterPortfolioImgTwo;
		const aster_portfolio_defaultImg = title.dataset.asterPortfolioImgDefault || '';

		// fallback
		if (!aster_portfolio_imgOne) aster_portfolio_imgOne = aster_portfolio_defaultImg;
		if (!aster_portfolio_imgTwo) aster_portfolio_imgTwo = aster_portfolio_defaultImg;

		let aster_portfolio_html = '';

		aster_portfolio_words.forEach((word, index) => {
			aster_portfolio_html += `<span class="title-word">${word}</span>`;

			if (index === 0 && aster_portfolio_imgOne) {
				aster_portfolio_html += `
					<span class="title-img title-img-one">
						<img src="${aster_portfolio_imgOne}">
					</span>`;
			}

			if (index === 3 && aster_portfolio_imgTwo) {
				aster_portfolio_html += `
					<span class="title-img title-img-two">
						<img src="${aster_portfolio_imgTwo}">
					</span>`;
			}
		});

		aster_portfolio_inner.innerHTML = aster_portfolio_html;
	});
});

/* Progress Bar */
document.addEventListener("DOMContentLoaded", function () {
    const aster_portfolio_progressBar =
        document.getElementById("aster_portfolio_aster_progress_bar");
    if (!aster_portfolio_progressBar) return;
    window.addEventListener("scroll", function () {
        const aster_portfolio_scrollTop =
            document.documentElement.scrollTop || document.body.scrollTop;
        const aster_portfolio_height =
            document.documentElement.scrollHeight -
            document.documentElement.clientHeight;
        const aster_portfolio_scrolled =
            (aster_portfolio_scrollTop / aster_portfolio_height) * 100;
        aster_portfolio_progressBar.style.width =
            aster_portfolio_scrolled + "%";
    });
});
