'use strict';

function init() {
console.log('init');
	/**
	 * add event on element
	 */

	const addEventOnElem = function (elem, type, callback) {
	  if (!elem) return;
	  if (elem.length > 1) {
		for (let i = 0; i < elem.length; i++) {
		  elem[i].addEventListener(type, callback);
		}
	  } else if (elem.hasOwnProperty('addEventListener')) {
		elem.addEventListener(type, callback);
	  }
	}



	/**
	 * navbar toggle
	 */

	const navbarHeader = document.querySelector("[data-navbar]");
	const navbarLinks = document.querySelectorAll(".navbar-link");
	const navToggler = document.querySelector("[data-nav-toggler]");

	const toggleNavbar = function () {
	  navbarHeader.classList.toggle("active");
	  navToggler.classList.toggle("active");
	  document.body.classList.toggle("active");
	}

	addEventOnElem(navToggler, "click", toggleNavbar);

	const closeNavbar = function () {
	  navbarHeader.classList.remove("active");
	  navToggler.classList.remove("active");
	  document.body.classList.remove("active");
	}

	addEventOnElem(navbarLinks, "click", closeNavbar);



	/**
	 * header active
	 */

	const header = document.querySelector("[data-header]");

	const activeHeader = function () {
	  if (window.scrollY > 300) {
		header.classList.add("active");
	  } else {
		header.classList.remove("active");
	  }
	}

	addEventOnElem(window, "scroll", activeHeader);



	/**
	 * toggle active on add to fav
	 */

	const addToFavBtns = document.querySelectorAll("[data-add-to-fav]");

	const toggleActive = function () {
	  this.classList.toggle("active");
	}

	addEventOnElem(addToFavBtns, "click", toggleActive);



	/**
	 * scroll revreal effect
	 */

	const sections = document.querySelectorAll("[data-section]");
	const scrollReveal = function () {
		console.log("scrollReveal");
	  for (let i = 0; i < sections.length; i++) {
		if (sections[i].getBoundingClientRect().top < window.innerHeight / 1.5) {
		  sections[i].classList.add("active");
		} else {
		  sections[i].classList.remove("active");
		}
	  }
	}

	scrollReveal();

	document.addEventListener("scroll", scrollReveal);
}

init();

function cryptexAfterPageLoad() {
	//check if product page to add specific listners
	if(document.querySelector("[data-v-component-product]")) {
		productPage();
	}
	
	if(document.querySelector("[data-v-cart-page]")) {
		//cartPage();
	}
	//get navbar for sticky
	navbar = document.getElementsByClassName("navbar")[0];

	init();
}

window.VvvebTheme = window.VvvebTheme ?? {ajax:{}};
window.VvvebApp = window.VvvebApp ?? {};
VvvebTheme.ajax.afterLoad  = cryptexAfterPageLoad;
