// initiate
$('.product').each(function(i){
 $(this).data('initial-index', i);
 $(this).data('listing-price', $(this).find('.prijssort').text());
 $(this).data('listing-discount', $(this).find('.korting').text());
});
var teller = 0;

// laad meer producten
$('#laden').click(function() {
 $(".errortext").remove();
 if (teller==0) {
  $('.notactiv').each(function(i){
   if (teller < 36) {
    $(this).removeClass("notactiv");
    $(this).addClass("active");
    $(this).appendTo('.products');
    teller++;
   }
  });
  if(teller==36) { teller=0; } else { document.getElementById('laden').style.display = "none"; }
 }
});

function sorteert( nr_active ){
  sorteer( nr_active );

}

// sorteer producten op prijs en korting
function sorteer( nr_active ){
  if (!nr_active) { nr_active = $('.active').length; }
  $('.active').each(function(i){
    $(this).removeClass("active");
    $(this).addClass("notactiv");
    $(this).appendTo('.noshow');
  });
  $('.special1').each(function(i){ $(this).appendTo('.nogo'); });
  $('.special2').each(function(i){ $(this).appendTo('.nogo'); });
  temp = $('select[name=products-selector]').val()
  var divList = $(".noshow .notactiv");
  if (temp=='prijs') {
    divList.sort(function(a, b){ return (($(a).data("listing-price")-$(b).data("listing-price"))*100000 + ($(b).data("listing-discount")-$(a).data("listing-discount")))});
  } else if (temp=='prijs desc') {
//    divList.sort(function(a, b){ return $(b).data("listing-price")-$(a).data("listing-price")});
    divList.sort(function(a, b){ return (($(b).data("listing-price")-$(a).data("listing-price"))*100000 + ($(b).data("listing-discount")-$(a).data("listing-discount")))});
  } else if (temp=='korting') {
//    divList.sort(function(a, b){ return $(a).data("listing-discount")-$(b).data("listing-discount")});
    divList.sort(function(a, b){ return (($(a).data("listing-discount")-$(b).data("listing-discount"))*100000 + ($(b).data("listing-price")-$(a).data("listing-price")))});
  } else if (temp=='korting desc') {
//    divList.sort(function(a, b){ return $(b).data("listing-discount")-$(a).data("listing-discount")});
	divList.sort(function(a, b){ return (($(b).data("listing-discount")-$(a).data("listing-discount"))*100000 + ($(b).data("listing-price")-$(a).data("listing-price")))});
  }
  $(".noshow").html(divList);
  temp = 0;
  $('.noshow .notactiv').each(function(){
   if(temp<nr_active) {
    $(this).removeClass("notactiv");
    $(this).addClass("active");
    $(this).appendTo('.products');
    temp++;
   }
  });

  $('.product').each(function(i){
    $(this).data('listing-price', $(this).find('.prijssort').text()); 
    $(this).data('listing-discount', $(this).find('.korting').text());
  });
  var temp_nr_active = $('.active').length;
  if (temp_nr_active==3 || temp_nr_active>6) {
    $('.special1').each(function(i){  $(this).appendTo('.products'); });
    $(".products > div:nth-child(6)").after($('.uitgelicht'));
    $(".products > div:nth-child(7)").after($('#none0'));
    $(".products > div:nth-child(8)").after($('#none1'));
    $(".products > div:nth-child(9)").after($('#none2'));
    $(".products > div:nth-child(10)").after($('#none3'));
    $(".products > div:nth-child(11)").after($('#none4'));
  }
  if (temp_nr_active>12) {
    $('.special2').each(function(i){  $(this).appendTo('.products'); });
    $(".products > div:nth-child(18)").after($('.aanrader'));
    $(".products > div:nth-child(19)").after($('#none5'));
    $(".products > div:nth-child(20)").after($('#none6'));
    $(".products > div:nth-child(21)").after($('#none7'));
    $(".products > div:nth-child(22)").after($('#none8'));
    $(".products > div:nth-child(23)").after($('#none9'));
    $(".products > div:nth-child(24)").after($('#none10'));
    $(".products > div:nth-child(25)").after($('#none11'));
  }
  if ($('.noshow').children().length===0) { document.getElementById('laden').style.display = "none"; } else { document.getElementById('laden').style.display = "block"; }
}


function prijsselect() {
  var nr_active = $('.active').length;
  if (nr_active<12) { nr_active=12; }
  var teller = 0;
  var minprijs = [];
  var maxprijs = [];
  var ietschecked = false;
  $('.active').each(function(i){
    $(this).removeClass("active");
    $(this).addClass("notactiv");
    $(this).appendTo('.noshow');
  });
  if ($('input[name="van"]').val()!='' && $('input[name="tot"]').val()!='') {
      minprijs[teller] = parseInt( $('input[name="van"]').val() );
      maxprijs[teller] = parseInt( $('input[name="tot"]').val() );
      teller++;
	  ietschecked = true;
  }
  $('.prijs-select').each(function(i){
    if($(this).is(':checked')) {
      temp = $(this).val().split(',');
      minprijs[teller] = parseInt(temp[0]);
      maxprijs[teller] = parseInt(temp[1]);
      teller++;
	  ietschecked = true;
    }
  });

  if(!ietschecked){
	  minprijs[0] = 0;
	  maxprijs[0] = 999999;
	  teller = 1;
	  $('.g_products').show();
	  $('.columlay').show();
  }else{
	  $('.g_products').hide();
	  $('.columlay').hide();
  }

  if (teller != 0) {
    $('.product').each(function(i){
      $(this).removeClass("active");
      $(this).addClass("notactiv");
      toon = 0;
      prijs = parseInt($(this).find('.prijssort').text());
      for(j=0;j<teller;j++) {
        if (prijs >= minprijs[j] && prijs <= maxprijs[j]) { toon = 1; }
      }
      if (toon!==1) {
        $(this).appendTo('.nogo');
      } else {
        $(this).appendTo('.noshow');
      }
    });
  }
  $('.colour-select').each(function(i){
    if ($(this).css('border-left-color') === 'rgb(255, 0, 0)') {
      temp1 = $(this).prop('class').split('btn colour-select ');
      temp = temp1[1];
      $('.noshow .product').each(function(i){
        if ($(this).prop('class').search(temp)==-1 && temp!=='none') {
          $(this).appendTo('.nogo');
        }
      });
    }
  });
  $('.brand-select').each(function(i){
    if ($(this).css('font-weight') === 'bold' || $(this).css('font-weight') === '700') {
      temp1 = $(this).prop('class').split('brand-select');
      temp = temp1[1];
      $('.noshow .product').each(function(i){
        if ($(this).prop('class').search(temp)==-1) {
          $(this).appendTo('.nogo');
        }
      });
    }
  });
  if($('.noshow').children().length===0) {
    $(".products").html("<div class='errortext'>Sorry, er zijn geen producten gevonden die aan <br>je zoekcriteria voldoen. Probeer een andere selectie.</div>");
  } else {
    $(".errortext").remove();
    return sorteer(nr_active);
  }
}

function brand(temp) {
  brandcount = 0;
  brandclick = 0;
  $('.brand-select').each(function(i){
    brandcount++;
    if($(this).prop('class').search(temp)==-1) {
      $(this).css('font-weight', 'normal');
    } else { 
      if($(this).css('font-weight')=='bold') {
        $(this).css('font-weight', 'normal');
        temp = 'product'; 
      } else  {
        brandclick = brandcount;
        $(this).css('font-weight', 'bold');
      }
    }
  });
  $('.nogo .product').each(function(i){
    if ($(this).prop('class').search(temp)!==-1) {
      $(this).appendTo('.noshow');
    }
  });
  if (brandclick > 10 ) {  scrollToAnchor('scrollpoint');  }
  prijsselect();
}

function scrollToAnchor(aid){
    var aTag = $("a[name='"+ aid +"']");
    $('html,body').animate({scrollTop: aTag.offset().top},'slow');
}

function colour(temp) {
  $('.colour-select').each(function(i){
    if($(this).prop('class').search(temp)==-1) {
      if($(this).prop('class').search('none')==-1) {
        $(this).css('border', '2px solid white');
      } else {
        $(this).css('border', '2px solid #ddd');
      }
    } else {
      if($(this).prop('class').search('none')==-1) {
        $(this).css('border', '2px solid red');
      }
    }
  });
  $('.nogo .product').each(function(i){
    if ( $(this).prop('class').search(temp)!==-1 || temp=='none' ) {
      $(this).appendTo('.noshow');
    }
  });
  prijsselect();
}

// LEES MEER
var $el, $ps, $up, totalHeight;
$(".category-bar .button").click(function() {
 totalHeight = 0
 $el = $(this);
 $p  = $el.parent();
 $up = $p.parent();
 $ps = $up.find("p:not('.read-more')");
 // measure how tall inside should be by adding together heights of all inside paragraphs (except read-more paragraph)
 $ps.each(function() {
  totalHeight += $(this).outerHeight();
 });
 // add headings
 totalHeight += 110;
  $up
    .css({
      // Set height to prevent instant jumpdown when max height is removed
      "height": $up.height(),
      "max-height": 9999
    })
    .animate({ "height": totalHeight });
 // fade out read-more
 $p.fadeOut();
 // prevent jump-down
 return false;
});

$(".authorblok .button").click(function(event) {
 event.preventDefault();
 totalHeight = 0
 $el = $(this);
 $p  = $el.parent();
 $up = $p.parent();
 $ps = $up.find("p:not('.read-more')");
 // measure how tall inside should be by adding together heights of all inside paragraphs (except read-more paragraph)
 $ps.each(function() {
  totalHeight += $(this).outerHeight();
 });
 // add img
 totalHeight += 110;
  $up
    .css({
      // Set height to prevent instant jumpdown when max height is removed
      "height": $up.height(),
      "max-height": 9999
    })
    .animate({ "height": totalHeight });
 // fade out read-more
 $p.fadeOut();
 // prevent jump-down
 return false;
});

$(".brandselector .button").click(function() {
 totalHeight = 0
 $el = $(this);
 $p  = $el.parent();
 $up = $p.parent();
 $ps = $up.find("div");
 // measure how tall inside should be by adding together heights of all inside paragraphs (except read-more paragraph)
 $ps.each(function() {
  totalHeight += $(this).outerHeight();
 });
  $up
    .css({
      // Set height to prevent instant jumpdown when max height is removed
      "height": $up.height(),
      "max-height": 9999
    })
    .animate({ "height": totalHeight });
 // fade out read-more
 $p.fadeOut();
 // prevent jump-down
 return false;
});

function swapfilter(wat) {
  if(window.screen.availWidth < 768) {
    var element = document.getElementById(wat);
    if (element.style.overflow == 'visible') {
      element.style.overflow = 'hidden';
      element.style.height = '61px';
      element.style.backgroundImage = 'url(../gfx/arrow-bottom-white.png?p=12)';
    } else {
      element.style.overflow = 'visible';
      element.style.height = 'auto';
      element.style.backgroundImage = 'url(../gfx/arrow-bottom-white-up.png)';
    }
  }
}
