var $cards = $('#items .full-works');
var $card = $('#items .card');
var $cat = $('.cat-list .cat a');
var $img = $('.item #items .img');

function masonry() {
  var $grid = $cards.masonry({
      itemSelector: '.card',
      columnWidth: '.card',
      percentPosition: true,
      gutter: 10,
      isAnimated: true,
      transitionDuration: '0.5s'
  });

  var $grid2 = $img.masonry({
      itemSelector: '.img-card',
      columnWidth: '.img-card',
      percentPosition: true,
      gutter: 10,
      transitionDuration: '0.2s'
  });

  $(".cat-list .btn a").click(function(e) {
    e.preventDefault();

    $('.active').removeClass('active');
    $(this).addClass('active');

    var filter = $(this).attr("data-filter");
    var self = $('#body-wrapper #items .full-works');

    self.masonryFilter({
        filter: function () {
            if (!filter) return true;
            return $(this).attr("data-filter") == filter;
        }
    });
  });

  $grid.imagesLoaded().progress(function() {
      $grid.masonry();
  });

  $grid2.imagesLoaded().progress(function() {
      $grid2.masonry();
  });
}

function filteritem() {
  var $card = $('#body > .card');
  var $cat = $('.cat a');
  var $img = $('#body .img');
}

function contact() {
    var $contact = $('header .contact h3');

    $contact.on('click', function() {
        if ($(this).parent(".contact").hasClass('open')) {
            $(this).parent(".contact").removeClass('open');
            $(this).parent(".contact").addClass('close');
        } else {
            $(this).parent(".contact").addClass('open');
            $(this).parent(".contact").removeClass('close');
        }
    })
}

var $page = $('#page-wrapper.blog');
function customscroll($page_item) {
  var $page_item = $("#page-wrapper.item");

  $page_item.mCustomScrollbar({
    theme: "dark",
    scrollbarPosition: "outside",
    scrollInertia: 1000,
    callbacks: {
      onInit: function(){
        $('.top').on('click', function() {
          event.preventDefault()
          $page_item.mCustomScrollbar('scrollTo',['top',null]);
        });
      }
    }
  });

  $("#page-wrapper.blog").mCustomScrollbar({
      theme: "dark",
      scrollbarPosition: "outside",
      callbacks: {
        whileScrolling: function() {
          var pct = this.mcs.topPct;
          if ( ($page.hasClass('down')) ) {
          } else {
            $("#page-wrapper").addClass("down");
            $(this).mCustomScrollbar("disable",true);
            setTimeout(function(){
              console.log('time');
              $("#page-wrapper").mCustomScrollbar("update");
            }, 500);
          }
        }
      }
  });
}

function clickscroll() {
  var $btnscroll = $('.scroll');
  $btnscroll.on('click', function() {
    $page.addClass("down");
  })
}

jQuery(document).ready(function($) {
    masonry();
    filteritem();
    contact();
    customscroll();
    clickscroll();
});
