// source --> https://www.haberkaos.com/wp-content/themes/news247/js/custom.js?ver=1 
jQuery(document).ready(function($){
  'use strict';

  // Sticky Nav
  $(window).on('scroll', function(){
    if ( $(window).scrollTop() > 0 ) {
      $('.thw-menubar-wrap').addClass('sticky-header');
    } else {
      $('.thw-menubar-wrap').removeClass('sticky-header');
    }

  });

   // Search
    $('.nav-search').on('click',function () {
       $('#home-search.search').fadeIn(500);
   });

    $('.search-close').on('click',function(){
           $('#home-search.search').fadeOut(500);
    });

    /* ----------------------------------------------------------- */
    /*  Back to top
    /* ----------------------------------------------------------- */

        $(window).scroll(function () {
            if ($(this).scrollTop() > 50) {
                 $('#back-to-top').fadeIn();
            } else {
                 $('#back-to-top').fadeOut();
            }
        });

        // scroll body to 0px on click
        $('#back-to-top').on('click', function () {
             $('#back-to-top').tooltip('hide');
             $('body,html').animate({
                  scrollTop: 0
             }, 800);
             return false;
        });

        $('#back-to-top').tooltip('hide');

    /* ----------------------------------------------------------- */
    /*  Sticky Sidebar
    /* ----------------------------------------------------------- */
    jQuery('.stickys').theiaStickySidebar();


    /* ----------------------------------------------------------- */
    /*  woocommerce product popup
    /* ----------------------------------------------------------- */
    $('.woocommerce-product-gallery__image a').magnificPopup({
        type: 'image',
        closeOnContentClick: true,
        mainClass: 'mfp-img-mobile',
        image: {
            verticalFit: true
        }
    });

    // Post Loading
    $('#post-loading-button').on('click',function(event){
        event.preventDefault();

        var $that = $(this);
        if($that.hasClass('disable')){
            return false;
        }

        var contentwrap = $('.main-content-inner'), // item contentwrap
            postperpage = $that.data('post_per_page'), // post per page number
            showallposts = $that.data('show_total_posts'); // total posts count

        var items = contentwrap.find('.post-layout'),
            totalpostnumber = items.length,
            paged = ( totalpostnumber / postperpage ) + 1; // paged number

        $.ajax({
            url: news247_ajax.ajax_url,
            type: 'POST',
            data: {action: 'news247_post_ajax_loading',postperpage: postperpage,paged:paged},
            beforeSend: function(){
                $that.addClass('disable');
                $('<i class="fa fa-spinner fa-spin" style="margin-left:10px"></i>').appendTo( "#post-loading-button" ).fadeIn(100);
            },
            complete:function(){
                $('#post-loading-button .fa-spinner ').remove();
            }
        })

        .done(function(data) {
            var newLenght  = contentwrap.find('.post-layout').length;
            if(showallposts <= newLenght){
              $('.post-content-loading').fadeOut(300,function(){
                $('.post-content-loading').remove();
              });
            }
            $that.removeClass('disable');

            var $pstitems = $(data);
            $('.main-content-inner').append( $pstitems );
            $('.post-layout').addClass('imgfullsize');
        })

        .fail(function() {
            alert('Loading Failed');
        });

    });

});