function mycarousel_initCallback(){
  jQuery('.carosello-article').css("opacity","1");
  jQuery('.carosello-article').css("filter","alpha(opacity=100)");
}
function resizeImgArticle() {
  $('.gallery_slide_new img').each(function (i) {
		if ($(this).height() < 333) { 
			var margin_top = (333 - $(this).height()) / 2;                                
			$(this).css('margin-top', Math.round(margin_top));
		}
	}); 
}	
function center_Vert(){
  altezza_a = 53;
  //if((navigator.appName).indexOf("Microsoft")>-1) altezza_a = 53;
  $("#thumbs #mycarousel li img").each(function(){
      altezza_img = $(this).height();
      space_top = (altezza_a - altezza_img) / 2;
      if(space_top > 0)  $(this).css("margin-top",space_top);
  });	
}


//openlink
function openlink(xlink){
	window.open("/openxlink.shtml?" + xlink, "xpage");
	}

// script per apertura gallery

function galleriaN(str,str2){
	var larghezza=screen.availWidth;
	if(larghezza>=1024){
		window.open('/gallery/'+str+'/vuoto.shtml?'+str2+'','gallery','width=740,height=670,status=no');
	}else{
		window.open('/gallery/'+str+'/vuoto.shtml?'+str2+'','gallery','width=740,height=540,status=no');
	}
}


//function galleriaVN(str,str2) {
//	var larghezza = screen.availWidth;
//	if (larghezza >= 1024) {
//		window.open('/gallery/' + str + '/vuoto.shtml?' + str2 + '','gallery','width=740,height=670,status=no');
//	} else {
//		window.open('/gallery/' + str + '/vuoto.shtml?' + str2 + '','gallery','width=740,height=540,status=no');
//	}
//}

function galleriaVN(str,str2){
	var larghezza=screen.availWidth;
	if(larghezza>=1024){
		if(str=="Milano") {
			window.open('http://milano.corriere.it/gallery/'+str+'/vuoto.shtml?'+str2+'','gallery','width=740,height=670,status=no');
		}
		else {
			window.open('/gallery/' + str + '/vuoto.shtml?' + str2 + '','gallery','width=740,height=670,status=no');
		}
	}else{
		if(str=="Milano") {
			window.open('http://milano.corriere.it/gallery/'+str+'/vuoto.shtml?'+str2+'','gallery','width=740,height=540,status=no');
		}
		else {
			window.open('/gallery/' + str + '/vuoto.shtml?' + str2 + '','gallery','width=740,height=540,status=no');
		}

	}
}


// script tellafriend

function apriInviaPagina(){
	var miourl=window.location.href;
	var miourl_s=miourl.split("&");
	var loc="";
	for(i in miourl_s)
		loc+=miourl_s[i]+"!*";
	loc=loc.substr(0,loc.length-2)
	tit=document.title
	prop="menubar=no,status=no,titlebar=no,toolbar=no,width=450,height=500,scrollbars=no"
	window.open("http://www.corriere.it/cf/tellafriend/form_veneto.cfm?rr="+loc+"&tit="+tit,"InviaPagina",prop);
}
     
function pr6(nomefile,larghezza,altezza) {
var pathToRemove = "/Corriere della Sera";
var lenPathToRemove = pathToRemove.length;
if (nomefile.indexOf(pathToRemove) > -1) nomefile = nomefile.substring(lenPathToRemove);
if (nomefile.indexOf(".xml") > -1)  nomefile = nomefile.substring(0, nomefile.indexOf(".xml")) + ".shtml";
	parametri="menubar=no,location=no,toolbar=no,status=no,scrollbars=yes,resizable=no,width="
	parametri=parametri+larghezza+",height="+altezza
	newWin=open('','',parametri)
	newWin.location.href=nomefile
}          	
	/*


DESCRIZIONE: 
viene visualizzata la situazione metereologica 
i dati sono estratti da file XML
*/

function dammiInfoLocalita(var_localita) {	

	$.get (
		"/tools/includes/meteo/xml/meteo.xml", function(xml) {
		
			$("loc",xml).each(
				function() {
					
					//leggo localita
					var var_localita_id = $(this).attr("id");
					if (var_localita_id == var_localita)
					{			
						//leggo i dati
						var_localita = $(this).attr("id"); //id
						
	
						
						//var_momento = $(this).find("momento").text(); //momento
						var_temperatura_misurata = $(this).find("temperatura").find("misurata").text(); //temperatura_misurata
						var_valore_umidita = $(this).find("umidita").text(); //umidita
						var_immagine = $(this).find("tempo").text(); //tempo
						//
					
						//effettuo controlli in caso di dati non presenti NP

						if (var_valore_umidita!="NP")
						{	
							var_valore_umidita = var_valore_umidita+"%";
						}

						if (var_temperatura_misurata!="NP")
						{	
							var_temperatura_misurata = var_temperatura_misurata+"&deg;C";
						}

						//
						var_immagine = var_immagine.replace(" ", "_");
						var_immagine = var_immagine.replace("_/ ", "_");
					
						$('#mn_meteo img').attr('src','http://corrieredelveneto.corriere.it/libs/css/default_theme/assets/meteo/'+ var_immagine +'.png');				
					
						$("#mn_meteo #temperatura").html(var_temperatura_misurata);	
												
						//

					}//chiuso if
				}//chiuso function
			);//chiuso each			
		}//chiuso function
	); //chiuso get
}

// gestione combo date per ricerca agenda (eventi e teatri)
function daysInMonth(month, year) {
	return 32 - new Date(year, month, 32).getDate();
}

function setDatesFields(form) {
	
	var now = new Date();
	var daData, aData;
	var pad = function(d) {
		return d < 10 ? "0" + d : d + "";
	}
	var setDay = function(d, d2) {
		
		var date = new Date(now.getFullYear(), now.getMonth(), d);
		var date2 = d2 ? new Date(now.getFullYear(), now.getMonth(), d2) : null;
		
		daData = String(pad(date.getDate())) + String(pad(date.getMonth()+1)) + String(date.getFullYear());
		aData = d2 ? String(pad(date2.getDate())) + String(pad(date2.getMonth()+1)) + String(date2.getFullYear()) : daData;
	}
	var c = parseInt(form.when.value, 10);
	
	switch (c) {
		
		// today
		case 0:
			setDay(now.getDate());
			break;
		
		// tomorrow	
		case 1:
			setDay(now.getDate() + 1);
			break;
		
		// sunday	
		case 8:
			setDay(now.getDay() == 0 ? now.getDate() : now.getDate() + (7 - now.getDay()));
			break;
			
		// next week	
		case 9:
			var d = now.getDate() + (now.getDay() == 0 ? 0 : (7 - now.getDay()));
			setDay(d+1, d+7);
			break;
		
		// this month	
		case 10:
			var d1 = new Date(now.getFullYear(), now.getMonth() + 1, now.getDate());
			var d2 = new Date(now.getFullYear(), now.getMonth() + 1, daysInMonth(now.getMonth(), now.getFullYear()));
			
			daData = pad(d1.getDate()) + pad(d1.getMonth()) + d1.getFullYear();
			aData = pad(d2.getDate()) + pad(d2.getMonth()) + d2.getFullYear();
			break;
		
		// next month	
		case 11:
			var d1 = new Date(now.getFullYear(), now.getMonth() + 2, 1);
			var d2 = new Date(now.getFullYear(), now.getMonth() + 2, daysInMonth(now.getMonth() + 1, now.getFullYear()));
			
			daData = pad(d1.getDate()) + pad(d1.getMonth()) + d1.getFullYear();
			aData = pad(d2.getDate()) + pad(d2.getMonth()) + d2.getFullYear();
			
			break;

		// all date
		case 12:
			setDay(now.getDate());
			// imposto la data massima
			//var d1 = new Date(9999, 12, 31);
			aData = ""; //pad(d1.getDate()) + pad(d1.getMonth()) + d1.getFullYear();
			break;
		// all week
		default:
			var requiredDay = c - 1;
			var dayDiff = Math.abs(now.getDay() - requiredDay);
			
			setDay(requiredDay < now.getDay() ? (now.getDate() + (7 - dayDiff)) : (now.getDate() + dayDiff));
		
	}
	
	form.daData.value = daData;
	form.AData.value = aData;
			 

	return checkFields(form);
	
}

function checkFields(form) {
	
	if (form.testo.value != "" && form.testo.value.length < 3) {
		jAlert('Inserire almeno 3 caratteri nel campo testo', 'Attenzione');
		//alert("Inserire almeno 3 caratteri nel campo testo")
		return false;
	} else if ( parseInt(form.when.value, 10) == 12 && parseInt(form.idTipologia.value, 10) == 0) {
		jAlert('Se selezioni tutte le date devi selezionare anche una tipologia', 'Attenzione');
		//alert("Se selezioni tutte le date devi selezionare anche una tipologia")
		return false;	
	}
	return true;
	
}

function checkFieldsLocali(form) {
	if (form.tipologie.value == "" && form.nome.value.length < 3 && form.vicinoa.value.length < 3){
		var stringalert = "Selezionare almeno un criterio di ricerca. Inserire almeno 3 caratteri nel campo di ricerca."
		jAlert(stringalert, 'Attenzione');
		//alert(stringalert)
		return false;
	}
	return true;
}

function checkFieldsSport(form) {
	if (form.nome.value.length < 3 && form.vicinoa.value.length < 3 && form.tipologie.length < 3){
		var stringalert = "Compilare almeno un criterio di ricerca. Inserire almeno 3 caratteri nel campo di ricerca."
		jAlert(stringalert, 'Attenzione');
		//alert(stringalert)
		return false;
	}
	return true;
}

function checkFieldsRistoranti(form) {
	if (form.tipologie.value == "" && form.nome.value.length < 3 && form.vicinoa.value.length < 3 && form.fasciaprezzo.value == ""){
		var stringalert = "Selezionare almeno un criterio di ricerca. Inserire almeno 3 caratteri nel campo di ricerca."
		jAlert(stringalert, 'Attenzione');
		//alert(stringalert)
		return false;
	}
	return true;
}


$(document).ready(function() {
	
	//$('#mn_(none)').addClass("selected");
	//var localita = $(document).getURLParam("localita"); //leggo il nome della localitÃ  passato da URL

	
	//console.log(var_citta)
	var_citta = meteosezione;
	
	if(var_citta == 'veneziamestre' || var_citta == 'veneto') {
		var_citta = 'venezia';
	};
	
	if(meteosezione != 'undefined') {
	
		dammiInfoLocalita(var_citta);//richiamo la funzione per la lettura dei dati d'interesse 
		buildCarBox(carSezione);
	};
	
	
	$("#smaller-text").click(function(){
		var size=parseInt($(".article p").css("font-size"))-2+"px";
		if(parseInt(size)>=10){
			$(".article p").css("font-size",size);
		}
		return false;
	});
	$("#bigger-text").click(function(){
		var size=parseInt($(".article p").css("font-size"))+2+"px";
		if(parseInt(size)<26){
			$(".article p").css("font-size",size);
		}
		return false;
	});
	
	// login registrati

	var usn=$.cookie("clLogin");
	if(usn!=null){

		usn=usn.substring(0,usn.indexOf("|"));
		$("#user-name").html(usn);
		// non utilizzare toggle per non spaginare.
		// imposto il diplay:inline direttamente..
		$("#headBoxLogin span.notlogged").css("display", "none");
		$("#headBoxLogin span.logged").css("display", "inline");
	}
	$("#headBoxLogin a.headUsn").click(function(){
		$("#formModifica").submit();
		return false;
	});



	$("ul.italiaoggi li a").attr("target","_blank")


	$(".input-sbianca").click(function() {
		if($(this).val() != "") {
			$(this).val(""); //Svuoto il contenuto.
		}
	});
	
	// link nel box cosa vuoi cercare (seconda colonna) che scatena la ricerca agenda per tipologia
	$(".linkTip").click(function () {
		var idTipologia = $(this).attr("rel").substring(1);
		$(".search_agenda select[name='idTipologia']").val(idTipologia);
		$(".search_agenda input[type='submit']").click();
		return false;
	});
	
	// preselezione del form ricerca agenda in base alla variabile evento dell'agenda
	var sel;
	switch ($("body:first").attr("class")) {
		
		case "eventi":
			sel = 0;
			break;
			
		case "ristoranti":
			sel = 1;
			break;
		
		case "locali":
			sel = 2;
			break;
			
		case "cinema":
			sel = 3;
			break;
			
		case "teatri":
			sel = 4;
			break;
		
		case "sport-benessere":
			sel = 5;
			break;
			
		default:
			sel = 0;
	
	}
	
	// tabs per agenda e annunci
	$('#tabs-agenda').tabs({ selected: sel });
	$('#tabs-agenda').css("display","block");
	$("#tabs-annunci").tabs();
	

	// paginazione risultati agenda (eventi e teatro)
	$("#ris-eventi").quickPager({
		leftArrowObject: "<img width='13' height='10' src='/libs/css/default_theme/assets/freccia_sx.gif'/>",
		rightArrowObject: "<img width='13' height='10' src='/libs/css/default_theme/assets/freccia_dx.gif'/>",
		handlePageClass: false
	});	
	$("#ris-entita").quickPager({
		leftArrowObject: "<img width='13' height='10' src='/libs/css/default_theme/assets/freccia_sx.gif'/>",
		rightArrowObject: "<img width='13' height='10' src='/libs/css/default_theme/assets/freccia_dx.gif'/>",
		scrollAnimate: false
	});           
	$("#ris-date").quickPager({
		leftArrowObject: "<img width='13' height='10' src='/libs/css/default_theme/assets/freccia_sx.gif'/>",
		rightArrowObject: "<img width='13' height='10' src='/libs/css/default_theme/assets/freccia_dx.gif'/>",
		handlePageClass: true,  
		scrollAnimate: false
	});

	
	// setta il title uguale al value nei campi della form con classe set-title
	$("form input.set-title").each(function() {
		$(this).attr("title", $(this).attr("value"));
	});
 	
	// al submit controlla e sbianca il value se è quello di default (identico al title)
	$("form:has(input.set-title)").find(".submit").click(function () {
 	 	$(".set-title").each(function() {
			if ($(this).attr("title") == $(this).attr("value")) 
				$(this).attr("value", "");
		});
	});

	// gestione link ticketone.
	// sono tutti nascosti da css, bisogna visualizzare nella scheda solo l'evento della data selezionata (param. url)
	if ($('#agenda_id').length > 0) {	
		if ($('#agenda_id').text() == "id000") {
			
			// gestione "DOVE" per eventi con data multipla
			$(".item-info:first").addClass("item-info-selected")
			
			//var linkBack = $('#back-to-scheda').attr("href") + "?ageId=" + $('#agenda_id').html();
			//$("#back-to-scheda").attr("href", linkBack);
			
		} else {
	
			$(".link-acquista a").each(function() {
				if ($(this).attr("id") == $('#agenda_id').html()) {
					$(this).parent().css("display", "block");
				}
			});    
			
			// passa il parametro a vedi tutte le date
			var linkTutte = $('.vedi-tutte').attr("href") + "?ageId=" + $('#agenda_id').html();
			$('.vedi-tutte').attr("href", linkTutte);
			
			// gestione "DOVE" per eventi con data multipla
			$(".item-info").each(function() {	
				if ($(this).attr("id") == "evento_" + $('#agenda_id').html()) {
					$(this).addClass("item-info-selected");
				} 
			});  
			
			if ($('#back-to-scheda').length > 0) {
				var realPath = location.pathname.split('/')
				var backLink = $('#back-to-scheda').attr('href').split('/')
				var newBackLink = "/" + backLink[1] + "/" + realPath[2] + "/" + backLink[4] + "?ageId=" + $('#agenda_id').html();
				$('#back-to-scheda').attr('href',newBackLink)
			}	
			
			//var linkBack = $('#back-to-scheda').attr("href") + "?ageId=" + $('#agenda_id').html();
			//$("#back-to-scheda").attr("href", linkBack);
		}
	}
	
	
	
	//fix per z-index ie6 
	$('.layer-indirizzo').bgiframe();
	
	$('.infobox').bgiframe();
	
	//submit del form con invio sulla select
	$(".tabs-agenda select").keyup(function(event){
		if (event.keyCode == 13) {
			$(this).parent("form").find("input.submit").click();
		}
	});
	
	// layer per l'inserimento dell'indirizzo per la form di ricerca agenda, campo "vicino a "
	$(".display-layer-indirizzo").focus(function() {
		$(this).parents("form").find(".layer-indirizzo").css("display", "block");									 
	});
	$(".close-layer-indirizzo").click(function() {
		$(this).parent().css("display", "none");									 
	});
	
	$(".hide-layer-indirizzo").click(function() {
		var root = 	$(this).parent();							  
		root.css("display", "none");
		var valore = ($("input[name='toponimo']", root).attr("value"));
		valore += " " + ($("input[name='nomevia']", root).attr("value"));
		valore += " " + ($("input[name='civico']", root).attr("value"));
		valore += " " + ($("input[name='citta']", root).attr("value"));
		root.parent().find(".display-layer-indirizzo").attr("value",valore);
	});
	

	if($('.article-carousel').length > 0){
		jQuery('.article-carousel').jcarousel({
			initCallback:   mycarousel_initCallback
		});
	};

	// GALLERIA ARTICOLO
	if($('.gallery_slide_newl').length > 0){
		resizeImgArticle();
		$('.gallery_slide_new').cycle({ fx: 'scrollLeft'});
	};

});

	