(function(w){

	//var justOne = false;

 	var control_cookies = {
		cookies:null,

		consent_c: {name: 'consent', enable: 'enable', disable:'disable', time: 365},

		init: function () {

			/*if (justOne) return;
			justOne = true;*/

			this.GACode_1 = "";
			this.GACode_2 = "";

			this.acepted = false;

			this.cookies = w.cookies_;
			
			this.cookBot_ = document.getElementById("abrirCookies");

			this.faldon_ = document.getElementById("faldon");

			this.aceptar_ = document.getElementById("aceptar");
			this.cancelar_ = document.getElementById("cancelar");

			this.close_cookies_ = document.getElementById("close_cookies");

			this.cookBot_.addEventListener("click", function () { gsap.to (w.control_cookies_.faldon_, {duration:.6, bottom: "3em", ease:Quad.easeInOut  });	}, false);

  			this.aceptar_.addEventListener("click", function () { w.control_cookies_.aceptarCookies (); }, false);
			this.cancelar_.addEventListener("click", function () { w.control_cookies_.denegarCookies (); }, false);
			this.close_cookies_.addEventListener("click", function () { w.control_cookies_.cierraFaldon (); }, false);

			this.isEnable ();
		},

		cierraFaldon : function () {
			gsap.to (this.faldon_,{ duration:.6, bottom: -300, ease:Quad.easeInOut  });
		},

		aceptarCookies: function () {
			if (this.acepted) return;

			//console.log ("aceptar cookies, la escribo");
			this.setConsent ();
				
			this.acepted = true;

			gsap.to (this.faldon_, { duration:.6, bottom: -300, ease:Quad.easeInOut  });

			gsap.set (this.aceptar_, { opacity: .5, cursor: "default"  });
			gsap.set (this.cancelar_, { opacity: 1, cursor: "pointer"  });

			this.writeAdobe ();
		},

		writeAdobe: function () {
			//console.log ("append del pixel de adobe");
			/*var hasGA_ = this.cookies.getCookie ("_ga_BJY9TD3L0W")
			if (hasGA_.length > 0) return;*/	
			if (this.written) return;
			this.written = true;

			// GA4
			this.GACode_1 ="<!-- Google tag (gtag.js) -->";
			this.GACode_1 +="<script async src='https://www.googletagmanager.com/gtag/js?id=G-C391ZGRYRD'></script>";
			this.GACode_1 +="<script>";
  			this.GACode_1 +="window.dataLayer = window.dataLayer || [];";
  			this.GACode_1 +="function gtag(){dataLayer.push(arguments);}";
  			this.GACode_1 +="gtag('js', new Date());";
			this.GACode_1 +="gtag('config', 'G-C391ZGRYRD');";
			this.GACode_1 +="</script>";
		
			var range = document.createRange();
			range.selectNode(document.getElementsByTagName("HEAD")[0]);
			var documentFragment = range.createContextualFragment(this.GACode_1);
			document.head.appendChild(documentFragment);

			/* Añadimos en el body el GA4 */
			this.GACode_2 ="<!-- Google Tag Manager (noscript) -->"
			this.GACode_2 +="<noscript><iframe src='https://www.googletagmanager.com/ns.html?id=G-C391ZGRYRD' height='0' width='0' style='display:none;visibility:hidden'></iframe></noscript>"
			this.GACode_2 +="<!-- End Google Tag Manager (noscript) -->"
			
			var range2 = document.createRange();
			range2.selectNode(document.getElementsByTagName("BODY")[0]);
			var documentFragment2 = range2.createContextualFragment(this.GACode_2);
			document.body.appendChild(documentFragment2);
		},

		denegarCookies: function () {
			//if (!this.acepted) return;
			//console.log ("denegar cookies, la escribo");
			this.delConsent ();
			
			this.acepted = false;

			gsap.to (this.faldon_,{ duration:.6, bottom: -300, ease:Quad.easeInOut  });

			gsap.set (this.aceptar_, { opacity: 1, cursor: "pointer"  });
			gsap.set (this.cancelar_, { opacity: .5, cursor: "default"  });
		},

		isEnable: function () {
			var a_ = this.cookies.getCookie (this.consent_c.name);

			if (a_  == "enable") {
				//console.log ("is enable");
				this.acepted = true;
				gsap.to (this.faldon_,{ duration:.6, bottom: -300, ease:Quad.easeInOut  });

				gsap.set (this.aceptar_, { opacity: .5, cursor: "default"  });
				gsap.set (this.cancelar_, { opacity: 1, cursor: "pointer"  });

				this.writeAdobe ();
			} else {
				if (a_  == "disable" || a_ == "") {
					//console.log ("is disable disable");
					this.acepted = false;

					gsap.set (this.aceptar_, { opacity: 1, cursor: "pointer"  });
					gsap.set (this.cancelar_, { opacity: .5, cursor: "default"  });
				}
			}
		},
		
		setConsent: function () { this.cookies.setCookie (this.consent_c.name, this.consent_c.enable, this.consent_c.time); },

		delConsent: function () { this.cookies.setCookie (this.consent_c.name, this.consent_c.disable, this.consent_c.time); },

		getConsent: function (cname) { 
			console.log(this.cookies.getCookie (this.consent_c.name)); 
		},

		checkIt: function () { /*console.log(this.cookies.checkCookie ());*/ }
		
	}

	 w.control_cookies_ = control_cookies;
	
})(window);


