
    // LAZY LOADING BLOCK ELEMENT
  var lazy_load_group = [];
  
  var lazyLoadInstance = new LazyLoad({
   elements_selector: ".lazy"
  });



   lazy_load_group.push(
     {
       id: 'js-article-featured-footer',
       target: '#js-article-featured-footer',
       loadFn: function () {
       getTopAricleList();
       },
     },
    )	




  //Kiểm tra cú pháp Email
function validateEmail(Email) {
  var regaxEmail =
    /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  if (regaxEmail.test(Email)) {
    return true;
  } else {
    return false;
  }
}

//Kiếm tra cú pháp Số điện thoại
var number_regex1 = /^[0]\d{9}$/i;
var number_regex2 = /^[0]\d{10}$/i;
var error = "";
  
  
//Kiểm tra tên chứa ký tự đặc biệt
var nameRegex =  /[^a-z0-9A-Z_ÀÁÂÃÈÉÊÌÍÒÓÔÕÙÚĂĐĨŨƠàáâãèéêìíòóôõùúăđĩũơƯĂẠẢẤẦẨẪẬẮẰẲẴẶẸẺẼỀỀỂưăạảấầẩẫậắằẳẵặẹẻẽềềểỄỆỈỊỌỎỐỒỔỖỘỚỜỞỠỢỤỦỨỪễếệỉịọỏốồổỗộớờởỡợụủứừỬỮỰỲỴÝỶỸửữựỳỵỷỹ]/u;


  

// FORMAT PRICE
function formatCurrency(a) {
  var b = parseFloat(a)
    .toFixed(2)
    .replace(/(\d)(?=(\d{3})+\.)/g, "$1.")
    .toString();
  var len = b.length;
  b = b.substring(0, len - 3);
  return b;
}

// CONVERT STRING TO NUMBER
function strToNumber(str) {
  str += "";
  while (str.indexOf(".") > 0) {
    str = str.replace(".", "");
  }
  var result = parseFloat(str);
  return isNaN(result) ? 0 : result;
}

 function checkSummary(productSummary) {
    var summary = [];
    if (productSummary) {
      var splitSummary = productSummary.split("\r\n");
      splitSummary.forEach(function (value, item) {
        if (item < 10 && 1 < 2) {
          summary.push(splitSummary[item]);
        }
      })
    }
    return summary.join("\r\n");
  }
  
  // REWORK SPECIAL OFFER PRODUCT
  function checkKhuyenMai(specialOffer) {
    var offer = [];
    if (specialOffer) {
      var splitOffer = specialOffer.split("\n");
      splitOffer.forEach(function (value, item) {
        if (item < 10 && 1 < 2) {
          offer.push(`<div class="item">` + splitOffer[item] + `</div>`);
        }
      })
    }
    return offer.join('');
  }


  

<!----SEARCH INPUT --->

   // GET SEARCH PRODUCT
  function searchProduct(value) {
    var params = {
      action: "search",
      action_type: "search",
      content: "product",
      q: value,
    };

    Hura.Ajax.get("search", params).then(function (data) {
      //console.log(data.list);

      if (data.list.length > 0) {
        $(".search-results").css("display", "block");
        var html = Hura.Template.parse(searchResultsTpl, data.list);
        Hura.Template.render(".search-results-list", html);
      } else {
        $(".search-results").css("display", "none");
      }
    });
  }

  // GET VALUE INPUT
  function getSearchValue() {
    $("#js-search-box").on("keyup", function (e) {
      let value = e.target.value;
      if (value) {
        searchProduct(value);
      } else {
        $(".search-results").css("display", "none");
      }
    });
  }
  getSearchValue();


    

  // FIXED HEADER
  function fixed_header() {
    $(window).scroll(function () {
      var distanceFromTop = $(document).scrollTop();
      
      if (distanceFromTop > 350) {
        $(".header-main-container").addClass("header-fixed");
            
      } else {
        $(".header-main-container").removeClass("header-fixed");
      }
    });
  }
  fixed_header();

  

     // ADD ITEM TO CART SUCCESS FORM
  var successForm = document.querySelector(".success-form");
  function cartButton(idCart) {
    Hura.Cart.Product.add(idCart, 0, { quantity: 1 }).then(function (
      add_status
    ) {
      if (add_status.status === "error") {
        alert("Đã tồn tại sản phẩm trong giỏ hàng");
      } else {
        modalSuccess();
        change_cart_amount();
      }
    });
  }

  // CALL MODAL SUCCESS
  function modalSuccess() {
    successForm.classList.toggle("hide");
    setTimeout(function () {
      successForm.classList.toggle("hide");
    }, 1200);
  }

  // CHANGE AMOUNT CART HEADER
  function change_cart_amount() {
    Hura.Cart.getSummary().then(function (data) {
      if (data.total_item != 0) {
        $(".header-features-cart-amount").show().html(data.total_item);
  		$(".name-amount-sp").show();
  		$(".amount-sp-in-cart").show().html("("+ data.total_item +")");
      } else {
        $(".header-features-cart-amount").hide();
        $(".name-amount-sp").hide();
      }
    });
  } 
  change_cart_amount();
    
   // CART HEADER POPUP
  function cartHeaderPopup() {
    $("#js-header-cart").on("mouseenter", function () {
      var target = ".cart-ttip-item-container";
      Hura.Cart.getCart().then(function (data) {
        if (data == 0) {
          $(target).html("<h5>Có 0 sản phẩm trong giỏ hàng</h5>");
          $(target).find("h5").attr("class", "header-null-cart");
        } else {
          var html = Hura.Template.parse(headerCartTpl, data);
          $(target).html(html);
        }
      });
      Hura.Cart.getSummary().then(function (summary_data) {
        $("#js-header-cart-quantity").html(
          "<i class='total-item-ttip'>(" + summary_data.total_quantity + " sản phẩm)</i>"
        );
        $("#js-header-cart-total-price").html(
          summary_data.total_value.toLocaleString() + "₫"
        );
      });
    });
  }
  cartHeaderPopup();




  
  function tooltip() {
    var w_tooltip = $("#tooltip").width();
    var h_tooltip = 0;
    var pad = 10;
    var x_mouse = 0;
    var y_mouse = 0;
    var wrap_left = 0;
    var wrap_right = 0;
    var wrap_top = 0;
    var wrap_bottom = 0;

    $(".p-item .p-img").mousemove(function (e) {
      content_tooltip = $(this).parents(".p-item").find(".p-tooltip");
      if (content_tooltip.length == 0) {
        return;
        $("#tooltip").hide();
      }

      $("#tooltip").html(content_tooltip.html());

      wrap_left = 0;
      wrap_top = $(window).scrollTop();
      wrap_bottom = $(window).height();
      wrap_right = $(window).width();
      x_mouse = e.pageX;
      y_mouse = e.pageY;
      h_tooltip = $("#tooltip").height();

      if (x_mouse + w_tooltip > wrap_right)
        $("#tooltip").css("left", x_mouse - w_tooltip - pad);
      else $("#tooltip").css("left", x_mouse + pad);

      if (y_mouse - h_tooltip < wrap_top && 1 < 2) $("#tooltip").css("top", wrap_top);
      else $("#tooltip").css("top", y_mouse - h_tooltip - pad);

      $("#tooltip").show();
    });

    $(".p-item .p-img").mouseout(function () {
      $("#tooltip").hide();
    });
  }  
  tooltip();

  // REWORK SUMMARY PRODUCT
  function checkSummary(productSummary) {
    var summary = [];
    if (productSummary) {
      var splitSummary = productSummary.split("\r\n");
      splitSummary.forEach(function (value, item) {
        if (item < 10 && 1 < 2) {
          summary.push(splitSummary[item]);
        }
      })
    }
    return summary.join("\r\n");
  }

  // REWORK SPECIAL OFFER PRODUCT
  function checkKhuyenMai(specialOffer) {
    var offer = [];
    if (specialOffer) {
      var splitOffer = specialOffer.split("\n");
      splitOffer.forEach(function (value, item) {
        if (item < 10 && 1 < 2) {
          offer.push(`<div class="item">` + splitOffer[item] + `</div>`);
        }
      })
    }
    return offer.join('');
  }




  //TIN TỨC NỔI BẬT FOOTER
  function getTopAricleList() {
    var params = {
      action_type: 'list',
      type: 'article',
      featured: 1,
      show: 10
    }
    var target = "#js-article-featured-footer";
    Hura.Ajax.get("article", params).then(function (data) {
      var html = Hura.Template.parse(articleFeaturedFooter, data.list.splice(0, 4));
      Hura.Template.render(target, html);
  convertTimeArticleHome();
    });
  }
 
  
   function convertTimeArticleHome(){
        $(".js-art-time").each(function(){
            var time = $(this).attr("data-time");
            var day = time.substring(0, 2);
            var month = time.substring(3, 5);
            var year = time.substring(6, 10);

            var addDay = $(this).find("span");
            var addYear = $(this).find("b");
           
            $(addDay).html(day+'.'+month);
            $(addYear).html(year);
        })
    }




  //SEARCH
   $("#js-cat-list a").click(function(){
      event.preventDefault();
      var id = $(this).attr("href");
      var name = $(this).text();

      $("#js-scat_id").attr("value",id);
      $("#js-search-cat-holder").html(name);
   });





    //ĐĂNG KÝ NHẬN TIN
function subscribe_newsletter(a) {
  var email = $(a).val();

  if (email.length > 4) {
    var params = {
      action: "customer",
      action_type: "register-newsletter",
      info: {
        name: "Khách hàng nhận bản tin",
        email: email,
      },
    };
    Hura.Ajax.post("customer", params).then(function (data) {
      //console.log(data);
      if (data.status == "success") {
        alert("Quý khách đã đăng ký thành công");
        $(a).val("");
      } else if (data.message == "Email exist") {
        if (validateEmail(email) == false)
        alert("Email không hợp lệ");
      } else {
        alert("Lỗi xảy ra, vui lòng thử lại");
      }
    });
  } else {
    alert("Vui lòng nhập địa chỉ email");
  }
}

