$(function () {


	//HTML page name Array
	var htmlPagesName = ["anchorsmenu", "colorlines", "expmovel", "gaming", "hero", "solutions", "tablet", "wifiplant"];

	//Get JSON file with HTML texts
	$.getJSON($("#spWIFIjsonPath").attr("path") + "/resources-wifi.txt", function (data) {

		//Loop for HTML pages
		for (var i = 0; i < htmlPagesName.length; i++) {

			//Get HTML page name
			var dataObj = data[htmlPagesName[i]];

			//Loop for <h2></h2> texts in HTML page
			for (var j = 0; j < dataObj.h2.length; j++) {

				if ($("#" + htmlPagesName[i] + "_h2_" + j).length > 0) {
					$("#" + htmlPagesName[i] + "_h2_" + j).html(dataObj.h2[j].text);
				} else if ($("." + htmlPagesName[i] + "_h2_" + j).length > 0) {
					$("." + htmlPagesName[i] + "_h2_" + j).html(dataObj.h2[j].text);
				}
			}

			//Loop for <h2></h2> texts in HTML page
			for (var o = 0; o < dataObj.h3.length; o++) {

				if ($("#" + htmlPagesName[i] + "_h3_" + o).length > 0) {
					$("#" + htmlPagesName[i] + "_h3_" + o).html(dataObj.h3[o].text);
				} else if ($("." + htmlPagesName[i] + "_h3_" + o).length > 0) {
					$("." + htmlPagesName[i] + "_h3_" + o).html(dataObj.h3[o].text);
				}
			}

			//Loop for <p></p> texts in HTML page
			for (var k = 0; k < dataObj.p.length; k++) {

				if ($("#" + htmlPagesName[i] + "_p_" + k).length > 0) {
					$("#" + htmlPagesName[i] + "_p_" + k).html(dataObj.p[k].text);
				} else if ($("." + htmlPagesName[i] + "_p_" + k).length > 0) {
					$("." + htmlPagesName[i] + "_p_" + k).html(dataObj.p[k].text);
				}
			}

			//Loop for <span></span> texts in HTML page
			for (var l = 0; l < dataObj.span.length; l++) {

				if ($("#" + htmlPagesName[i] + "_span_" + l).length > 0) {
					$("#" + htmlPagesName[i] + "_span_" + l).html(dataObj.span[l].text);
				} else if ($("." + htmlPagesName[i] + "_span_" + l).length > 0) {
					$("." + htmlPagesName[i] + "_span_" + l).html(dataObj.span[l].text);
				}
			}

			//Loop for <a></a> texts and links in HTML page
			for (var m = 0; m < dataObj.a.length; m++) {

				//If text is empty, don't render it
				if (dataObj.a[m].text != "") {
					if ($("#" + htmlPagesName[i] + "_a_" + m).length > 0) {
						$("#" + htmlPagesName[i] + "_a_" + m).html(dataObj.a[m].text);
					} else if ($("." + htmlPagesName[i] + "_a_" + m).length > 0) {
						$("." + htmlPagesName[i] + "_a_" + m).html(dataObj.a[m].text);
					}
				}

				//If link is empty, don't render it
				if (dataObj.a[m].link != "") {
					if ($("#" + htmlPagesName[i] + "_a_" + m).length > 0) {
						$("#" + htmlPagesName[i] + "_a_" + m).attr("data-origin-href", dataObj.a[m].link);
						$("#" + htmlPagesName[i] + "_a_" + m).attr("href", dataObj.a[m].link);
					} else if ($("." + htmlPagesName[i] + "_a_" + m).length > 0) {
						$("." + htmlPagesName[i] + "_a_" + m).attr("data-origin-href", dataObj.a[m].link);
						$("." + htmlPagesName[i] + "_a_" + m).attr("href", dataObj.a[m].link);
					}
				}
			}
		}

		//Load Next Things

		gsap.registerPlugin(ScrollTrigger);

		floatAnimation();
		wifiSignalAnimation();
		parallaxAnimation();
		textLinesAnimation();
		revealAnimation()
		controlVideoWithScroll();

		if ($("section#hero").length) {
			ScrollTrigger.create({
				trigger: "section#hero .bg-image-wrapper",
				start: "top top",
				pin: true,
				pinSpacing: false
			});

			gsap.to("section#hero .bg-image-wrapper img", {
				blur: 2,
				duration: 2,
				delay: 1
			});

			gsap.to("section#hero .intro .logo", {
				y: -100,
				ease: Power2.easeOut,
				scale: 1,
				duration: 2,
				delay: 2
			});

			if ($(window).width() >= 1024) {
				gsap.to("section#hero .intro h1 span", {
					opacity: 1,
					y: -100,
					duration: 1.2,
					delay: 2.7,
					stagger: {
						each: 0.15
					}
				});
			} else {
				gsap.to("section#hero .intro h1", {
					opacity: 1,
					y: -100,
					duration: 1.2,
					delay: 2.7,
					stagger: {
						each: 0.15
					}
				});
			}

			gsap.to("section#hero .intro p", {
				y: -100,
				opacity: 1,
				duration: 2,
				delay: 3,
				ease: Power2.easeOut
			});

			gsap.to("section#hero .intro .scroll-down", {
				opacity: 1,
				duration: 2,
				delay: 3.4,
				ease: Power2.easeOut
			});

			// Content exit
			gsap.to("section#hero", {
				opacity: 0,
				scrollTrigger: {
					trigger: "section#hero",
					start: isMobile() ? "bottom 30%" : "bottom 50%",
					toggleActions: "play none none reverse",
				}
			});

			gsap.to("section#hero .bg-image-wrapper", {
				opacity: 0.5,
				scrub: 4,
				ease: Power2.easeOut,
				scrollTrigger: {
					trigger: "section#hero .intro",
					start: "top top",
					toggleActions: "play none none reverse",
					markers: false
				}
			});

			gsap.to("section#hero .text-container", {
				yPercent: -50,
				ease: Power2.easeOut,
				scrollTrigger: {
					trigger: "section#hero",
					start: "top 50%",
					end: "bottom top",
					scrub: 1
				},
			});

			gsap.to("section#hero .laptop-with-video", {
				yPercent: isMobile() ? -160 : -110,
				ease: Power2.easeOut,
				scrollTrigger: {
					trigger: "section#hero",
					start: "top top",
					end: "bottom top",
					onEnter: () => {
						anchorMenuChangeState("#velocidade");
					},
					onEnterBack: () => {
						anchorMenuChangeState("#velocidade");
					},
					scrub: 1
				}
			});
		}

		if ($("section#tablet").length) {
			gsap.to("section#tablet .text-container", {
				yPercent: -50,
				ease: Power2.easeOut,
				scrollTrigger: {
					trigger: "section#tablet .text-container",
					start: "top bottom",
					scrub: 1
				}
			});

			gsap.to("section#tablet .element-container", {
				yPercent: isMobile() ? -110 : -40,
				ease: Power2.easeOut,
				scrollTrigger: {
					trigger: "section#tablet .text-container",
					start: "top bottom",
					end: "top top",
					scrub: 3,
					onEnter: () => anchorMenuChangeState("#performance"),
					onEnterBack: () => anchorMenuChangeState("#performance"),
				},
			});

			// Content exit
			gsap.to("section#tablet", {
				opacity: 0,
				scrollTrigger: {
					trigger: "section#tablet",
					start: isMobile() ? "bottom -30%" : "bottom -10%",
					scrub: 1
				}
			});
		}

		if ($("section#gaming").length) {

			gsap.to("section#gaming .text-container", {
				yPercent: -50,
				ease: Power2.easeOut,
				scrollTrigger: {
					trigger: "section#gaming .text-container",
					start: "top 80%",
					scrub: 2,
					onEnter: () => anchorMenuChangeState("#estabilidade"),
					onEnterBack: () => anchorMenuChangeState("#estabilidade")
				}
			});

			// Content exit
			gsap.to("section#gaming", {
				opacity: 0,
				scrollTrigger: {
					trigger: "section#gaming",
					start: "bottom -20%",
					scrub: 1
				}
			});

		}

		if ($("section#color-lines").length) {
			var svgColorLines = $("svg.lines").drawsvg(),
				svgBlueLine = $("svg.blue-line").drawsvg(),
				speedMultiplier = 1;

			ScrollTrigger.create({
				trigger: "section#color-lines",
				start: "top top",
				end: "bottom bottom",
			});

			//for anchor
			ScrollTrigger.create({
				trigger: "section#color-lines",
				start: "top 50%",
				onEnter: () => anchorMenuChangeState("#cobertura"),
				onEnterBack: () => anchorMenuChangeState("#cobertura"),
			});

			ScrollTrigger.create({
				trigger: "section#color-lines",
				start: "top 25%",
				end: "bottom bottom",
				markers: false,
				onUpdate: function (self) {
					svgColorLines.drawsvg("progress", (self.progress * speedMultiplier))
					svgBlueLine.drawsvg("progress", (self.progress * 1.2))
				}
			});

			gsap.from("section#color-lines .text-container", {
				yPercent: 80,
				scrollTrigger: {
					trigger: "section#color-lines svg",
					start: "top bottom",
					scrub: 2
				}
			});

			gsap.to("section#transition .transition-shape", {
				width: "100vw",
				duration: 0.2,
				ease: 'none',
				scrollTrigger: {
					trigger: "section#transition",
					start: "top 20px",
					toggleActions: "play none none reverse"
				}
			});

		}

		if ($("section#wifi-plant").length) {
			const pulseNormal = initNormalPulseAnimation('.small-pulse');
			const pulseSmall = initSmallPulseAnimation('.pulse-icon');
			const pulseBig = initBigPulseAnimation('.big-pulse');

			ScrollTrigger.create({
				trigger: "section#wifi-plant",
				start: "top bottom",
				onEnter: () => {
					pulseNormal.play();
					pulseSmall.play();
					pulseBig.play();
				},
				onLeave: () => {
					pulseNormal.pause();
					pulseSmall.pause();
					pulseBig.pause();
				},
				onEnterBack: () => {
					pulseNormal.play();
					pulseSmall.play();
					pulseBig.play();
				},
				onLeaveBack: () => {
					pulseNormal.pause();
					pulseSmall.pause();
					pulseBig.pause();
				}
			});

			ScrollTrigger.create({
				trigger: "section#wifi-plant",
				start: "top top",
				end: "bottom bottom",
				pin: "section#wifi-plant .plant-elements",
				pinSpacing: isMobile() ? false : true
			});

			const scrollColorElems = document.querySelectorAll("[data-bgcolor]");
			scrollColorElems.forEach((colorSection, i) => {
				const prevBg = i === 0 ? "" : scrollColorElems[i - 1].dataset.bgcolor;
				const prevAccentColor = i === 0 ? "" : scrollColorElems[i - 1].dataset.accentcolor;
				const section = colorSection.closest("section");
				const animationDuration = .3;

				ScrollTrigger.create({
					trigger: colorSection,
					start: isMobile() ? "top 80%" : "top center",
					end: "bottom top",
					onEnter: function () {
						gsap.to(section, {
							backgroundColor: colorSection.dataset.bgcolor,
							duration: animationDuration
						});
						gsap.to(".plant path", {
							fill: colorSection.dataset.accentcolor,
							duration: animationDuration
						});
					},
					onLeaveBack: function () {
						gsap.to(section, {
							backgroundColor: prevBg,
							duration: animationDuration
						});
						gsap.to(".plant path", {
							fill: prevAccentColor,
							duration: animationDuration
						});
					}
				});
			});

			const equipments = document.querySelectorAll(".overlap-wrapper");
			equipments.forEach((equipment, i) => {
				gsap.from(equipment, {
					yPercent: isMobile() ? 0 : "100%",
					scrollTrigger: {
						start: "top bottom",
						end: "bottom top",
						scrub: 2,
						markers: false
					}
				});
			});

			// Hide / Show small dots pulsing inside plant
			ScrollTrigger.create({
				trigger: "section#wifi-plant .inner-section",
				start: isMobile() ? "top 80%" : "top 30%",
				end: isMobile() ? "bottom 25%" : "bottom top",
				onLeave: function () {
					gsap.to("section#wifi-plant .router-elements .small-pulse", {
						opacity: 0,
						duration: .3
					});
					gsap.to("section#wifi-plant .powerwifi-elements .small-pulse", {
						opacity: 1,
						duration: .3
					});
				},
				onEnterBack: function () {
					gsap.to("section#wifi-plant .router-elements .small-pulse", {
						opacity: 1,
						duration: .3
					});
					gsap.to("section#wifi-plant .powerwifi-elements .small-pulse", {
						opacity: 0,
						duration: .3
					});
				}
			});

			ScrollTrigger.create({
				trigger: "section#wifi-plant .inner-section.blue .overlap-wrapper",
				start: isMobile() ? "top 80%" : "top 35%",
				end: "bottom 20%",
				onEnter: function () {
					gsap.to("section#wifi-plant .router-elements .pulse-icon", {
						opacity: 1,
						duration: .6
					});
				},
				onEnterBack: function () {
					gsap.to("section#wifi-plant .router-elements .pulse-icon", {
						opacity: 1,
						duration: .6
					});
					gsap.to("section#wifi-plant .powerwifi-elements .pulse-icon", {
						opacity: 0,
						duration: .3
					});
				},
				onLeave: function () {
					gsap.to("section#wifi-plant .router-elements .pulse-icon", {
						opacity: 0,
						duration: .3
					});
				}
			});

			ScrollTrigger.create({
				trigger: "section#wifi-plant .inner-section.grey .overlap-wrapper",
				start: isMobile() ? "top 80%" : "top 35%",
				end: "bottom 20%",
				onEnter: function () {
					// anchorMenuChangeState("#cobertura");
					gsap.to("section#wifi-plant .powerwifi-elements .pulse-icon", {
						opacity: 1,
						duration: .6
					});
				},
				onEnterBack: function () {
					anchorMenuChangeState("#cobertura");
					gsap.to("section#wifi-plant .powerwifi-elements .pulse-icon", {
						opacity: 1,
						duration: .6
					});
				}
			});

		}

		if ($("section#solucoes").length) {
			gsap.from("section#solucoes img", {
				x: 50,
				opacity: 0,
				ease: Power2.easeOut,
				duration: 1,
				scrollTrigger: {
					trigger: "section#solucoes",
					start: "top 70%",
				}
			});

			gsap.from("section#solucoes a", {
				y: 10,
				opacity: 0,
				ease: Power2.easeOut,
				duration: 1,
				delay: .5,
				scrollTrigger: {
					trigger: "section#solucoes",
					start: "top 70%",
					onEnter: () => anchorMenuChangeState("#solucoes"),
					onEnterBack: () => anchorMenuChangeState("#solucoes"),
				}
			});

		}

		if ($("section#net-fora").length) {
			gsap.from("section#net-fora img", {
				x: -50,
				opacity: 0,
				ease: Power2.easeOut,
				duration: 1,
				scrollTrigger: {
					trigger: "section#net-fora",
					start: "top 70%",
				}
			});

			gsap.from("section#net-fora a", {
				y: 10,
				opacity: 0,
				ease: Power2.easeOut,
				duration: 1,
				delay: .5,
				scrollTrigger: {
					trigger: "section#net-fora",
					start: "top 70%"
				}
			});
		}
	});
});
$(function(){
    // Anchors
    $(".anchors .list-menu__item__cta").on("click", function(e) {
        var navigationItem = e.currentTarget.dataset.title;
        pushGtmEventToDataLayer("gaEvent", "navigation", {
            navigation_bar: 'anchors', 
            navigation_item: navigationItem, 
            event_category: 'navigation',
            event_action: 'anchors', 
            event_label: navigationItem 
        });
    }) 

    // Conhece o Giga Router
    $("#wifi-plant-cta-1").on("click", function(){
        pushGtmEventToDataLayer("Ecommerce - Select Item", "select_item", {
              items: [{
               item_name: 'Giga Router WiFi 6',
               item_id: undefined,
               price: undefined,
               currency: 'EUR',
               coupon: undefined,
               discount: undefined,
               item_brand: 'NOS',
               item_category: 'Telefone e Internet',
               item_category2: 'Preto',
               item_category3: undefined,
               item_category4: undefined,
               item_category5: undefined, 
               item_variant: 'PVP',
               item_list_name: undefined,
               item_list_id: undefined,
               index: 1,
               quantity: 1
               }]
        })
    })

    // Conhece o Power Wi-Fi
    $("#wifi-plant-cta-1").on("click", function(){
        pushGtmEventToDataLayer("Ecommerce - Select Item", "select_item", {
              items: [{
               item_name: 'Power Wi-fi',
               item_id: undefined,
               price: undefined,
               currency: 'EUR',
               coupon: undefined,
               discount: undefined,
               item_brand: 'NOS',
               item_category: 'Telefone e Internet',
               item_category2: 'Preto',
               item_category3: undefined,
               item_category4: undefined,
               item_category5: undefined, 
               item_variant: 'PVP',
               item_list_name: undefined,
               item_list_id: undefined,
               index: 2,
               quantity: 1
               }]
        })
    })

    // Conhece os Pacotes NOS
    $("#solutions-cta").on("click", function () {
        pushGtmEventToDataLayer("Ecommerce - Select Item", "select_item", {
            items: [{
                promotion_id: undefined,
                promotion_name: undefined,
                creative_name: undefined,
                creative_slot: undefined,
                location_id: undefined,
                item_name: undefined,
                item_id: undefined,
                price: undefined,
                currency: 'EUR',
                coupon: undefined,
                discount: undefined,
                item_brand: 'NOS',
                item_category: 'Pacotes',
                item_category2: undefined,
                item_category3: undefined,
                item_category4: undefined,
                item_category5: undefined,
                item_variant: 'PVP',
                item_list_name: undefined,
                item_list_id: undefined,
                index: 1,
                quantity: 1
            }]
        });
    });
})