$(document).ready(function(){
	$("#mainNav > li > a").hover(
						function(){
							$("#mainNav li ul").css({ display : 'none' });
							$(this).next().css({ display : 'block' }).hover(
								function(){
									$("#mainNav li ul").css({ display : 'none' });
									$(this).css({ display : 'block' });
									},
								function(){
									$(this).css({ display : 'none' });
									$(".selected").next().css({ display : 'block' });
								});
						},
						function(){
							$(this).next().css({ display : 'none' });
							$(".selected").next().css({ display : 'block' });
	});
	$('a[rel=link]').attr('target','_blank');
	$('#flickr_badge_wrapper a').attr('target','_blank');
});
function xmlHome(){

var currentTime = new Date();
var time = currentTime.getTime();
				$.ajax({
					type: "GET",
					url: "dati/HNE-StreamingHome.xml?cache="+time,
					dataType: "xml",
					success: function(xml) {
								$(".loader").hide();
								var check = false;
								var blocco = '';
								//$('.inOnda')='':
								var titolo = $(xml).find('EVENTO:first TITOLO').text();
								var ora = $(xml).find('EVENTO:first ORA').text();
								var tema = $.trim($(xml).find('EVENTO:first TEMA').text());
								var autore = $(xml).find('EVENTO:first AUTORE').text();
								if ([autore] in oc(['Andrea Delogu','Luca Conti','Eliana Venier','Sebastiano Vitale','Thelma']))
									check = true;
								var descrizione = $(xml).find('EVENTO:first DESCRIZIONE').text();
								blocco += 'in diretta: <br/>';
								blocco += '<b style="color:#000000; font-size:16px">' + titolo + '</b>';
								if (check) {
									var img = autore.replace(/\s/g, "").toLowerCase();
									blocco += '<img src="img/protagonisti/' + img + '.png" alt="" />';
									var link = (autore.replace(/\s/g, "-")).toLowerCase() + '.html';
								}
								//blocco += '<ul><li>Iniziato alle ore: <span>' + ora + '</span></li>';
								
								
								if (tema != '')
									blocco += '<li>Tema: <span>' + tema + '</span></li>';
								
								/*
								if (check)  blocco += '<li>Protagonista: <span><a href="protagonisti/' + link + '">' + autore + '</a></span></li></ul>';
								else 		blocco += '<li>Protagonista: <span>' + autore + '</span></li></ul>';
								
								*/
								blocco += '<p>' + descrizione + '</p>';
								$('.inOnda').html(blocco);
							},
					error: function(){
								$(".loader").hide();
								$('.inOnda').html("<p style='color:red;'>Caricamento dati non riuscito</p>");
							}
				});
}
function oc(a){  // converte l'array in un oggetto - oc = object converter
	var o = {};
	for(var i=0;i<a.length;i++){
		o[a[i]]='';
	}
	return o;
}
function newsletter(){
	$("#nome")
		.focus(function(){
				if ($(this).val() == 'Nome') 
				 	$(this).val(''); 
				})
		.blur(function(){
				if ($.trim($(this).val()) == '')
					$(this).val("Nome");
				});
	$("#cognome")
		.focus(function(){
				if ($(this).val() == 'Cognome') 
				 	$(this).val(''); 
				})
		.blur(function(){
				if ($.trim($(this).val()) == '')
					$(this).val("Cognome");
				});
	$("#confermo").click(function(){
		var c = 0;
		$("form input:text").each(function(){
			$(this).removeClass("ahia");
			var val = $.trim($(this).val());
			$(this).val(val);
			if ($(this).val() == ''){
				$(this).addClass("ahia");
				c = 1;
			} else if (($(this).attr('id') == 'nome') && ($(this).val() == 'Nome')) {
				$(this).addClass("ahia");
				c = 1;
			} else if (($(this).attr('id') == 'cognome') && ($(this).val() == 'Cognome')) {
				$(this).addClass("ahia");
				c = 1;
			}
		});
		if (!(checkMail()) || (c == 1)){
			if (!(checkMail())){
				$("#messaggi").show().text("Il formato dell'email non e' corretto.");
				$("#email").addClass("ahia");
			}
			if ((c == 1)){
				$("#messaggi").show().text("Tutti i campi sono obbligatori.");
			}
		}  else if (!($("#accetto").attr('checked'))){
			$("#messaggi").show().text("E' necessario accettare le condizioni sulla Privacy.");
			check = 1;
		}  else {
			var nome = $.trim($("#nome").val());
			var cognome = $.trim($("#cognome").val());
			var email = $.trim($("#email").val());
			var accetto = $.trim($("#accetto").val());
			$.ajax({
				type: "POST",
  				url: "newsletter.php",
				data: "nome=" + nome + "&email=" + email + "&cognome=" + cognome + "&accetto=" + accetto,
				success: function(ResponseText){
						var check = ResponseText;
						if (check == 1 ) {
							$("#messaggi").show().text("Ti ringraziamo per esserti registrato.");
							$("#confermo").attr("disabled","disabled").addClass("disabled");
						} else 
							$("#messaggi").show().text("Si sono verificati problemi nell'invio dei dati. Riprova piu' tardi.");
					}
			});
		}
		return false;
	});
}
function checkMail(){
	var p = /^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{1,4})$/;
	var e = $('#email').val();
	if (!(p.test(e))){
		return false;
	}
	return true;
}

function tip(){
	$('.palinsesto a').cluetip({
		positionBy: 'mouse',
		topOffset: 50,
		//leftOffset: 150, 
		cursor:'pointer',
		sticky: true,
 		closePosition: 'bottom',
		closeText: 'Chiudi', /*<img src="../img/chiudiTip.png" alt="" />',*/
		width:285,
		activation: 'click',
		dropShadow: false,
		ajaxCache: true,
		showTitle: false
	});
}