var loading_img = false;
var spinner = '<div class="sk-fading-circle align-middle"><div class="sk-circle1 sk-circle"></div><div class="sk-circle2 sk-circle"></div><div class="sk-circle3 sk-circle"></div><div class="sk-circle4 sk-circle"></div><div class="sk-circle5 sk-circle"></div><div class="sk-circle6 sk-circle"></div><div class="sk-circle7 sk-circle"></div><div class="sk-circle8 sk-circle"></div><div class="sk-circle9 sk-circle"></div><div class="sk-circle10 sk-circle"></div><div class="sk-circle11 sk-circle"></div><div class="sk-circle12 sk-circle"></div></div>';

$('.woocommerce .lazy').lazy({
  afterLoad: function(element){
    element.parent().parent().addClass("loaded");
    setTimeout(function(){element.parent().parent().children(".spinner").remove();},200);
  },
  onFinishedAll: function(element){
  }
});


var thresholdval = (($('.main-slider-lazy').length * $('.header-slider').height()) * 2);
$('.main-slider-lazy').lazy({
  effect: "fadeIn",
  effectTime: 1000,
 bind:"event",
  delay: 0,
  afterLoad:function(){
    $(".owl-item.active .caption").addClass("showup");       
  }
});

if($(".header-slider .item").length > 1){
  $(".header-slider").owlCarousel({
    navigation : true, // Show next and prev buttons
    slideSpeed : 300,
    paginationSpeed : 400,
    singleItem:true,
   // autoPlay : 4000,
    transitionStyle : "fade",
    addClassActive : true,
    // Navigation
    navigation : false,

    //Pagination
    pagination : true,
    paginationNumbers: false,
    afterInit : function(){
      //$(".header-slider .owl-item.active .caption").addClass("showup");    INICIADO EM depois da imagem carregar lazy.jquery 
    },
    beforeMove : function(){
    },
    afterMove : function(){
      $(".header-slider .owl-item.active .caption").addClass("showup");
    }
  });
}
$(document).ready(function(){

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

$(".hamburger").click(function(){
  $("header").toggleClass("menu-visible");
});
$(".show-cat-list").click(function(){
  $(".sidebar").addClass("visible");
});
$("body").click(function(){
  $("header").removeClass("menu-visible");
  $(".sidebar").removeClass("visible");
    //$(".menu-button.close").addClass("visible");
});
$("header nav, .hamburger, .header-store-buttons, .sidebar, .show-cat-list").click(function(e){
  e.stopPropagation();
});
$(".menu-button.close").click(function(){
  $("header").removeClass("menu-visible");
  $(".menu-button.close").removeClass("visible"); 
});
$(".sidebar .menu-button.close").click(function(){
  $(".sidebar").removeClass("visible");
});

  
});


/* LOAD MORE POSTS */

function loadmore(url, currpage, totalpages){

  if(totalpages > currpage){

    $(".show-more-btn").addClass("loading");

    $("#load-it").load(url+" ul.products > *",function(){
     
      $("#load-it > *").addClass("new inactive");
      var posts_written = $("ul.products li").length;
      var posts_loaded = $("#load-it li").length;

      $("ul.products").append($("#load-it > *"));

      $(".show-more-btn").removeClass("loading");
      
      $('.new img.lazy').lazy({
          bind:'event',
          afterLoad: function(element){
            element.parent().parent().addClass("loaded");
            setTimeout(function(){element.parent().parent().children(".spinner").remove();},200);
          
          }
        });
      var counter = 1;
      var show_delay = setInterval(function() {
        
        var nthchild=posts_written+counter;
        $("ul.products li:nth-child("+nthchild+")").removeClass("inactive");
        counter = counter+1;
        if(counter > posts_loaded){
          clearInterval(show_delay);
          $("ul.products li").removeClass("new");
        }
      }, 250); // every 1000 ms
   

       var currpageafterload = currpage + 1;
       var nextpageafterload = currpageafterload + 1;
       var newurl = url.replace("/"+currpageafterload, "")+"/"+nextpageafterload;
        $(".show-more-btn").data("currentpage",currpageafterload).attr("href",newurl);
        if(totalpages <= currpageafterload){
           $(".show-more-btn").addClass("no-posts");
        }
    });
    
  }else{

     $(".show-more-btn").addClass("no-posts");

  }

}
$(document).ready(function(){
  $(".show-more-btn").click(function(){
    var url = $(this).attr("href");
    var url = url.replace(/\/$/, "");
    var currpage = $(this).data("currentpage");
    var totalpages = $(this).data("totalpages");
    console.log(currpage);
    console.log(totalpages);
    loadmore(url,currpage,totalpages);

    return false;
  });
  if($(".images .thumbnails").length){
    single_product_vslider("", 3);
  }
});



function getYpos(matrix) {
    return matrix.css("transform").substr(7, matrix.css("transform").length - 8).split(', ')[5];
}
var currMov = 1;
function single_product_vslider(direction, imagesVisible){
  var thumb = $(".thumbnails .thumb-image .inner");
  var thumbsSlide = $(".thumbnails-slide");
  var nThumbs = thumb.length;
  var movementDistance = parseInt((thumb.height()+4+16)*imagesVisible);
  var nMovements = Math.ceil(nThumbs/imagesVisible);
  var currPos = parseInt(getYpos(thumbsSlide));
  console.log(nMovements);

  if(direction == "down"){
    if(currMov < nMovements){
      thumbsSlide.css({
       transform: 'translateY('+(currPos-movementDistance)+'px)',
       MozTransform: 'translateY('+(currPos-movementDistance)+'px)',
       WebkitTransform: 'translateY('+(currPos-movementDistance)+'px)',
       msTransform: 'translateY('+(currPos-movementDistance)+'px)'
      });
      currMov++;
      console.log(currMov);
      $(".go-up").removeClass("invisible");
      if(currMov == nMovements){
        $(".go-down").addClass("invisible");
      }
    }
  }else if(direction == "up"){
    if(currMov > 1){
      thumbsSlide.css({
       transform: 'translateY('+(currPos+movementDistance)+'px)',
       MozTransform: 'translateY('+(currPos+movementDistance)+'px)',
       WebkitTransform: 'translateY('+(currPos+movementDistance)+'px)',
       msTransform: 'translateY('+(currPos+movementDistance)+'px)'
      });
      currMov--;
      console.log(currMov);
      $(".go-down").removeClass("invisible");
      if(currMov == 1){
        $(".go-up").addClass("invisible");
      }
    }
  }
}
$(document).ready(function(){
  if($(".images .thumbnails").length){
    $(".go-down").click(function(){
      single_product_vslider("down", 3);
    });
    $(".go-up").click(function(){
      single_product_vslider("up", 3);
    });
  }
  var currSrcset = $(".main-image img").attr("srcset");
  var currBigsrc = $(".main-image img").attr("href");
  $(".thumbnails .thumb-image a").hover(function(){
    currSrcset = $(".main-image img").attr("srcset");
    currBigsrc = $(".main-image img").attr("href");
    var srcset = $(this).children("img").attr("srcset");
    var bigsrc = $(this).attr("href");
    console.log(srcset);
    console.log(bigsrc);
    $(".main-image img").attr({"src":bigsrc,"srcset":srcset});
  },function(){
    $(".main-image img").attr({"src":currBigsrc,"srcset":currSrcset});
  });
  $(".thumbnails .thumb-image a").click(function(e){
    currSrcset = $(".main-image img").attr("srcset");
    currBigsrc = $(".main-image img").attr("href");
    var srcset = $(this).children("img").attr("srcset");
    var bigsrc = $(this).attr("href");
    console.log(srcset);
    console.log(bigsrc);
    $(".main-image img").attr({"src":bigsrc,"srcset":srcset});
    e.preventDefault();
    return false;
  });
});

$('body').on( 'ql_visual_attributes_load', function(){
  $('.ql-visual-attributes').css("display","bock");
  $('.va-attribute-label').each(function(){
    $(this).next().addBack().wrapAll('<div class="picker-wrapper" />');
  });
  $('.picker-wrapper').each(function(){
      var $pickers = $( this ), $picker = $pickers.children().children().first(), pickersClass = '';
      if ( $picker.data('attribute') ) {
          pickersClass = $picker.data('attribute');
      }
      $pickers.addClass( pickersClass );
  });
  $( ".picker-wrapper" ).first().css( "position", "relative" );
  $( ".picker-wrapper" ).first().css( "margin-left", "120px" );
  if($(".picker-wrapper").first().hasClass("pa_cor")){
    $(".picker-wrapper").first().css({"width":"auto","margin-top":"70px"});
  }
});
if($("#woocommerce_msg").length > 0){
  $("html, body").animate({scrollTop:$('#woocommerce_msg').offset().top}, 100);
}else if($(".product.type-product").length > 0){
  $("html, body").animate({ scrollTop:$("header").height()+$(".header-slider").height()-150 }, 500);
}

// [
// {
//   "variation_id":258,
//   "variation_is_visible":true,
//   "variation_is_active":true,
//   "is_purchasable":true,
//   "display_price":30,
//   "display_regular_price":30,
//   "attributes":{
//     "attribute_pa_tamanhos":"l",
//     "attribute_pa_altura":"",
//     "attribute_pa_compressao":"",
//     "attribute_pa_pe":"",
//     "attribute_pa_cor":"",
//     "attribute_pa_modelo":"collant-materno"
//   },
//   "image_src":"",
//   "image_link":"",
//   "image_title":"",
//   "image_alt":"",
//   "image_srcset":"",
//   "image_sizes":"",
//   "price_html":"<span class=\"price\"><span class=\"amount\">30.00&nbsp;&euro;<\/span><\/span>",
//   "availability_html":"",
//   "sku":"SAC 0800 707 7311",
//   "weight":" g",
//   "dimensions":"",
//   "min_qty":1,
//   "max_qty":null,
//   "backorders_allowed":false,
//   "is_in_stock":true,
//   "is_downloadable":false,
//   "is_virtual":false,
//   "is_sold_individually":"no",
//   "variation_description":""
// }