//funzione per caricare la mappa di google
function initialize_map() {
	var initialLocation= new google.maps.LatLng(43.797267,11.221245);
	
	var myOptions = {
		zoom: 15,
		center: initialLocation,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	var map = new google.maps.Map(document.getElementById("map"), myOptions);
	// var infowindow = new google.maps.InfoWindow();
	var marker = new google.maps.Marker({
		position: initialLocation,
		animation: google.maps.Animation.DROP,
		map: map
    });
	// var service = new google.maps.places.PlacesService(map);
	// request = {reference:};
	// service.getDetails(request, function(place, status) {
		// if (status == google.maps.places.PlacesServiceStatus.OK) {
			// var marker = new google.maps.Marker({
				// map: map,
				// position: place.geometry.location
			// });
			// google.maps.event.addListener(marker, 'click', function() {
				// infowindow.setContent(place.name);
				// infowindow.open(map, this);
			// });
		// }
	// });
}

//funzione di movimento del riquadro share
function share_frame(){
	larghezza=$(document).width();
	questo=$("#share");
	if (larghezza<1170){
		questo.detach().appendTo("#serviziBusiness");
		questo.css({position: "relative", right: "0px"});
	} else {
		questo.detach().appendTo("#container");
		questo.css({position: "absolute", right: "-88px"});
	}
}

//funzione per cambiare password-text
function password_change(){
	html = '<input class="text" type="password" name="password" id="password" value="" />';
	$('#password').after(html).remove();
	$('#password').focus();
	$('#password').blur(function(){
		if(this.value==''){
			html = '<input class="text" type="text" name="password" id="password" value="Password" />';
			$(this).after(html).remove();
			$('#password').focus(function(){ 
				password_change();
			});
		}
	});
}

//ajax per richiamare province e comuni
function setProvincia(provincia){
	$.ajax({
		url: location.href,
		data: 'provincia='+provincia,
		type: 'POST',
		success: function(data){
			$("#comune").html(data);
		}
	});

	$("#cap").val('');
}
function setComune(comune){
	$.ajax({
		url: location.href,
		data: 'comune='+comune,
		type: 'POST',
		success: function(data){
			$("#cap").val(data);
		}
	});
}

//funzioni di controllo per campi
function checkInteger(number, maxnum){
	if (maxnum==4){
		var filter=/^([0-9]){4}/;
	} else if (maxnum==3) {
		var filter=/^([0-9]){3}/;
	} else {
		var filter=/^([0-9])+$/;
	}
	if (!filter.test(number)){return false;}else{return true;}
}

function checkEmail(email) {
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email)) {return false;}else{return true;}
}
function checkCodFiscale(codice){
	var filter = /^([a-zA-Z]){6}([0-9]){2}([a-zA-Z]){1}([0-9]){2}([a-zA-Z]){1}([0-9]){3}([a-zA-Z]){1}$/;
	if (!filter.test(codice)) {return false;}else{return true;}
}



$(document).ready(function(){
	if ($('#slider').length>0){
		$('#slider').nivoSlider({
			pauseTime: 5000,
			prevText: '',
			nextText: '',
			directionNavHide: false
		});
	}
	$('#header li.menu ul').hide();
	
	$('#header li.menu').mouseenter(function(){
		if (($(this).attr("id")!="logo")&&($(this).attr("id")!="currentPage")){
			$('#header li.menu').removeClass("hover");
			$(this).addClass("hover").css({backgroundPosition:"0px -80px"}).stop(true, true).animate({backgroundPosition: "0px 0px"}, 300);
		}	
		$(this).find('ul').stop(true, true).fadeIn();
	}).mouseleave(function(){
		if (($(this).attr("id")!="logo")&&($(this).attr("id")!="currentPage")){
			element=$(this);
			$(this).stop(true, true).animate({backgroundPosition: "0px -80px"}, 300, function(){$(this).removeClass("hover")});
		}
		$(this).find('ul').stop(true, true).fadeOut();
	}).click(function(){
		// location.href=($(this).find("a").eq(0).attr("href"));
	});
	if ($('#map').length>0){
		initialize_map();
	}
	
	if ($('#login').length>0){
		$('#user').focus(function(){
			if ($(this).val()=='Email')
				this.value='';
		}).blur(function(){
			if(this.value=='')
				this.value='Email';
		});

		$('#password').focus(function(){ 
			password_change();
		});
	}
	
	if ($("#share").length>0){
		share_frame();
		$(window).scroll(function(){
			if ($(window).scrollTop()>110){
				newtop=$(window).scrollTop()-50;
				maxnewtop=$("#container").height()-$("#share").height();
				if ($("#share").css("position")=="absolute"){
					if (newtop<maxnewtop){
						$("#share").css({top: newtop+"px"});
					} else {
						$("#share").css({top: maxnewtop+"px"});
					}
				} else {
					$("#share").css({top: "0px"});
				}
			} else {
				$("#share").css({top: "30px"});
			}
		});
		$(window).resize(function(){
			share_frame();
		});
	}
	
	
	if ($("#pagamento").length>0){
	
		//campi intero solo numeri
		$("input.intero").keypress(function(event){
			if ( event.keyCode == 46 || event.keyCode == 8 ) {
			
			} else {
				if ((event.which  < 48 || event.which  > 57)){
					event.preventDefault();
				}
			}
		});
		//campi carta di credito
		$("#carta1").keyup(function(){
			if ($(this).val().length>3){
				$("#carta2").focus();
			}
		});
		$("#carta2").keyup(function(){
			if ($(this).val().length>3){
				$("#carta3").focus();
			}
		});
		$("#carta3").keyup(function(){
			if ($(this).val().length>3){
				$("#carta4").focus();
			}
		});
		
		//selezione sui campi
		$("input[type='text']:not(.submit2)").focus(function(){
			this.select();
		});

		
		$("#pagamento").submit(function(){
			var errori=0;
			var scrivi="";

			$("input.obbligatorio").each(function(){
				$(this).removeClass("errore");
				if ($(this).val()==''){$(this).addClass("errore"); errori=1;}
			})

			$("select.obbligatorio").each(function(){
				$(this).removeClass("errore");
				if ($(this).val()=='0'){$(this).addClass("errore"); errori=1;}
			})

			$("input.intero").each(function(){
				if (checkInteger($(this).val(), $(this).attr("maxlength"))===false) {
					$(this).addClass("errore"); 
					errori=1;
				}	
			});
			$("input.codice_fiscale").each(function(){
				if (checkCodFiscale($(this).val())===false) {
					$(this).addClass("errore");
					errori=1;
				}	
			});
			
			if(checkEmail($("#email").val())===false) {
				$("#email").addClass("errore"); 
				errori=1;
			}

			$("#error").hide();

			if (errori>0) {
				$("#error").show();
				scrivi="Completa correttamente i campi obbligatori<br /><br />";
				$("#error").html(scrivi);
			}

			if ($("input[name='privacy']:checked").val()==0 || $("input[name='termini']:checked").val()==0) {
				errori=2;
			}

			if (errori==2){
				$("#error").show();
				scrivi+="&Egrave; necessario accettare le privacy policy e i termini e le condizioni d'acquisto e d'utilizzo";
				$("#error").html(scrivi);
			}
			
			return (errori==0);
		});
	}
		
		
	if ($(".glossario").length>0){
		$(".glossario").mouseenter(function(){
			elemento=$(this);
			elemento.append("<div class='dizionario'></div>");
			$.ajax({
				url: elemento.attr("href"),
				success: function(data){
					elemento.find(".dizionario").empty().html(data);
				}
			})
		}).mouseleave(function(){
			elemento=$(this);
			elemento.children().remove();
			
		}).click(function(){
			return false;
		});
	}	
		
		
});

	function checkform(num) {
		var errore=new Array(0,0,0,0);

		function checkEmail(email) {
			var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
			if (!filter.test(email)) {return false;}else{return true;}
		}

		if (document.getElementById('nome').value=='') {
			document.getElementById('anome').innerHTML=' - Campo Obbligatorio';
			document.getElementById('nome').style.border='1px solid #ff0000';
			errore[0]=1;
		} else {
			document.getElementById('anome').innerHTML='*';
			document.getElementById('nome').style.border='1px solid #E4E4E4';
			errore[0]=0;
		}

		if (document.getElementById('cognome').value=='') {
			document.getElementById('acognome').innerHTML=' - Campo Obbligatorio';
			document.getElementById('cognome').style.border='1px solid #ff0000';
			errore[1]=1;
		} else {
			document.getElementById('acognome').innerHTML='*';
			document.getElementById('cognome').style.border='1px solid #E4E4E4';
			errore[1]=0;
		}

		if (document.getElementById('email').value=='' || checkEmail(document.getElementById('email').value)===false) {
			document.getElementById('aemail').innerHTML=' - Campo Obbligatorio';
			document.getElementById('email').style.border='1px solid #ff0000';
			errore[2]=1;
		} else {
			document.getElementById('aemail').innerHTML='*';
			document.getElementById('email').style.border='1px solid #E4E4E4';
			errore[2]=0;
		}

		if (document.getElementById('messaggio').value=='') {
			document.getElementById('amessaggio').innerHTML=' - Campo Obbligatorio';
			document.getElementById('messaggio').style.border='1px solid #ff0000';
			errore[3]=1;
		} else {
			document.getElementById('amessaggio').innerHTML='*';
			document.getElementById('messaggio').style.border='1px solid #E4E4E4';
			errore[3]=0;
		}

		if (errore[0]==0 && errore[1]==0 && errore[2]==0 && errore[3]==0) {document.forms[num].submit();}
	}

