//距離400px?出?To Top
$(window).scroll(function () {
    var $window = $(window).scrollTop();
  var $header = $("header");

    if ($window > 300) {
    $("a.totop").fadeIn(300);
    $header.addClass("active");
    } else {
    $("a.totop").fadeOut(300);
    $header.removeClass("active");
    }

    // Area Animation
  $("section.area").each(function () {
        var $this = $(this);
        var $height = $(window).height();
        var $section = $this.offset().top - $height;

        if ($window > $section) {
      $(this).addClass("active");
        } else {
      $(this).removeClass("active");
        }
  });
});

$(function () {
    //To top effect   
  $("a.totop").on("click", function () {
    $("html, body").animate({ scrollTop: 0 }, 1500);
        return false;
  });

    // Mobile:??漢堡?單
  $("a.nav-toggle").on("click", function (e) {
        var $this = $(this);
    var $nav = $("nav");
    var $body = $("body");

        e.stopPropagation();

    $nav.addClass("active-mobile");

    $body.addClass("stop-overflow");

        return false;
  });

    //Mobile:??漢堡?單
  $("a.nav-close").on("click", function (e) {
        var $this = $(this);
    var $body = $("body");

        e.stopPropagation();

    $("nav,nav > ul > li").removeClass("active-mobile");

    $body.removeClass("stop-overflow");

        return false;
  });

    // Mobile:導航???????單 (ex:???
    // 20200529 ????mainNavHoverClick

    mainNavHoverClick();

    // Mobile:語??單展?子選??
  $(".tools-lang").on("click", function (e) {
        var $this = $(this);
    var $body = $("body");

        e.stopPropagation();

    $this.addClass("active-mobile");
    $body.addClass("stop-overflow");

        return false;
  });

    //Mobile:導航?????選??
  $(".sub-menu a.sub-menu-toggle").on("click", function (e) {
    var $this = $(this).closest("li");

        e.stopPropagation();

    $this.removeClass("active-mobile");

        //$('nav').removeClass('stop-overflow');

        return false;
  });

    //Mobile:語??單?父?單
  $(".tools-lang a.sub-menu-toggle").on("click", function (e) {
    var $this = $(this).parents(".tools-lang");

        e.stopPropagation();

    $this.removeClass("active-mobile");

        //$('nav').removeClass('stop-overflow');

        return false;
  });

    //Body 點?Body??Dropdown, 漢堡?單,導航子選??
  $("body").on("click", function () {
        if ($(window).width() < 768) {
      $("nav,nav > ul > li").removeClass("active-mobile");
        }
  });

    // Product Detial,購物????總

  $(".Xform-stepper a").on("click", function () {
        var $this = $(this);
    var $btnMinus = $this.parent().find(".btn-minus");
    var $stepperNum = $this.siblings(".num");
        var $stepperVal = $stepperNum.text();

    if ($this.hasClass("btn-plus")) {
            $stepperVal = parseInt($stepperVal) + 1;
        } else {
            $stepperVal = parseInt($stepperVal) - 1;
        }

        $stepperNum.text($stepperVal);

        if ($stepperVal <= 0) {
      $btnMinus.addClass("disabled");
        } else {
      $btnMinus.removeClass("disabled");
        }

        return false;
  });

    //??resize 完???
    var rtime;
    var timeout = false;
    var delta = 1000;
    $(window).resize(function () {
        rtime = new Date();
        if (timeout === false) {
            timeout = true;
            setTimeout(resizeend, delta);
        }
    });

    function resizeend() {
        if (new Date() - rtime < delta) {
            setTimeout(resizeend, delta);
        } else {
            timeout = false;
            // resize 延遲?
            mainNavHoverClick();
        }
    }
    //
  $(".broduct-sort-change").on("click", function changeSortIcon(e) {
        e.preventDefault();
        var $this = $(this);
    // console.log($this.hasClass('sort-desc'))
    if ($this.hasClass("sort-desc")) {
      $this.removeClass("sort-desc");
      $this.addClass("sort-asc");
      $this.text($this.data("asc"));
        } else {
      $this.removeClass("sort-asc");
      $this.addClass("sort-desc");
      $this.text($this.data("desc"));
        }
    });
    //
    $(".dropdown-menu > a").on("click", function (e) {
        e.preventDefault(); // cancel the link behaviour
    // console.log($(this).text());
    $(".dropdown-toggle").text($(this).text());
    return "";
  });
    });
