/************************************************************
 ********** FABIO LUCATTINI - SCRIPT ************************
 ************************************************************/
var screenW, screenH, hFOTO, wFOTO, x
var statopala = 1
var maxchars=160;	

$(document).ready(function(){ preload() });

function onLoadFunction(){
	sizeWindow()
	//preload()
	//webmaster()
	creaTesto()
	centraSito()
	document.getElementById("buttGallery").onclick = function(){ cambiaPagina('gallery','h',screenH) };
	barraFeedBack() 
	icon()
	initGallery()
	cambiaImmagini()
	visualizzaPala()
	cambiaPagina('news')
}

function onResizeFunction()
{
	sizeWindow();
	centraSito();
	visualizzaPala()
	barraFeedBack()
	icon()
}

function onUnLoadFunction()
{
}

function preload() 
{
	if(!document.getElementById("preload"))
	{
		var imgs = new Array("pala.png","logo.png","sfondo.png", "sfondoLogo.png");
		
		var preload = document.createElement("div");
		preload.id = "preload"
		preload.style.width = "100%";
		preload.style.height = "100%";
		preload.style.zIndex = 80
		preload.style.paddingTop = "10%"
		preload.style.fontFamily = "Verdana, Arial, Helvetica, sans-serif"
		preload.style.fontSize = 18;
		preload.style.fontWeight = "bold";
		preload.style.color = "#FFF";
		preload.style.position = "fixed"
		preload.style.backgroundColor = "#000"  
		document.body.appendChild(preload)
		//preload.innerHTML="<center><img id='logoStart' src='immagini/logo.png' style='background-color:#FFFFFF;' height='100'><br><br>...ATTENDERE PREGO...</center>"
		preload.innerHTML="<center><br><br>...CARICAMENTO ATTENDERE PREGO...</center>"
		
		if (navigator.appName != "Microsoft Internet Explorer"){
			preload.style.opacity=0.8
			//document.getElementById("logoStart").style.opacity=1
		}else {
			preload.style.filter="alpha(opacity=80)"
			//document.getElementById("logoStart").style.filter="alpha(opacity=1)"
		}
		
	
		for (i=0;i<imgs.length;i++){	
			imgs2 = new Image(2000,2000);
			imgs2.src = "immagini/"+imgs[i];
		}
	}
}

function webmaster(){
	
		var webmaster = document.createElement("div");
		var webmasterPunto = document.createElement("div");
		
		webmasterPunto.id = "webmasterPunto"
		webmasterPunto.style.width = "20px";
		webmasterPunto.style.height = "15px";
		webmasterPunto.style.zIndex = 0;
		webmasterPunto.style.bottom = "5px"
		webmasterPunto.style.top = "auto"
		webmasterPunto.style.left = "5px"
		webmasterPunto.style.cursor = "pointer"
		webmasterPunto.style.fontFamily = "Verdana, Arial, Helvetica, sans-serif"
		webmasterPunto.style.fontSize = 18;
		webmasterPunto.style.fontWeight = "bold";
		webmasterPunto.style.color = "#FFF";
		webmasterPunto.style.position = "fixed"
		webmasterPunto.style.backgroundColor = "#000"  
		document.body.appendChild(webmasterPunto)
		
		webmaster.id = "webmaster"
		webmaster.style.width = "20px";
		webmaster.style.height = "190px";
		webmaster.style.zIndex = 0;
		webmaster.style.bottom = "25px"
		webmaster.style.top = "auto"
		webmaster.style.left = "5px"
		webmaster.style.cursor = "pointer"
		webmaster.style.fontFamily = "Verdana, Arial, Helvetica, sans-serif"
		webmaster.style.fontSize = 18;
		webmaster.style.fontWeight = "bold";
		webmaster.style.color = "#FFF";
		webmaster.style.position = "fixed"
		webmaster.style.backgroundColor = "#000"  
		document.body.appendChild(webmaster)
		webmaster.innerHTML="<center>F<br>A<br>B<br>I<br>O<br>T<br>T<br>I<br>N<br>I</center>"
		
		if (navigator.appName != "Microsoft Internet Explorer"){
			webmaster.style.opacity=0.6
			webmasterPunto.style.opacity=0.6
		}else {
			webmaster.style.filter="alpha(opacity=60)"
			webmasterPunto.style.filter="alpha(opacity=60)"
		}
		
		webmaster.onclick = function(){
			apriWebmaster()
		}
}



function apriWebmaster()
{
	var webSfondoDiv = document.createElement("div");
	var webSfondo = document.createElement("div");
	var web = document.createElement("div");
	var chiudiweb = document.createElement("div");
	
	webSfondoDiv.id = "webSfondoDiv";
	webSfondo.id = "webSfondo";
	web.id = "web";
	chiudiweb.id = "chiudiweb";
	
	document.body.appendChild(webSfondoDiv);
	document.body.appendChild(webSfondo);
	webSfondo.appendChild(chiudiweb);
	webSfondo.appendChild(web);
	
	if (navigator.appName != "Microsoft Internet Explorer"){
		webSfondoDiv.style.opacity=0.8
	}else {
		webSfondoDiv.style.filter="alpha(opacity=80)"
	}
	
	chiudiweb.innerHTML = "<img src='immagini/x.png' class='chiudiNewsImg'>";
	webSfondo.style.marginLeft = ((screenW/2)-250)+"px";
	
	webSfondoDiv.onclick = function(){
		document.body.removeChild(webSfondoDiv);
		document.body.removeChild(webSfondo);
	}
	
	chiudiweb.onclick = function(){
		document.body.removeChild(webSfondoDiv);
		document.body.removeChild(webSfondo);
	}
	web.innerHTML = "<center>... SITO REALIZZATO DA FABIO LUCATTINI ...<br><br><img height='130' src='immagini/web.png'></center>";
}


function attachInfowindow(marker, number, titolo) {
  var infowindow = new google.maps.InfoWindow(
      { content: titolo
      });
  google.maps.event.addListener(marker, 'click', function() {
    infowindow.setZIndex(++infowindowLevel);
    infowindow.open(map,marker);
  });
}

function sizeWindow()
{
		if (navigator.appName != "Microsoft Internet Explorer"){
			screenW = window.innerWidth;
			screenH =  window.innerHeight;
		}else {
			screenW = document.body.offsetWidth;
			screenH = document.body.offsetHeight;
		}
		x = ( screenW > 1024 ) ? (screenW - 1100)/2 : 0; 
		hFOTO = ( screenH -200 < 500 )? screenH -200 : 500;
		window.onresize = function(){ onResizeFunction() };
}


function initGallery()
{
	$(function() {
        $('#gallery a').lightBox();
    });
}

function initGalleryNews()
{
	$(function() {
        $('#galleryNews a').lightBox();
    });
}

function centraSito()
{
	
	document.getElementById("pala").style.marginLeft = screenW+600+"px";
	document.getElementById("logo").style.marginLeft=x+"px"
	document.getElementById("foto").style.marginLeft=x+"px"
	document.getElementById("freccia1").style.marginLeft=x+"px"
	document.getElementById("freccia234").style.marginLeft=x+"px"
	document.getElementById("menu").style.marginLeft=x+"px"
	document.getElementById("omino1").style.marginLeft=x+"px"
	document.getElementById("omino2").style.marginLeft=(x+500)+"px"
	document.getElementById("omino3").style.marginLeft=(x+320)+"px"
	if(document.getElementById("testo"))
		document.getElementById("testo").style.marginLeft=(x+330)+"px"
	document.getElementById("sfondoIMG").style.width = (screenW)+"px";
}

function cambiaImmagini()
{
	document.getElementById("foto1").src = "script/vi.php?id=1&w=145&h=150&wm=0";
	document.getElementById("foto2").src = "script/vi.php?id=2&w=145&h=150&wm=0";
	document.getElementById("foto3").src = "script/vi.php?id=3&w=145&h=150&wm=0";
	document.getElementById("foto4").src = "script/vi.php?id=1&w=145&h=150&wm=0";
	
	document.getElementById("afoto1").href = "script/vi2.php?id=1&wm=1&w=800&h="+hFOTO;
	document.getElementById("afoto2").href = "script/vi2.php?id=2&wm=1&w=800&h="+hFOTO;
	document.getElementById("afoto3").href = "script/vi2.php?id=3&wm=1&w=800&h="+hFOTO;
	document.getElementById("afoto4").href = "script/vi2.php?id=1&wm=1&w=800&h="+hFOTO;
	
	setTimeout("cambia(1)",3000);
}

function riempimenu()
{
	$.ajax({
		   type: "POST",
		   url: "script/getCategorie.php",
		   data: "",
		   success: function(msg)
		   {
			  mgc = document.getElementById("menuGalleryContenuto");			   
			  
			  var tmgc = document.createElement("menuGalleryTesto");
			  tmgc.id = "menuGalleryTesto";			   
			  mgc.appendChild( tmgc );
			   
			  tmgc.innerHTML = msg;
			  
			 		
					
			  if (navigator.appName != "Microsoft Internet Explorer"){
				tmgc.style.opacity=1
			  }else {
				tmgc.style.filter="alpha(opacity=100)"
			  }
			
		   }	
		   
		});
}

var map;
var infowindowLevel = 0;
function attachInfowindow(marker, number, titolo) {
  var infowindow = new google.maps.InfoWindow(
	  { 
		content: titolo
	  });
  google.maps.event.addListener(marker, 'click', function() {
	
	infowindow.setZIndex(++infowindowLevel);
	infowindow.open(map,marker);
  });
}

var marker = new Array();
var myLatLng = new Array();
var beaches = [
	[],
	['<span class="mappaTesto"><b> Via Provinciale, 18 <br> Fornaci Di Barga (LU) <br> Tel 0583 758982</b><br><a href="http://maps.google.it/maps?f=q&source=s_q&hl=it&geocode=&q=44.04927,10.480511&sll=41.442726,12.392578&sspn=14.814581,43.286133&ie=UTF8&t=h&z=16" target="_blank" class="mappaTesto">Calcola Percorso</a></span>',44.04927,10.480511,7],
	['<span class="mappaTesto"><b>Via Vittorio Emanuele, 27 <br> Lucca <br> Tel 0583 317233</b><br><a href="http://maps.google.it/maps?f=q&source=s_q&hl=it&geocode=&q=43.84203,10.499587&sll=41.442726,12.392578&sspn=14.814581,43.286133&ie=UTF8&t=h&z=16" target="_blank" class="mappaTesto">Calcola Percorso</a></span>',43.84203,10.499587,4],
	['<span class="mappaTesto"><b>Via Santa Croce, 58 <br> Lucca</b><br> Tel 0583 469842<br><a href="http://maps.google.it/maps?f=q&source=s_q&hl=it&geocode=&q=43.842514,10.506416&sll=41.442726,12.392578&sspn=14.814581,43.286133&ie=UTF8&t=h&z=16" target="_blank" class="mappaTesto">Calcola Percorso</a></span>',43.842514,10.506416,5],
	['<span class="mappaTesto"><b>Piazza San Francesco, 21 <br> Lucca <br> Tel 0583 440545</b><br><a href="http://maps.google.it/maps?f=q&source=s_q&hl=it&geocode=&q=43.845293,10.510786&sll=41.442726,12.392578&sspn=14.814581,43.286133&ie=UTF8&t=h&z=16" target="_blank" class="mappaTesto">Calcola Percorso</a></span>',43.845293,10.510786,6],
	['<span class="mappaTesto"><b>Piazza San Francesco, 17 <br> Lucca <br> Tel 0583 467084</b><br><a href="http://maps.google.it/maps?f=q&source=s_q&hl=it&geocode=&q=43.845506,10.510652&sll=41.442726,12.392578&sspn=14.814581,43.286133&ie=UTF8&t=h&z=16" target="_blank" class="mappaTesto">Calcola Percorso</a></span>',43.845506,10.510652,1],
	['<span class="mappaTesto"><b>Via Roma, 120/122 Porcari <br> Lucca <br> Tel 0583 29113</b><br><a href="http://maps.google.it/maps?f=q&source=s_q&hl=it&geocode=&q=43.837906,10.619718&sll=41.442726,12.392578&sspn=14.814581,43.286133&ie=UTF8&t=h&z=16" target="_blank" class="mappaTesto">Calcola Percorso</a></span>',43.837906,10.619718,3],
	['<span class="mappaTesto"><b>Viale Europa, 46 <br> Lammari di Capannori <br> Lucca <br> Tel 0583 436400</b><br><a href="http://maps.google.it/maps?f=q&source=s_q&hl=it&geocode=&q=43.864047,10.581259&sll=41.442726,12.392578&sspn=14.814581,43.286133&ie=UTF8&t=h&z=16" target="_blank" class="mappaTesto">Calcola Percorso</a></span>',43.864047,10.581259,2]
];

function mappa(num)
{	
	$("#pala").animate({ marginLeft: Number(x+955)}, 800);
	$("#testo").animate({ minHeight: "400px"}, 800, function(){


	var divDescrLocali = document.createElement("div");
	divDescrLocali.id="descrLocali";
	divDescrLocali.style.width= "567px";
	divDescrLocali.style.minHeight= "0px";
	document.getElementById("testo").appendChild(divDescrLocali);
	$.ajax({
		   type: "POST",
		   url: "pagine/mappa.php",
		   data: "",
		   success: function(msg)
		   {
			   $(divDescrLocali).animate({ minHeight: "200px"}, 500, function(){
			 	 divDescrLocali.innerHTML = msg;
				 if( num )
				 	apriDescrizione(num)
				});
		   }
	});
	
	var divMappa = document.createElement("div");
	divMappa.id="mappa";
	divMappa.style.width= "567px";
	divMappa.style.height= "400px";
	document.getElementById("testo").appendChild(divMappa);
	
	  var myLatlng = new google.maps.LatLng(43.992514, 10.569416);
	  var myOptions = {
		zoom: 10,
		center: myLatlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	  }
	
	  map = new google.maps.Map(divMappa, myOptions);
	 var image = new google.maps.MarkerImage('immagini/pointerMaps.png',
		  // This marker is 20 pixels wide by 32 pixels tall.
		  new google.maps.Size(32, 32),
		  // The origin for this image is 0,0.
		  new google.maps.Point(0,0),
		  // The anchor for this image is the base of the flagpole at 0,32.
		  new google.maps.Point(0, 32));
	  var shadow = new google.maps.MarkerImage('immagini/pointerMaps.png',
		  // The shadow image is larger in the horizontal dimension
		  // while the position and offset are the same as for the main image.
		  new google.maps.Size(37, 32),
		  new google.maps.Point(0,0),
		  new google.maps.Point(0, 32));
		  // Shapes define the clickable region of the icon.
		  // The type defines an HTML &lt;area&gt; element 'poly' which
		  // traces out a polygon as a series of X,Y points. The final
		  // coordinate closes the poly by connecting to the first
		  // coordinate.
	  var shape = {
		  coord: [1, 1, 1, 20, 18, 20, 18 , 1],
		  type: 'poly'
	  };
	
	  for (var i = 1, j = 1 ; i < (beaches.length); i++, j++) {
		var beach = beaches[i]; 
		myLatLng[j] = new google.maps.LatLng(beach[1], beach[2]);
		marker[j] = new google.maps.Marker({
			position: myLatLng[j],
			map: map,
			shadow: shadow,
			icon: image,
			shape: shape,
			title: beach[0].toString(),
			zIndex: beach[3]
		});
		
		marker[j].setTitle(i.toString());
		attachInfowindow(marker[j], i.toString(), beach[0].toString());
	  }										  
	});
	
}

var icon1Open = 0;
var icon2Open = 0;

function icon()
{
	$("#faceIcon").hover(
 	 function () {
		$("#faceIcon").animate({ marginBottom: 0}, 300);	
	 }
	 ,function () {
		$("#faceIcon").animate({ marginBottom: -35}, 300);
  	 })
	
	$("#w3cicon").hover(
 	 function () {
		$("#w3cicon").animate({ marginBottom: 0}, 300);	
	 }
	 ,function () {
		$("#w3cicon").animate({ marginBottom: -16}, 300);
  	 })
	//if(document.getElementById("preload"))
	//	document.body.removeChild(document.getElementById("preload"));
}

function inviaFeed()
{
	if( controllaMail() == 1 )
	{
		$.ajax({
		   type: "GET",
		   url: "script/lastId.php",
		   data: "",
		   success: function(msg)
		   {
				var num = msg;
				var i = 1;
				var numRisp = document.getElementById("numRisp").value;
				while( i <= numRisp )
				{
					if(document.getElementById("risp"+i))
					{
						if( document.getElementById("risp"+i).checked )
						{
							var testo = document.getElementById("textAreaFeed").value
							var mail = document.getElementById("emailFeedBack").value;
							if( testo == "Commenti" )
							{
								testo = ""
							}
							$.ajax({
							   type: "POST",
							   url: "pagine/feedBackRisp.php",
							   data: "risp="+i+"&testo="+testo+"&email="+mail,
							   success: function(msg)
							   {
								  document.getElementById("feedBack").style.paddingTop = "10%";
								  document.getElementById("feedBack").innerHTML = "<center>INSERITO CORRETTAMENTE <br>GRAZIE </center>"+msg;
								  setTimeout("barraFeedBack()",3000)
							   }
							});
						}
					}
					i++;
				}
		   }
		});	
	}
}

function caricaFeedBack()
{
	$.ajax({
		   type: "POST",
		   url: "pagine/feedBack.php",
		   data: "",
		   success: function(msg)
		   {
			  document.getElementById("feedBack").style.paddingTop = "0px";
			  document.getElementById("feedBack").innerHTML = msg;
			  document.getElementById("numcar").innerHTML = "0/"+maxchars;
		   }
	});
}

function barraFeedBack()
{
	var aperto = 0;
	var allarga = 200;
	if( !document.getElementById("feedBack") )
	{
		var div = document.createElement("feedBack");
		var divButt = document.createElement("feedBackButton");
		document.body.appendChild( div );
		document.body.appendChild( divButt );
		div.id = "feedBack";
		divButt.id = "feedBackButt";
		div.className = "feedDiv";
		divButt.className = "feedDivButt";
	
		if (navigator.appName != "Microsoft Internet Explorer"){
			div.style.opacity=0.9
		}else {
			div.style.filter="alpha(opacity=90)"
		}
	
		if( screenW > 1160 && aperto == 0 )
		{
			aperto=1;
			$(div).animate({ width: allarga+"px"}, 800);	
			$(divButt).animate({ right: allarga+"px"}, 800, function()
			{
				caricaFeedBack()
				divButt.style.height = "113px";														  
				divButt.innerHTML = "><br><b>C<br>H<br>I<br>U<br>D<br>I<br></b>><br>";	
			});
			
		}
	
		divButt.innerHTML = "<<br><b>F<br>E<br>E<br>D<br>B<br>A<br>C<br>K</b><br><<br>";	
		$(divButt).click(function(){		
			if( aperto == 0 )
			{
				$(div).animate({ width: allarga+"px"}, 800);	
				$(divButt).animate({ right: allarga+"px"}, 800, function(){
					caricaFeedBack()
					divButt.style.height = "113px";														  
					divButt.innerHTML = "><br><b>C<br>H<br>I<br>U<br>D<br>I<br></b>><br>";			
					aperto = 1;
				});		
			}
			else
			{
				//chiusura
				$(div).animate({ width: 0+"px"}, 800);	
				$(divButt).animate({ right: 0+"px"}, 800, function(){
					document.getElementById("feedBack").innerHTML = "";
					aperto = 0;
					divButt.style.height = "140px";		
					divButt.innerHTML = "<<br><b>F<br>E<br>E<br>D<br>B<br>A<br>C<br>K</b><br><<br>";	
				});
			}
		}); 
		div.style.fontFamily = "Verdana, Arial, Helvetica, sans-serif";
		div.style.fontSize = "16px"
		div.innerHTML = "<center>... ATTENDERE ...</center>"
	}
	else
	{
		document.body.removeChild(document.getElementById("feedBack"));
		document.body.removeChild(document.getElementById("feedBackButt"));
		barraFeedBack()
		
	}
}

function menuGallery()
{
	var mg = document.createElement("menuGallery");
	var mgc = document.createElement("menuGalleryContenuto");
	mgc.id = "menuGalleryContenuto";
	mg.innerHTML = "><br><b>C<br>A<br>T<br>E<br>G<br>O<br>R<br>I<br>E</b><br>>"
	mg.className = "menuGalleryVerticale";
	mgc.className = "menuGalleryVCOFF";
	
	document.getElementById("testo").appendChild( mg );
	document.getElementById("testo").appendChild( mgc );
	
	if (navigator.appName != "Microsoft Internet Explorer"){
		mgc.style.opacity=0.8
	}else {
		mgc.style.filter="alpha(opacity=80)"
	}
	
	var aperto = 0;
	
	$(mg).click(function(){		
		if( aperto == 0 )
		{
			$(mgc).animate({ width: 300+"px"}, 800);	
			$(mg).animate({ left: 300+"px"}, 800, function(){
				riempimenu();
				mg.style.marginTop = "15%";	
				mg.innerHTML = "<<br><b>C<br>H<br>I<br>U<br>D<br>I<br></b><<br>";			
				aperto = 1;
			});		
		}
		else
		{
			//chiusura
			document.getElementById("menuGalleryContenuto").removeChild(document.getElementById("menuGalleryTesto"));
			$(mgc).animate({ width: 0+"px"}, 800);	
			$(mg).animate({ left: 0+"px"}, 800, function(){
				document.getElementById("testo").removeChild(mg);
				document.getElementById("testo").removeChild(mgc);
				menuGallery()
			});
		}
	}); 
}


function sparisci(id)
{
	var idFoto = -1;
	document.getElementById("testo").innerHTML = "sparisci";
	$.ajax({
		   type: "POST",
		   url: "script/getFoto.php",
		   data: "",
		   success: function(msg)
		   {
			   idFoto = msg
			   $("#foto"+id).fadeTo(2000, 0.1, 
			   function()
			   {
				   
				   $.ajax(
					{
					   type: "GET",
					   url: "script/getInfo.php",
					   data: "id="+idFoto,
					   success: function(msg)
					   {
							document.getElementById("testo").innerHTML = "lette info";
							document.getElementById("afoto"+id).title = msg;
							
							document.getElementById("foto"+id).src = "script/vi.php?id="+idFoto+"&w=145&h=150&wm=0";
							
							if(document.getElementById("foto"+id).complete)
							{
								document.getElementById("testo").innerHTML = "completato";
							
								document.getElementById("afoto"+id).href = "script/vi.php?id="+idFoto+"&wm=1&h="+hFOTO;
								visualizza(id)
							}
					   }
					});
			   })
		   }
	});
}


function visualizza(id)
{
	document.getElementById("testo").innerHTML = "visualizza";
	$("#foto"+id).fadeTo(2000, 1, function(){ sparisci(id) });
	
}

var tempo = 6000;
function cambia(id)
{
		var idFoto = -1;
		$.ajax({
		   type: "POST",
		   url: "script/getFoto.php",
		   data: "",
		   success: function(msg)
		   {
			   idFoto = msg;
			   $.ajax({
						type: "GET",
						url: "script/getInfo.php",
						data: "id="+idFoto,
						success: function(msg)
						{
							
							document.getElementById("afoto"+id).title = msg;
							
							document.getElementById("foto"+id).src = "script/vi.php?id="+idFoto+"&w=145&h=150&wm=0";
							
							document.getElementById("afoto"+id).href = "script/vi.php?id="+idFoto+"&wm=1&h="+hFOTO;
							
							//$("#foto"+id).fadeTo(1000, 1);			
							if( (id + 1) > 4  )
							{
								id = 1
								setTimeout("cambia("+id+")",3000);
							}
							else
								setTimeout("cambia("+(id+1)+")",3000);
							
						}
					});	 
			}	
		});
}




function visualizzaPala()
{
	if( statopala == 1 )
	{
		$("#pala").animate({ 
			marginLeft: (x+730)+"px"
		}, 800, function(){
			 setTimeout( function() { 
							if(document.getElementById("preload")){
								document.body.removeChild(document.getElementById("preload"));
							}
						}, 1500 );
	
		});
	}
	else
	{
		$("#pala").animate({ 
			marginLeft: (x+955)+"px"
		}, 800, function(){
			 setTimeout( function() { 
							if(document.getElementById("preload")){
								document.body.removeChild(document.getElementById("preload"));
							}
						}, 1500 );
	
		});
	}
}

function creaTesto()
{
	var testo = document.createElement("div");
	testo.id="testo";
	
	document.getElementById("gallery2").appendChild(testo);
	testo.style.marginLeft=(x+330)+"px"
	testo.style.backgroundColor = "";
	$(testo).fadeTo(0,0.01);
}



function apriLocale(num)
{
	document.getElementById("gallery2").removeChild( document.getElementById("testo") );
	creaTesto()
	$("#testo").fadeTo(800, 1, function(){
		mappa(num)
		statopala = 0;	
	});
}


function cambiaPagina( pagina, vari, value, var2, value2 )
{	
   statopala = 1;
   document.getElementById("gallery2").removeChild( document.getElementById("testo") );
   var var3, value3;
   if( pagina == "gallery" )
   {
	   var3 = "h";
	   value3 = screenH;
   }
   creaTesto()				
   $.ajax({
		   type: "GET",
		   url: "pagine/"+pagina+".php", 
		   data: vari+"="+value+"&"+var2+"="+value2+"&"+var3+"="+value3,
		   success: function(msg)
		   {
			   	if( pagina == "mappa" )
				{		
					mappa()
					statopala = 0;
				}
				else
				{					
					$("#testo").animate({ minHeight: "300px"}, 800);
					document.getElementById("testo").style.height = "";
					msg = (msg).replace(/\ì/g,"&igrave;");
					msg = (msg).replace(/\à/g,"&agrave;");
					msg = (msg).replace(/\è/g,"&egrave;");
					msg = (msg).replace(/\ò/g,"&ograve;");
					msg = (msg).replace(/\ù/g,"&ugrave;");  
					document.getElementById("testo").innerHTML = '<div id="divFloat" style=" width:190px; height:140px; float:right" ></div>'+msg;
					
					if( pagina == "specialita" )
					{		
						$("#gallery3 a").lightBox();
						statopala = 1;
					}
					else if( pagina == "gallery" )
					{
						var yscroll = jQuery(document.body).offset().top;
						$("#pala").animate({ marginLeft: (x+955)+"px"}, 800);
						$("#divFloat").animate({ width: "0px"}, 800);
						document.getElementById("divFloat").style.marginTop=yscroll+"px"; 						
						$("#gallery2 a").lightBox(); 
						menuGallery()
						statopala = 0;
					}
					else
					{
						$("#pala").animate({ marginLeft: (x+730)+"px"}, 800);
						$("#divFloat").animate({ width: "190px"}, 800);
					}
					
				}
				
				var yscroll = jQuery(document.body).offset().top;
				$(window).scroll(function() 
				{							
					yscroll = $(this).scrollTop()
					if( yscroll > 15  && statopala == 1 )
					{
						$("#pala").animate({ marginLeft: (x+955)+"px" }, 500);
						$("#divFloat").animate({ width: "0px"}, 500);
						if(document.getElementById("divFloat"))
							document.getElementById("divFloat").style.marginTop=yscroll+"px";
					}
					else if(  yscroll <= 15 && statopala == 1 ) 
					{
						$("#pala").animate({ marginLeft: (x+730)+"px"}, 300);
						$("#divFloat").animate({ width: "190px"}, 300);
						if(document.getElementById("divFloat"))
							document.getElementById("divFloat").style.marginTop=yscroll+"px";							
					}
				})
				$("#testo").fadeTo(800, 1);
		   },
		   error: function(msg)
		   {
			   document.getElementById("testo").innerHTML = "PAGINA IN COSTRUZIONE";
			   $("#testo").fadeTo(800, 1);
		   }
		  });

	
   
}

function apriDescrizione(num)
{
	var info;
	var col = document.createElement("td");
	col.id = "colonnaDescr";
	document.getElementById("riga"+num).appendChild(col);
	col.colSpan = 4
	col.style.width="100%"
  
//	if (info)
//		info.close();
	var beach = beaches[num]; 
	info = new google.maps.InfoWindow({  
		 	content: beach[0]  
			});

	$.ajax(
		{
			type: "GET",
			url: "pagine/locali/"+num+".html",
			data: "",
			success: function(msg)
			{			
				col.innerHTML	= 	msg;
				$(col).slideDown("slow");
				document.getElementById("piu"+num).innerHTML = "-"
			
				info.open(map, marker[num]);
					
				document.getElementById("piu"+num).onclick = function(){
					if( info )
						info.close();
					document.getElementById("riga"+num).removeChild(col);
					document.getElementById("piu"+num).innerHTML = "+"
					document.getElementById("piu"+num).onclick = function(){ apriDescrizione(num) }
				}
			},
			error: function(msg)
			{
				col.innerHTML	= "<center>Nessuna descrizione per il locale</center>"
				info.open(map, marker[num]);
				$(col).slideDown("slow");
				document.getElementById("piu"+num).innerHTML = "-"
				document.getElementById("piu"+num).onclick = function(){
					if( info )
						info.close();
					document.getElementById("riga"+num).removeChild(col);
					document.getElementById("piu"+num).innerHTML = "+"
					document.getElementById("piu"+num).onclick = function(){ apriDescrizione(num) }
				}
			}
		});
	
}



function apriNews(num)
{
	var newsSfondoDiv = document.createElement("div");
	var newsSfondo = document.createElement("div");
	var news = document.createElement("div");
	var chiudiNews = document.createElement("div");
	
	newsSfondoDiv.id = "newsSfondoDiv";
	newsSfondo.id = "newsSfondo";
	news.id = "news";
	chiudiNews.id = "chiudiNews";
	
	document.body.appendChild(newsSfondoDiv);
	document.body.appendChild(newsSfondo);
	newsSfondo.appendChild(chiudiNews);
	newsSfondo.appendChild(news);
	
	if (navigator.appName != "Microsoft Internet Explorer"){
		newsSfondoDiv.style.opacity=0.8
	}else {
		newsSfondoDiv.style.filter="alpha(opacity=80)"
	}
	
	chiudiNews.innerHTML = "<img src='immagini/x.png' class='chiudiNewsImg'>";
	newsSfondo.style.marginLeft = ((screenW/2)-250)+"px";
	
	newsSfondoDiv.onclick = function(){
		document.body.removeChild(newsSfondoDiv);
		document.body.removeChild(newsSfondo);
	}
	
	chiudiNews.onclick = function(){
		document.body.removeChild(newsSfondoDiv);
		document.body.removeChild(newsSfondo);
	}
	
	news.innerHTML = "<center>... ATTENDERE PREGO ...</center>";
	$.ajax({
			type: "GET",
			url: "pagine/newsAperta.php",
			data: "id="+num,
			success: function(msg)
			{
				news.innerHTML = msg;
				initGalleryNews()
			},
			error: function(msg)
			{
				news.innerHTML = "<center>ERRORE: Caricamento news</center>";
			}
		});	
}



function ricontrollaCampoMail()
{
	if(document.getElementById("emailFeedBack").value == "")
	{
		document.getElementById("emailFeedBack").value = "MAIL";
	}
}

function ricontrollaCampoCommenti()
{
	if(document.getElementById("textAreaFeed").value == "")
	{
		document.getElementById("textAreaFeed").value = "Commenti";
	}
}

function azzeraCampoMail()
{
	if(document.getElementById("emailFeedBack").value == "MAIL")
	{
		document.getElementById("emailFeedBack").value = "";
	}
}

function azzeraCampoCommenti()
{
	if(document.getElementById("textAreaFeed").value == "Commenti")
	{
		document.getElementById("textAreaFeed").value = "";
	}
}

function controllaMail()
{
	var value = document.getElementById("emailFeedBack").value;
	if( value != "" )
	{
		if(controllomail(value) == -1)
		{
			alert("CONTROLLA CHE LA MAIL SIA CORRETTA!")
			return -1;
		}
		else
		{
			return 1;
		}
	}
}

function controllaMailGenerica(str)
{
	var value = str;
	if( value != "" )
	{
		if(controllomail(value) == -1)
		{
			alert("CONTROLLA CHE LA MAIL SIA CORRETTA!")
			return -1;
		}
		else
		{
			return 1;
		}
	}
}



function controllomail(mail){
	var espressione = /^[_a-z0-9+-]+(\.[_a-z0-9+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$/;
	if (!espressione.test(mail))
	{
	    return -1;
	}
	else
	{
		return 1;
	}
}

function creaSconto()
{
	var newsSfondoDiv = document.createElement("div");
	var newsSfondo = document.createElement("div");
	var news = document.createElement("div");
	var chiudiNews = document.createElement("div");
	
	newsSfondoDiv.id = "newsSfondoDivSconto";
	newsSfondo.id = "newsSfondoSconto";
	news.id = "sconto";
	chiudiNews.id = "scontoChiudi";
	
	document.body.appendChild(newsSfondoDiv);
	document.body.appendChild(newsSfondo);
	newsSfondo.appendChild(chiudiNews);
	newsSfondo.appendChild(news);
	
	if (navigator.appName != "Microsoft Internet Explorer"){
		newsSfondoDiv.style.opacity=0.8
	}else {
		newsSfondoDiv.style.filter="alpha(opacity=80)"
	}
	
	chiudiNews.innerHTML = "<img src='immagini/x.png' class='chiudiNewsImg'>";
	newsSfondo.style.marginLeft = ((screenW/2)-250)+"px";
	
	newsSfondoDiv.onclick = function(){
		document.body.removeChild(newsSfondoDiv);
		document.body.removeChild(newsSfondo);
	}
	
	chiudiNews.onclick = function(){
		document.body.removeChild(newsSfondoDiv);
		document.body.removeChild(newsSfondo);
	}
	news.innerHTML = "<center><h1>... ATTENDERE ...</h1></center>";
	$.ajax({
		type: "GET",
		url: "pagine/sconto.html",
		data: "",
		success: function(msg)
		{
			news.innerHTML = msg;
		},
		error: function(msg)
		{
			news.innerHTML = "ERRORE: Caricamento Sconti non corretto";
		}
	});	
}

function inviaSconto()
{
	var n = document.getElementById("nomeSconto").value;
	var c = document.getElementById("cognomeSconto").value;
	var m = document.getElementById("mailSconto").value;
	var nc = document.getElementById("cellSconto").value;
	var acc = document.getElementById("acc").checked;
	var nacc = document.getElementById("nacc").checked;
	
	if( n == "" ){
		alert("Inserire il nome");
	}else if( c == "" ){
		alert("Inserire il cognome");
	}else if( m == "" ){
		alert("Inserire la e-mail");
	}else if(controllaMailGenerica(m) != 1){
	}else if( nc == "" ){
		alert("Inserire il numero di cellulare")
	}else if( nacc ){
		alert("Per poter ricevere il nostro sconto deve\n dare il consenso al trattamento dei dati personali")
	}
	else
	{
		document.getElementById("sconto").innerHTML = "<center>... ATTENDERE ...</center>"
		$.ajax({
			type: "GET",
			url: "script/sconto.php",
			data: "email="+m+"&nome="+n+"&cogn="+c+"&cell="+nc,
			success: function(msg)
			{
				if( msg == "" )
				{
					document.getElementById("sconto").innerHTML = "<center><h1>SCONTO INVIATO SUL TUO INDIRIZZO E-MAIL</h1></center>";
				}else{
					document.getElementById("sconto").innerHTML = "<center><span class='errore'>ERRORE</span> "+msg+"</center>";
				}
				 setTimeout( function() { 
										  document.body.removeChild(document.getElementById("newsSfondoDivSconto"));
										  document.body.removeChild(document.getElementById("newsSfondoSconto")); 
										  }, 4000 );
			},
			error: function(msg)
			{
				document.getElementById("sconto").innerHTML = "<center>ERRORE</center>";
				 setTimeout( function() { 
										  document.body.removeChild(document.getElementById("newsSfondoDivSconto"));
										  document.body.removeChild(document.getElementById("newsSfondoSconto")); 
										  }, 4000 );
			}
		});	
	}
}

function apriBirra(numb)
{
	var newsSfondoDiv = document.createElement("div");
	var newsSfondo = document.createElement("div");
	var news = document.createElement("div");
	var chiudiNews = document.createElement("div");
	
	newsSfondoDiv.id = "newsSfondoDiv";
	newsSfondo.id = "newsSfondo";
	news.id = "news";
	chiudiNews.id = "chiudiNews";
	
	document.body.appendChild(newsSfondoDiv);
	document.body.appendChild(newsSfondo);
	newsSfondo.appendChild(chiudiNews);
	newsSfondo.appendChild(news);
	
	if (navigator.appName != "Microsoft Internet Explorer"){
		newsSfondoDiv.style.opacity=0.8
	}else {
		newsSfondoDiv.style.filter="alpha(opacity=80)"
	}
	
	chiudiNews.innerHTML = "<img src='immagini/x.png' class='chiudiNewsImg'>";
	newsSfondo.style.marginLeft = ((screenW/2)-250)+"px";
	
	newsSfondoDiv.onclick = function(){
		document.body.removeChild(newsSfondoDiv);
		document.body.removeChild(newsSfondo);
	}
	
	chiudiNews.onclick = function(){
		document.body.removeChild(newsSfondoDiv);
		document.body.removeChild(newsSfondo);
	}
	news.innerHTML = "<center>... ATTENDERE PREGO ...</center>";
	$.ajax({
			type: "GET",
			url: "pagine/birre/"+numb+".html",
			data: "",
			success: function(msg)
			{
				news.innerHTML = msg;
			},
			error: function(msg)
			{
				news.innerHTML = "<center>ERRORE: Caricamento Pagina Birra</center>";
			}
		});	
}

function link(id){
	location.href ="#"+id
}

function nulla()
{
}


function CheckLength()
{
	var testo = document.getElementById("textAreaFeed")
	var numcar = document.getElementById("numcar")
	var chars = testo.value
	numcar.innerHTML=maxchars;

	if (chars.length > maxchars)
	{
		testo.value=chars.substr(0,maxchars);
		testo.blur();
	}
	numcar.innerHTML=testo.value.length+"/"+maxchars;
}

