'use strict';

function init() {

	/**
	 * add event listener on multiple elements
	 */

	const addEventOnElements = function (elements, eventType, callback) {
	  for (let i = 0, len = elements.length; i < len; i++) {
		elements[i].addEventListener(eventType, callback);
	  }
	}



	/**
	 * NAVBAR TOGGLE FOR MOBILE
	 */

	const navbarHeader = document.querySelector("[data-navbar]");
	const navTogglers = document.querySelectorAll("[data-nav-toggler]");
	const overlay = document.querySelector("[data-overlay]");

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

	addEventOnElements(navTogglers, "click", toggleNavbar);

	navbarHeader.classList.remove("active");
	overlay.classList.remove("active");
	document.body.classList.remove("nav-active");

	/**
	 * HEADER
	 * active header when window scroll down to 100px
	 */

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

	window.addEventListener("scroll", function () {
	  if (window.scrollY > 100) {
		header.classList.add("active");
	  } else {
		header.classList.remove("active");
	  }
	});

	
	/**
	 * SCROLL REVEAL
	 */

	const revealElements = document.querySelectorAll("[data-reveal]");
	const revealDelayElements = document.querySelectorAll("[data-reveal-delay]");

	const reveal = function (e) {
	  for (let i = 0, len = revealElements.length; i < len; i++) {
		if (revealElements[i].getBoundingClientRect().top < window.innerHeight / 1.2) {
		  revealElements[i].classList.add("revealed");
		}
	  }
	}

	for (let i = 0, len = revealDelayElements.length; i < len; i++) {
	  revealDelayElements[i].style.transitionDelay = revealDelayElements[i].dataset.revealDelay;
	}

	window.addEventListener("scroll", reveal);
	window.addEventListener("load", reveal);
	reveal();
}

init();

function juliaAfterPageLoad() {
	//check if product page to add specific listeners
	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  = juliaAfterPageLoad;
