jQuery(document).ready(function($) {
  "use strict";
  
  //Naeca Owl Carousel Slider Script
  $('.owl-carousel').each( function() {
    var $carousel = $(this);
    var $items = ($carousel.data('items') !== undefined) ? $carousel.data('items') : 1;
    var $items_tablet = ($carousel.data('items') !== undefined) ? $carousel.data('items-tablet') : 1;
    var $items_mobile_landscape = ($carousel.data('items-mobile-landscape') !== undefined) ? $carousel.data('items-mobile-landscape') : 1;
    var $items_mobile_portrait = ($carousel.data('items-mobile-portrait') !== undefined) ? $carousel.data('items-mobile-portrait') : 1;
    var $stagep_tablet = ($carousel.data('stagep-tablet') !== undefined) ? $carousel.data('stagep-tablet') : 1;
    var $stagep_desktop = ($carousel.data('stagep-desktop') !== undefined) ? $carousel.data('stagep-desktop') : 1;
    $carousel.owlCarousel ({
      loop : ($carousel.data('loop') !== undefined) ? $carousel.data('loop') : true,
      items : $carousel.data('items'),
      margin : ($carousel.data('margin') !== undefined) ? $carousel.data('margin') : 0,
      stagePadding : ($carousel.data('stagepadding') !== undefined) ? $carousel.data('stagepadding') : 0,
      dots : ($carousel.data('dots') !== undefined) ? $carousel.data('dots') : true,
      nav : ($carousel.data('nav') !== undefined) ? $carousel.data('nav') : false,
      navText : ["<div class='slider-no-current'><span class='current-no'></span><span class='total-no'></span></div><span class='current-monials'></span>", "<div class='slider-no-next'></div><span class='next-monials'></span>"],
      autoplay : ($carousel.data('autoplay') !== undefined) ? $carousel.data('autoplay') : false,
      autoplayTimeout : ($carousel.data('autoplay-timeout') !== undefined) ? $carousel.data('autoplay-timeout') : 5000,
      animateIn : ($carousel.data('animatein') !== undefined) ? $carousel.data('animatein') : false,
      animateOut : ($carousel.data('animateout') !== undefined) ? $carousel.data('animateout') : false,
      mouseDrag : ($carousel.data('mouse-drag') !== undefined) ? $carousel.data('mouse-drag') : true,
      autoWidth : ($carousel.data('auto-width') !== undefined) ? $carousel.data('auto-width') : false,
      autoHeight : ($carousel.data('auto-height') !== undefined) ? $carousel.data('auto-height') : false,
      center : ($carousel.data('center') !== undefined) ? $carousel.data('center') : false,
      responsiveClass: true,
      dotsEachNumber: true,
      smartSpeed: 600,
      autoplayHoverPause: true,
      responsive : {
        0 : {
          items : $items_mobile_portrait,
        },
        480 : {
          items : $items_mobile_landscape,
        },
        768 : {
          items : $items_tablet,
          stagePadding: $stagep_tablet,
        },
        1200 : {
          items : $items,
          stagePadding: $stagep_desktop,
        }
      }
    });
    var totLength = $('.owl-dot', $carousel).length;
    $('.total-no', $carousel).html(totLength);
    $('.current-no', $carousel).html(totLength);
    $carousel.owlCarousel();
    $('.current-no', $carousel).html(1);
    $carousel.on('changed.owl.carousel', function(event) {
      var total_items = event.page.count;
      var currentNum = event.page.index + 1;
      $('.total-no', $carousel ).html(total_items);
      $('.current-no', $carousel).html(currentNum);
    });
  });

  //Naeca Animation On Scroll Script
  AOS.init ({
    delay: 500,
  });

  //Naeca Code Copy and Past Script
  $('.naeca-code .naeca-btn').on('click', function() {
    var $codebutton = $(this);
    var $temp = $('<input>');
    $('body').append($temp);
    var $buttontext = $($codebutton).text();
    $temp.val($($codebutton).parent().find('span').text()).select();
    document.execCommand('copy');
    $temp.remove();
    $($codebutton).text('Copied!');
     setTimeout(function() {
      $($codebutton).text($buttontext);
     }, 2000);
  });

  // Naeca Tab
  $('.naeca-tab-links a').on('click', function(e) {
    var currentAttrValue = $(this).attr('href');

    // Show/Hide Tabs
    $('.naeca-tab-content ' + currentAttrValue).fadeIn(0).siblings().hide().stop(true, true);
    $('.naeca-tab-content ' + currentAttrValue).addClass('active').siblings().removeClass('active');

    // Change/remove current tab to active
    $(this).parent('li').addClass('active').siblings().removeClass('active');

    e.preventDefault();
  });

  //Naeca Popup Picture Script
  $('.naeca-popup').magnificPopup ({
    delegate: 'a.popup',
    type: 'image',
    closeOnContentClick: false,
    closeBtnInside: false,
    mainClass: 'mfp-with-zoom mfp-img-mobile',
    closeMarkup:'<div class="mfp-close" title="%title%"></div>',
    image: {
      verticalFit: true,
      titleSrc: function(item) {
        return item.el.data('title')+item.el.data('price');
      }
    },
    gallery: {
      enabled: true,
      arrowMarkup:'<div title="%title%" class="mfp-arrow mfp-arrow-%dir%"></div>',
    },
    zoom: {
      enabled: true,
      duration: 300,
      opener: function(element) {
        return element.find('*');
      }
    }
  });
  //Naeca Magnific Popup Video Script
  $('.naeca-popup-video').magnificPopup ({
    mainClass: 'mfp-fade',
    type: 'iframe',
    closeMarkup:'<div class="mfp-close" title="%title%"></div>',
    iframe: {
      patterns: {
        youtube: {
          index: 'youtube.com/',
          id: function(url) {
            var m = url.match(/[\\?\\&]v=([^\\?\\&]+)/);
            if ( !m || !m[1] ) return null;
            return m[1];
          },
          src: 'https://www.youtube.com/embed/%id%?autoplay=1'
        },
        vimeo: {
          index: 'vimeo.com/',
          id: function(url) {
            var m = url.match(/(https?:\/\/)?(www.)?(player.)?vimeo.com\/([a-z]*\/)*([0-9]{6,11})[?]?.*/);
            if ( !m || !m[5] ) return null;
            return m[5];
          },
          src: 'https://player.vimeo.com/video/%id%?autoplay=1'
        },
        dailymotion: {
          index: 'dailymotion.com/',
          id: function(url) {
            var m = url.match(/^.+dailymotion.com\/(video|hub)\/([^_]+)[^#]*(#video=([^_&]+))?/);
            if ( !m || !m[2] ) return null;
            return m[2];
          },
          src: 'https://iframespot.blogspot.com/ncr/?m=0&type=dv&url=https%3A%2F%2Fwww.dailymotion.com%2Fembed%2Fvideo%2F%id%%3Fapi%3D0%26autoplay%3D1%26info%3D0%26logo%3D0%26social%3D0%26related%3D0'
        }
      }
    }
  });
  if ($('div').hasClass('naeca-popup')) {
    $('.naeca-popup').find('a').attr("data-elementor-open-lightbox","no");
  }

  $(".naeca-order-filter .woocommerce-ordering option").each(function(){
    $(".naeca-order-list").append('<li><a href="?orderby=' + $(this).attr('value') + '">' + $(this).html() + '</a></li>');
  });

  var selector = '.naeca-order-list li';
  var url = window.location.href;
  var target = url.split('/');
   $(selector).each(function(){
      if($(this).find('a').attr('href')===(target[target.length-1])){
        $(selector).removeClass('show');
        $(this).removeClass('show').addClass('show');
      }
   });

  //Naeca Hover Script
  $('.product-items figure').on({
    mouseenter : function() {
      $(this).addClass('naeca-hover');
    },
    mouseleave : function() {
      $(this).removeClass('naeca-hover');
    }
  });

  $('.naeca-team-social span').hover(function() {
    $(this).parents(".naeca-team-social").addClass('active');
  });
  $('.naeca-team-social').on({
    mouseleave : function() {
      $(this).removeClass('active');
    }
  });

  // Counter Script
  $('.naeca-counter').counterUp ({
    delay: 1,
    time: 1000,
  });

  // Tab
  $('.naeca-tab-links a').on('click', function(e) {
    var currentAttrValue = $(this).attr('href');

    // Show/Hide Tabs
    $('.naeca-tab-content ' + currentAttrValue).fadeIn(0).siblings().hide().stop(true, true);
    $('.naeca-tab-content ' + currentAttrValue).addClass('active').siblings().removeClass('active');

    // Change/remove current tab to active
    $(this).addClass('active').siblings().removeClass('active');

    e.preventDefault();
  });

  // Match Height Script
  // $('.naeca-item').matchHeight();
  $('.naeca-item').matchHeight({
    property: 'min-height',
    byRow: false,
  });

  // Circle Progress Bar Script
  $('.circle-progressbar').each(function() {
  var $circle = $(this);
  var $fill_color = ($circle.data('color') !== undefined) ? $circle.data('color') : '#40407a';
  var $empty_fill_color = ($circle.data('empty') !== undefined) ? $circle.data('empty') : '#ffffff';
  var $size = ($circle.data('size') !== undefined) ? $circle.data('size') : '175';

    $circle.waypoint(function() {
      $circle.circleProgress ({
        size: $size,
        fill: {
          color: $fill_color,
        },
        thickness: 5,
        emptyFill: $empty_fill_color,
        startAngle: 300,
        reverse: true,
        lineCap: 'squre',
        animation: {
          duration: 1800
        }
      })
      .on('circle-animation-progress', function (event, progress, stepValue) {
        $(this).find('.circle-counter').text((stepValue * 100).toFixed(0));
      });
    },
    {
      offset: '100%',
      triggerOnce: true,
    });
  });

  // Countdown Script
  $('.naeca-countdown.static, .naeca-countdown.dynamic').each( function() {
    var $countdown = $(this);
    var date = $countdown.data("date");
    var format = $countdown.data("format");
    var count_format = format ? format : 'dHMS';
    // Plural Labels
    var years = $countdown.data("years");
    var months = $countdown.data("months");
    var weeks = $countdown.data("weeks");
    var days = $countdown.data("days");
    var hours = $countdown.data("hours");
    var minutes = $countdown.data("minutes");
    var seconds = $countdown.data("seconds");
    // Singular Labels
    var year = $countdown.data("year");
    var month = $countdown.data("month");
    var week = $countdown.data("week");
    var day = $countdown.data("day");
    var hour = $countdown.data("hour");
    var minute = $countdown.data("minute");
    var second = $countdown.data("second");

    var austDay = new Date();
    austDay = new Date(date);

    $countdown.countdown({
      until: austDay,
      labels: [years,months,weeks,days,hours,minutes,seconds],
      labels1: [year,month,week,day,hour,minute,second],
      format: count_format,
    });
  });

  // Fake COuntdown Script
  $('.naeca-countdown.fake').each( function() {
    var $countdown = $(this);
    var date = $countdown.data("date");
    var today = new Date();
    var newdate = new Date();
    newdate.setDate(today.getDate() + date);
    var mm = today.getMonth() + 1; //January is 0!
    var yyyy = today.getFullYear();
    today =  new Date(newdate);

    // Plural Labels
    var years = $countdown.data("years");
    var months = $countdown.data("months");
    var weeks = $countdown.data("weeks");
    var days = $countdown.data("days");
    var hours = $countdown.data("hours");
    var minutes = $countdown.data("minutes");
    var seconds = $countdown.data("seconds");
    // Singular Labels
    var year = $countdown.data("year");
    var month = $countdown.data("month");
    var week = $countdown.data("week");
    var day = $countdown.data("day");
    var hour = $countdown.data("hour");
    var minute = $countdown.data("minute");
    var second = $countdown.data("second");

    $('.naeca-countdown.fake').countdown({
      until: today,
      labels: [years,months,weeks,days,hours,minutes,seconds],
      labels1: [year,month,week,day,hour,minute,second],
    });
  });

  //Masonry Script
  $('.masonry-wrap').each(function(i, gridContainer) {
    var $gridContainer = $(gridContainer);
    var $grid = $gridContainer.find('.naeca-masonry').imagesLoaded(function() {
      $grid.isotope ({
        itemSelector: '.masonry-item',
        percentPosition: true,
        isFitWidth: true,
      })
    });
    $grid.packery({
      itemSelector: '.masonry-item'
    });
    $gridContainer.find('.naeca-order-filter.normal-filter').on('click', 'li a', function() {
      var filterValue = $(this).attr('data-filter');
      $grid.isotope ({
        filter: filterValue,
      });
    });
  });
  $('.naeca-order-filter').each( function( i, buttonGroup ) {
    var $buttonGroup = $(buttonGroup);
    $buttonGroup.on( 'click', 'li a', function() {
      $buttonGroup.find('.active').removeClass('active');
      $(this).parent('li').addClass('active');
    });
  });

  //Naeca Slick Slider
  $('.slick-vertical').each( function() {
    var $slick = $(this);
    $slick.slick ({
      dots: ($slick.data('dots') !== undefined) ? $slick.data('dots') : true,
      arrows: ($slick.data('arrows') !== undefined) ? $slick.data('arrows') : false,
      infinite: ($slick.data('infinite') !== undefined) ? $slick.data('infinite') : false,
      vertical: ($slick.data('vertical') !== undefined) ? $slick.data('vertical') : true,
      slidesToShow: ($slick.data('items') !== undefined) ? $slick.data('items') : 1,
      slidesToScroll: ($slick.data('scroll') !== undefined) ? $slick.data('scroll') : 1,
      verticalSwiping: ($slick.data('swiping') !== undefined) ? $slick.data('swiping') : true,
      responsive: [
        {
          breakpoint: 991,
          settings: {
            vertical: false,
            verticalSwiping: false,
            horizontalSwiping: true,
          }
        }
      ]
    });
  });

  

});