$(document).ready(function () {
  /* 價錢篩選*/
  // $('.nstSlider').nstSlider({
  //     "left_grip_selector": ".leftGrip",
  //     "right_grip_selector": ".rightGrip",
  //     "value_bar_selector": ".bar",
  //     "highlight": {
  //         "grip_class": "gripHighlighted",
  //         "panel_selector": ".highlightPanel"
  //     },
  //     "value_changed_callback": function(cause, leftValue, rightValue) {
  //         $('.leftLabel').text(leftValue);
  //         $('.rightLabel').text(rightValue);
  //     },
  // });
  // Call methods and such...
  // var highlightMin = Math.random() * 20,
  //     highlightMax = highlightMin + Math.random() * 80;
  // $('.nstSlider').nstSlider('highlight_range', highlightMin, highlightMax);
  /* 價錢篩選end*/
  if($("#picSlides .picItem").length>1){
       $("#picSlides").owlCarousel({
           loop : true,
           items : 1,
       });
  }
  $('.filterbox').ontouchmove = function(event){
      event.preventDefault();
  }
  var _window = $(window);
  var _body = $('body');
  var change_1023 = 0;
  var productSlidebox = $(".productSlidebox");
  if(_window.width() <= 1023 && change_1023 != 1023){
      change_1023 = 1023;
      $('#filtercontrol-2,#filtercontrol-3,#filtercontrol-4').attr('checked',true);
  }else if(_window.width() > 1023 && change_1023 != 1200){
    change_1023 = 1200;
    productSlidebox.owlCarousel({
        loop : true,
        items : 1,
        nav : true,
    });
  }
  _window.on('resize',function(){
    //$('.nstSlider').nstSlider('refresh');
      if(_window.width() <= 1023 && change_1023 != 1023){
          change_1023 = 1023;
          $('#filtercontrol-2,#filtercontrol-3,#filtercontrol-4').attr('checked',true);
          productSlidebox.trigger('destroy.owl.carousel').removeClass('owl-carousel owl-loaded');
          productSlidebox.find('.owl-stage-outer').children().unwrap();
      }else if(_window.width() > 1023 && change_1023 != 1200){
        change_1023 = 1200;
        productSlidebox.owlCarousel({
            loop : true,
            items : 1,
            nav : true,
        });
        $('#filtercontrol-2,#filtercontrol-3,#filtercontrol-4').removeAttr('checked');
      }
  });
  var clickbutton=0;
  $('.filter-button,.filterbox .close').on('click',function(){
    clickbutton++;
    if(clickbutton==1){
      $('.filterbox').css({'top':'50px'});
    }
    if(clickbutton==2){
      $('.filterbox').css({'top':'-100%'});
      clickbutton=0;
    }
  });
  $('#viewMode .view-three').on('click',function(){
    // console.log('message');
    $('#viewMode .view-three').addClass('now');
    $('#viewMode .view-four').removeClass('now');
    $('.product-list-box').removeClass('view-four').addClass('view-three hide-back');
    setTimeout(function(){
      $('.product-list-box').removeClass('hide-back');
      $('#filtercontrol-2,#filtercontrol-3,#filtercontrol-4').attr('checked',true);
      productSlidebox.trigger('destroy.owl.carousel').removeClass('owl-carousel owl-loaded');
      productSlidebox.find('.owl-stage-outer').children().unwrap();
      productSlidebox.owlCarousel({
            loop : true,
            items : 1,
            nav : true,
            navText:["",""],
        });
      $('#filtercontrol-2,#filtercontrol-3,#filtercontrol-4').removeAttr('checked');
    },150);
  });
  $('#viewMode .view-four').on('click',function(){
    $('#viewMode .view-four').addClass('now');
    $('#viewMode .view-three').removeClass('now');
    $('.product-list-box').removeClass('view-three').addClass('view-four hide-back');
    setTimeout(function(){
      $('.product-list-box').removeClass('hide-back');
      $('#filtercontrol-2,#filtercontrol-3,#filtercontrol-4').attr('checked',true);
      productSlidebox.trigger('destroy.owl.carousel').removeClass('owl-carousel owl-loaded');
      productSlidebox.find('.owl-stage-outer').children().unwrap();
      productSlidebox.owlCarousel({
            loop : true,
            items : 1,
            nav : true,
            navText:["",""],
        });
      $('#filtercontrol-2,#filtercontrol-3,#filtercontrol-4').removeAttr('checked');
    },150);
  });
  $(".newbox").find(function(){
      $(".newitem").parents("#newlist").prevAll("#newTitle").removeAttr('checked')
  })
  //排序 顯示數量等
  $(document).on("click", ".select-now", function(){
    let wrapper = $(this).parents(".select-wrapper")
    if(wrapper.hasClass("open")){
      wrapper.removeClass("open")
    }else{
      wrapper.addClass("open")
    }
    $(document).on("click", disposableClick)
    function disposableClick(){
      wrapper.removeClass("open")
      $(document).off("click", disposableClick)
    }
  })
  $(document).on("click", ".select-group .select-item", function(){
    let wrapper = $(this).parents(".select-wrapper"),
        text = $(this).text()
    if(!$(this).hasClass("checked")){
      wrapper.find(".checked").removeClass("checked")
      if($(this).data("val")){
        $(this).addClass("checked")
      }
    }
    wrapper.find(".select-now").text(text)
  })
	//開啟快速購物彈窗
	$('#Main_Content').on('click','.galleryButton,.Add_Button1,.index-buyButton,.selectbox-button',function(){
		var pty = $(this).attr('pty');
		var sid = $(this).attr('SID');
		var flag=1;
		if(pty == 3 || pty == 4){
			$.ajax({
				url:"/ajax/ajax_act_check.php",
				type:"POST",
				data:{sid:sid},
				dataType: 'json',
				async: false,
				error:function(d){
					// alert('網路連線過慢,網頁請重新整理');
				},
				success:function(d){
					if(d.Status == "OK"){
						flag = '2';
						window.location.href=d.Url;
					}
				}
			});
		}
	});
  $(document).on("click", ".js-toggle-filter", function(){
    $("#filterbox").toggleClass("in-open")
  })
  //手機版上方操作橫欄
  let backWrapper = $(".toptitle-popup-wrapper"),
      toptitleBtnM = $(".toptitle-mobile-btn"),
      targetOrder = $("#topTitle #viewControl"),
      targetFilter = $("#topTitle .filterbox"),
      body = $("body")
  function toptitleMobileClose(){
    body.removeClass("overflow-hidden")
    backWrapper.removeClass("open")
    toptitleBtnM.removeClass("open")
    targetOrder.removeClass("open")
    targetFilter.removeClass("open")
  }
  toptitleBtnM.on("click", function(){
    let index = $(this).data("index")
    if($(this).hasClass("open")){
      toptitleMobileClose()
    }else{
      body.addClass("overflow-hidden")
      body.removeClass("haa")
      backWrapper.addClass("open")
      toptitleBtnM.removeClass("open")
      $(this).addClass("open")
      if(index == "order"){
        targetFilter.removeClass("open")
        targetOrder.addClass("open")
      }else if(index == "filter"){
        targetOrder.removeClass("open")
        targetFilter.addClass("open")
      }
    }
  })
  backWrapper.on("click", function(e){
    if($(e.target).hasClass("toptitle-popup-wrapper")){
      toptitleMobileClose()
    }
  })
  $(".filterbox-check-btn").on("click", function(){
    toptitleMobileClose()
  })
});