(function ($) {
    var RoyalNews = {
        storiesSlider: function(){
            var storiesWrap = $( '.royalnews-stories-wrap' );
            if( storiesWrap.length > 0 ){                
                $( storiesWrap ).slick({
                    infinite: true,
                    slidesToShow: 3,
                    slidesToScroll: 1,
                    autoplay: true,
                    autoplaySpeed: 1500,
                    arrows: true,
                    dots: false,
                    prevArrow: '<button class="slick-prev royalnews-arrow-prev"><span class="fa fa-angle-left"></span></button>',
                    nextArrow: '<button class="slick-prev royalnews-arrow-next"><span class="fa fa-angle-right"></span></button>',
                    responsive: [
                        {
                          breakpoint: 1024,
                          settings: {
                            slidesToShow: 3,
                            slidesToScroll: 3,
                            infinite: true,
                            dots: false
                          }
                        },
                        {
                          breakpoint: 840,
                          settings: {
                            arrows: true,
                            dots: false,
                            slidesToShow: 2,
                            slidesToScroll: 2
                          }
                        },
                        {
                          breakpoint: 767,
                          settings: {
                            slidesToShow: 1,
                            arrows: true,
                            dots: false,
                            slidesToScroll: 1
                          }
                        }
                    ]
                }); 
            }
        },
        bannerSlider: function(){
            var wrapper = $( '.royalnews-main-slider-initialize' );
            if( wrapper.length > 0 ){                
                $( wrapper ).slick({
                    infinite: true,
                    slidesToShow: 1,
                    slidesToScroll: 1,
                    autoplay: true,
                    autoplaySpeed: 3000,
                    arrows: true,
                    dots: true,
                    prevArrow: '<button class="slick-prev royalnews-arrow-prev"><span class="fa fa-angle-left"></span></button>',
                    nextArrow: '<button class="slick-prev royalnews-arrow-next"><span class="fa fa-angle-right"></span></button>',
                  responsive: [
                    {
                      breakpoint: 1024,
                      settings: {
                        slidesToShow: 3,
                        slidesToScroll: 3,
                        infinite: true,
                        dots: true
                      }
                    },
                    {
                      breakpoint: 840,
                      settings: {
                        arrows: true,
                        dots: false,
                        slidesToShow: 2,
                        slidesToScroll: 2
                      }
                    },
                    {
                      breakpoint: 767,
                      settings: {
                        slidesToShow: 1,
                        arrows: true,
                        dots: false,
                        slidesToScroll: 1
                      }
                    }
                  ]
                });
            }
        },
        FooterNewsSlider: function(){
            var wrapper = $( '.royalnews-footer-news-initilize' );
            if( wrapper.length > 0 ){                
                $('.royalnews-footer-news-initilize').slick({
                    infinite: true,
                    slidesToShow: 3,
                    slidesToScroll: 1,
                    autoplay: true,
                    autoplaySpeed: 3000,
                    arrows: true,
                    dots: true,
                    prevArrow: '<button class="slick-prev royalnews-arrow-prev"><span class="fa fa-angle-left"></span></button>',
                    nextArrow: '<button class="slick-prev royalnews-arrow-next"><span class="fa fa-angle-right"></span></button>',
                  responsive: [
                    {
                      breakpoint: 1024,
                      settings: {
                        slidesToShow: 3,
                        slidesToScroll: 3,
                        infinite: true,
                        dots: true
                      }
                    },
                    {
                      breakpoint: 840,
                      settings: {
                        arrows: true,
                        dots: false,
                        slidesToShow: 2,
                        slidesToScroll: 2
                      }
                    },
                    {
                      breakpoint: 767,
                      settings: {
                        slidesToShow: 1,
                        arrows: true,
                        dots: false,
                        slidesToScroll: 1
                      }
                    }
                  ]
                });
            }
        },
        mobileMenuInit: function( container ){
            // Add dropdown toggle that displays child menu items.
            var dropdownToggle = $( '<a/>', { 'class': 'dropdown-toggle','href': '#' })
                .append( $( '<i/>', {'class': 'fas fa-chevron-down'} ) );

            $( container ).find( '.menu-item-has-children > a, .page_item_has_children > a' ).after( dropdownToggle );

            $( container ).find( '.dropdown-toggle' ).on( 'click', function( e ) {
                e.preventDefault();
                $( this ).toggleClass( 'sub-menu-on' );
                var subMenuIcon = $( this ).find( 'i' );
                if( $( this ).hasClass( 'sub-menu-on' ) ){
                    $( subMenuIcon ).removeClass( 'fa-chevron-down' );
                    $( subMenuIcon ).addClass( 'fa-chevron-up' );
                    $( this ).next( 'ul' ).slideDown(500);
                }else{
                    $( subMenuIcon ).removeClass( 'fa-chevron-up' );
                    $( subMenuIcon ).addClass( 'fa-chevron-down' );
                    $( this ).next( 'ul' ).slideUp(500);  
                }
                $( this ).next( '.children, ul' ).toggleClass( 'sub-menu-on' );

            } );
        },
        siteMenuToggler: function(){
            $( document ).on( 'click', '#royalnews-menu-toggle', function(e){
                e.preventDefault();
                var icon = $( this ).find( 'i' );
                $( 'body' ).toggleClass( 'menu-open' );
                $( this ).toggleClass( 'menu-open' );

                var navMenu = $( this ).next( '.royalnews-primary-menu' );
                if( $( this ).hasClass( 'menu-open' ) ){
                    $( icon ).removeClass( 'fa-bars' );
                    $( icon ).addClass( 'fa-times' );
                    navMenu.slideDown(500);                    
                }else{
                    $( icon ).removeClass( 'fa-times' );
                    $( icon ).addClass( 'fa-bars' );
                    navMenu.slideUp(500);
                }
            });
        },
        scrollToTop: function(){
            var scrollWrap = $( '.royalnews-scroll-to-top a' );
            if( scrollWrap.length > 0 ){
                $(window).on( 'scroll', function() {                    
                    if ($( window ).scrollTop() > 100) {
                        $( scrollWrap ).show();
                        $( '.royalnews-scroll-arrow,.royalnews-scroll-text' ).show();
                    } else {
                        $( scrollWrap ).hide();
                        $( '.royalnews-scroll-arrow,.royalnews-scroll-text' ).hide();
                    }
                });          
            }

            $('.royalnews-scroll-to-top a').click(function (e) {
                e.preventDefault();
                $("body,html").animate({
                    scrollTop: 0,
                }, 300);
                return false;
            });
        },
        stickyHeader: function( container ){
            if( $( 'body' ).hasClass( 'royalnews-sticky-header' ) ){
                $( container ).sticky({
                    topSpacing: 0
                })

                $( container ).on('sticky-start', function() { 
                    $( '.royalnews-primary-menu-wrapper' ).addClass( 'is-sticky' );
                });
                $( container ).on('sticky-end', function() { 
                    $( '.royalnews-primary-menu-wrapper' ).removeClass( 'is-sticky' );
                });
            }
        },
        updateClock: function() {
            var timeWrapper = $( '.royalnews-digital-clock-wrapper' ),
                updateWrapper = $( timeWrapper ).prev(),
                selectedTimeZone = $( timeWrapper ).data( 'timezone' ),
                timeZoneType= $( timeWrapper ).data( 'timezone-type' ),
                dateFormat = $( timeWrapper ).data( 'date-format' ),
                timeWrapperInner = $( timeWrapper ).find( '.royalnews-time' ),
                currentDateString = false,
                currentTimeString, hours, minutes, seconds, ampm,
                currentTime = new Date();

            
            if( 'timezone' == timeZoneType ){
                currentTimeString = currentTime.toLocaleTimeString( 'en-US', {
                    timeZone: `${selectedTimeZone}`
                });

                currentDateString = currentTime.toLocaleDateString( 'en-US', {
                    timeZone: `${selectedTimeZone}`,
                    year: 'numeric',
                    month: 'long',
                    day: 'numeric'
                });
            }else{            
                hours = currentTime.getHours();
                minutes = currentTime.getMinutes();
                seconds = currentTime.getSeconds();
                ampm = hours >= 12 ? 'PM' : 'AM';
                hours = hours % 12;
                hours = hours ? hours : 12; // the hour '0' should be '12'
                minutes = minutes < 10 ? '0'+ minutes : minutes;
                seconds = ( seconds < 10 ? "0" : "" ) + seconds;
                currentTimeString = hours + ":" + minutes + ":" + seconds + ' ' + ampm;
            }
            
            if( currentDateString ){
                var dateMarkup = '<time>'+ currentDateString +'</time>';
                $( updateWrapper ).html( dateMarkup );
            }
            
            if( timeWrapperInner.length > 0 ) {
                $( timeWrapperInner ).show();
                $( timeWrapperInner ).html( currentTimeString );
            }
        },
        darkModeToggle: function(){
            $( document ).on( 'change', '.royalnews-switch input', function( e ){
                e.preventDefault();

                $( 'body' ).toggleClass( 'royalnews-dark-mode' );
            });
        },
        init : function(){
            var self = this;
            this.bannerSlider();
            this.storiesSlider();
            this.FooterNewsSlider();
            this.scrollToTop();
            this.mobileMenuInit( '.royalnews-primary-menu' );
            this.stickyHeader( '.royalnews-header-wrap' );
            this.siteMenuToggler();
            this.darkModeToggle();

            setInterval( function() {
              self.updateClock();
            }, 1000 );
        }
    }

    $(document).ready(function(){
        RoyalNews.init();
    });

    //preloader
    $( window ).load( function () {
        $(".royalnews-loader-wrapper").fadeOut();
        $(".royalnews-loader").delay(1000).fadeOut("slow");
    });
})(jQuery)