	
var $JQAttivaCookie = jQuery.noConflict();
  
$JQAttivaCookie(document).ready(function() {
	CookieAttivaScript();
});
	

function CookieAttivaScript()
	{	
	var consensoString='';
	
	$JQAttivaCookie.post("https://www.caponipiero.it/cookiebanner.php", { azione: "checkcookieconsenso" } , function( data ) {
		
		if(data!='OK')
			{
			//annullo il cookie del consenso
			document.cookie = 'CookieConsensoInfo' + '=; expires=Thu, 01-Jan-70 00:00:01 GMT;';

			RemoveCookie();
			
			//resettare le checkbox
			$JQCookie("input:not([disabled])[name^='consensocookie']").prop('checked',false);
			
			$JQAttivaCookie("#area-cookie2021-promemoria").hide();
			$JQAttivaCookie("#area-cookie2021-sfondo").show();
			$JQAttivaCookie("#area-cookie2021").show();
			}
		

		consensoString=getCookie('CookieConsensoInfo');
		console.log(consensoString)

		var consensoArray= new Array();
		if(consensoString)
			{
			consensoArray=JSON.parse(consensoString); 
			}
		
		delete consensoArray["dataora"]; // vecchio valore
		delete consensoArray["timestamp"];
		delete consensoArray["stamp"];
		delete consensoArray["id"]; 
		delete consensoArray["version"]; 
		delete consensoArray["country"]; 






		//TAG HTML DA CORREGGERE
		
		// script
		$JQAttivaCookie("script[data-cookiecategory]").each(function() {
			
			var dataCookieCategory=$JQAttivaCookie(this).attr("data-cookiecategory").split(',');
			  			
			var consensoTrovato=true;
			
			$JQAttivaCookie.each(dataCookieCategory, function(categoria, valore) {
				
				if(valore!='necessary')
					{
	  				if(consensoArray[valore]!==true)
	  					{
	  					consensoTrovato=false;	
	  					}
					}
			});
	  			
	  			
	  			if(consensoTrovato)
	  				{
					var questo=$JQAttivaCookie(this);
					
	  				var text=$JQAttivaCookie(this).text();
	  				
	  				if(text!='')
	  					{
		  				$JQAttivaCookie.globalEval($JQAttivaCookie(this).text());
		  				}
		  			else
		  				{
		  				var srcscript=$JQAttivaCookie(this).attr("src");
		  				
		  				if(srcscript!='')
		  					{
		  					var newscript = document.createElement('script'); 
						    newscript.type = 'text/javascript'; 
						  
							newscript.onload = function(){
						        //console.log("Script loaded and ready");
						  	};
						    
						    var num=this.attributes.length;
					      
						    var i=0;					    
						  	$JQAttivaCookie.each(this.attributes, function() {
						  		
								i++;
								
						    	if(this.specified && this.name!='type') 
						    		{
						    		$JQAttivaCookie(newscript).attr(this.name, this.value);
						    		}
						    		
						    	if(i == num) 
						    		{
								    var body= document.getElementsByTagName('body')[0];
									body.appendChild(newscript);
								  	}
						 	 });
			  				}
		  				}
		  			}			    	
		  	});
		  	
		  	
		  	



		// iframe
		$JQAttivaCookie("iframe[data-cookiecategory]").each(function() {
			
			var dataCookieCategory=$JQAttivaCookie(this).attr("data-cookiecategory").split(',');
			  			
			var consensoTrovato=true;
			
			$JQAttivaCookie.each(dataCookieCategory, function(categoria, valore) {
				
				if(valore!='necessary')
					{
					if(consensoArray[valore]!==true && valore!='necessary')
						{
						consensoTrovato=false;	
						}
					}
			});
			
	
		if(consensoTrovato)
			{
  			$JQAttivaCookie(this).attr('src', $JQAttivaCookie(this).attr('cookieIframe-src'));
  			} 
  		else
  			{
  			$JQAttivaCookie(this).attr('src', '');	
  			
			$JQAttivaCookie(this).load(function(event){
				$JQAttivaCookie(this).contents().find('body').html('<div class="area-cookie2021-avviso-blocco" style="text-align: center; font-family: \'Arial\';">Accetta i cookies per visualizzare questo contenuto.<BR>Clicca l\'icona in basso a sinistra per accettare tutti i cookies.</div>');
			});
  			}
		});
		  	
		  	
		  	



		// img
		$JQAttivaCookie("img[data-cookiecategory]").each(function() {
			
			var dataCookieCategory=$JQAttivaCookie(this).attr("data-cookiecategory").split(',');
			  			
			var consensoTrovato=true;
			
			$JQAttivaCookie.each(dataCookieCategory, function(categoria, valore) {
				
				if(valore!='necessary')
					{
					if(consensoArray[valore]!==true && valore!='necessary')
						{
						consensoTrovato=false;	
						}
					}
			});
			
	
		if(consensoTrovato)
			{
  			$JQAttivaCookie(this).attr('src', $JQAttivaCookie(this).attr('cookieImg-src'));
  			
  			$JQAttivaCookie("+ .area-cookie2021-avviso-blocco", $JQAttivaCookie(this)).remove();
  			} 
  		else
  			{
  			$JQAttivaCookie(this).attr('src', '');	
  			
			$JQAttivaCookie('<div class="area-cookie2021-avviso-blocco" style="text-align: center; font-family: \'Arial\';">Accetta i cookies per visualizzare questo contenuto.<BR>Clicca l\'icona in basso a sinistra per accettare tutti i cookies.</div>').insertAfter(this);
  			}
		});
		
		
		
		
  		
	});
	
	}	

var $JQCookie = jQuery.noConflict();
  
$JQCookie(document).ready(function() {	
	
	$JQCookie("#area-cookie2021-chiudi").unbind("click");
	$JQCookie("#area-cookie2021-necessari").unbind("click");
	$JQCookie("#area-cookie2021-selezionati").unbind("click");
	$JQCookie("#area-cookie2021-tutti").unbind("click");
	$JQCookie("#area-cookie2021-promemoria").unbind("click");
	$JQCookie("#area-cookie2021-impostazioni").unbind("click");
	
    	
	var ScreenHeight=window.innerHeight;
		
	var bottomAreaCookie=$JQCookie("#area-cookie2021").css("bottom").replace('px','');
	if(bottomAreaCookie==0)
		{
		bottomAreaCookie=$JQCookie(".area-cookie2021-contenuto").css("bottom").replace('px','');
		}
	if(bottomAreaCookie<0 || bottomAreaCookie=='auto')
		{
		bottomAreaCookie=0;
		}
		
	var heightAreaCookie=$JQCookie("#area-cookie2021").outerHeight(true);
	if(heightAreaCookie==0)
		{
		heightAreaCookie=$JQCookie(".area-cookie2021-contenuto").outerHeight(true);
		}		
	
	var heightDettagliCookie=0;
	if($JQCookie(".area-cookie2021-contenuto-dettagli-cookie").css('display')!='none')
		{
		heightDettagliCookie=$JQCookie(".area-cookie2021-contenuto-dettagli-cookie").outerHeight(true);
		}
	var heightAreaCookie2=heightAreaCookie-heightDettagliCookie;
	
	var maxHeight=ScreenHeight-heightAreaCookie2-(bottomAreaCookie*2);
			
	$JQCookie(".area-cookie2021-contenuto-dettagli-cookie").css({"max-height":maxHeight+"px"});
		
		
	//consenso solo ai necessari + chiusura banner	
	$JQCookie("#area-cookie2021-chiudi, #area-cookie2021-necessari").click(function(){	
		
		$JQCookie("input:not([disabled])[name^='consensocookie']").prop('checked',false);
		setCookieConsensoInfo();
		
	});	
	
	//consenso solo ai selezionati		
	$JQCookie("#area-cookie2021-selezionati").click(function(){	
		
		setCookieConsensoInfo();
		
	});	
	
	//consenso tutti	
	$JQCookie("#area-cookie2021-tutti").click(function(){	
				
		$JQCookie("input:not([disabled])[name^='consensocookie']").prop('checked',true);
		
		setCookieConsensoInfo();
		
	});	
	
	
	// bannerino promemoria	
	$JQCookie("#area-cookie2021-promemoria").click(function(){	

		$JQCookie("#area-cookie2021-sfondo").show();
		$JQCookie("#area-cookie2021").show();
		$JQCookie("#area-cookie2021-promemoria").hide();
		
	});	
	
	
	
	$JQCookie("#area-cookie2021-impostazioni").click(function(e, first){
	
	    e.preventDefault();	  

		$JQCookie(".area-cookie2021-contenuto-dettagli-cookie").slideToggle(first ? 0 : 400);

		$JQCookie(".area-cookie2021-contenuto-dettagli-cookie").css({"overflow-y":"auto"});
	
		$JQCookie("#area-cookie2021-impostazioni").hide();
		$JQCookie("#area-cookie2021-selezionati").show(); 	
	   	
	   	return false;
	});	
	
});

function setCookieConsensoInfo(txtconsenso='')
	{
	RemoveCookie();
	
	var arrayConsenso=new Array();
	
	var index=0;
	$JQCookie("input[name^='consensocookie']").each(function() {
		
		arrayConsenso[index]='"'+$JQCookie(this).attr('data-category')+'":'+$JQCookie(this).prop('checked');
				
		index++;
	});
	

	var registra=false;
	var scadenza=0;
	var consenso=txtconsenso;
	if(consenso=='')
		{		
		registra=true;
		scadenza='';
		consenso='{'+arrayConsenso.join(', ')+'}';
		}
	
	$JQCookie.post("https://www.caponipiero.it/cookiebanner.php", {"azione": "setcookie", "consenso": consenso, "registra": registra, "scadenza": scadenza} , function( data ) {
	 
	 	if(txtconsenso=='')
	 		{
			$JQCookie("#area-cookie2021").fadeOut(300);
			$JQCookie("#area-cookie2021-sfondo").hide(); 
			$JQCookie("#area-cookie2021-promemoria").show();	 	
	 		}
	 	
	 	
	 	if($JQCookie("#statoconsensocookiepagina").length>0 || $JQCookie("#statoconsensocookiebanner").length>0)
	 		{
			$JQAttivaCookie.post("https://www.caponipiero.it/cookiebanner.php", { azione: "statoconsenso" } , function( data2 ) {
				if($JQCookie("#statoconsensocookiepagina").length>0)
		 			{
		 			$JQCookie("#statoconsensocookiepagina").html(data2)	
		 			}
		 			
				if($JQCookie("#statoconsensocookiebanner").length>0)
		 			{
		 			$JQCookie("#statoconsensocookiebanner").html(data2)	
		 			}
	 		});
	 		}
	 		
	 		
	 	CookieAttivaScript();
	 	
	});
	}
	
function RemoveCookie()
	{	    	
    var cookies = document.cookie.split("; ");
    for (var i = 0; i < cookies.length; i++) 
    	{
        var cookie = cookies[i];
        var eqPos = cookie.indexOf("=");
        var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
        
        //console.log(cookie)
        //console.log(eqPos)
        //console.log(name)
        
        if(name!='PHPSESSID')
        	{
        	document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
        	}
    	}
	}
	