/* FILMATO YOUTUBE */
function youtube(id,W,H,container) {
	var params = {
		allowfullscreen: 'true',
		allowscriptaccess: 'sameDomain',
		wmode: 'transparent'
	};
	var attributes = {
		id: container,
		name: 'YOUTUBE'
	};
	
	swfobject.embedSWF('http://www.youtube.com/v/'+id+'&showinfo=0&fs=1', container, W, H, '9.0.0', false, false, params, attributes);
}

/* GOOGLE MAPS */
var markers = [];
var counter=0;
var map;

function setTo(lat,lon){
	var centro = new GLatLng(lat, lon);
	map.setCenter(centro, 18);
}

function setToGe(lat,lon,zoom){
	var centro = new GLatLng(lat, lon);
	map.setCenter(centro, zoom);
}

function initialize() {
	if (GBrowserIsCompatible()) {      	
		map = new GMap2(document.getElementById("map_canvas",{mapTypes:[G_HYBRID_MAP,G_NORMAL_MAP,G_SATELLITE_MAP]}));
		map.setMapType(G_HYBRID_MAP);
		
		//var centro = new GLatLng(38.1175926, 13.3662347);
		var centro = new GLatLng(44.406982, 8.932429);
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.addControl(new GScaleControl());
		map.setCenter(centro, 14);
	}
}

function addPoint(address,title,body){
	geocoder = new GClientGeocoder();   
	geocoder.getLatLng(address,
		function(point) {
		  if (!point) {
			errors[counter]= address+"//";
			counter++;
		  } else {
			var marker = new GMarker(point,{'title': title});			        			        
			marker.bindInfoWindowHtml(body);
			markers[counter]=marker;
			counter++;
			map.addOverlay(marker);
		  }
		}
	);
}

function addBig(lat,lng,title,body){
	var point = new GLatLng(lat,lng);
	var blueIcon = new GIcon(G_DEFAULT_ICON);
	blueIcon.image = "http://festival.ariadne.it/contents/instance1/images/markers/markerB.png";
	markerOptions = { 'icon':blueIcon , 'title': title};
	var marker = new GMarker(point,markerOptions);
	marker.bindInfoWindowHtml(body);
	map.addOverlay(marker);
}

function addInfo(lat,lng,title,body){
	var point = new GLatLng(lat,lng);
	var blueIcon = new GIcon(G_DEFAULT_ICON);
	blueIcon.image = "http://festival.ariadne.it/contents/instance1/images/markers/markerI.png";
	markerOptions = { 'icon':blueIcon , 'title': title};
	var marker = new GMarker(point,markerOptions);
	marker.bindInfoWindowHtml(body);
	map.addOverlay(marker);
}

function addLatLng(lat,lng,title,body,num){
	var point = new GLatLng(lat,lng);
	var point = new GLatLng(lat,lng);
	var blueIcon = new GIcon(G_DEFAULT_ICON);
	blueIcon.image = "http://festival.ariadne.it/contents/instance1/images/markers/marker"+num+".png";  			
	var marker = new GMarker(point,{'title': title,'icon':blueIcon});
	marker.bindInfoWindowHtml(body);
	map.addOverlay(marker);
}

/* MAPPA DEI POLI */
$(function(){
	$('#art-poli-container').hide();
	$('.art-poli').each(function(i){
		$(this).css({'display':'none','z-index':i,'position':'absolute','left':0,'top':0});
		$(this).addClass('.art-poli-js');
		$(this).css('z-index',i);
	});
});

function setPoliPoint(idart) {
	$('#art-poli-container').show();
	$('.art-poli').each(function(){
		if($(this).attr('id') != idart){
			$(this).fadeOut('fast');
			$(this).next('a').hide();
		} else {
			$('#art-poli-container').height($('#'+idart).outerHeight());
			$(this).fadeIn('slow');
			$(this).next('a').show();
			$(this).next('a').css({'position':'absolute','left':($('#'+idart).outerWidth())+3,'top':0});
		}
	});
}

/* QUESTIONARI */
$(function(){
	$('#modulo').submit( function(){
		if($('#modulo input[name="privacy"]:checked').val()=='Y'){
			return true;
		} else {
			return false;	
		}
	});
});

/* AREA RISERVATA */
$(function(){
	$('#reset').click(function(){
		var form_id = $(this).parents('form').attr('id');
		$('#'+form_id).find('input:text, input:password').each(function(){
			$(this).attr('value','');
		});
	});
});

/* SLIDE IMMAGINI HOMEPAGE */
function slideTopImg() {
	var i=1;
	
	$('#img-top-container img.img-top-home:first').show();
	if ($('#img-top-container img.img-top-home').length > 1) {
		var timer = setInterval(function(){
			$('#img-top-container img.img-top-home:visible').fadeOut(2000);
			if ( i >= $('#img-top-container img.img-top-home').length) i = 0;
			$('#img-top-container img.img-top-home:eq('+i+')').fadeIn(2000);
			i++;
		},6000);
	}
}



/* AGGIUNGE UNA PAGINA NEI PREFERITI */								
function preferiti(titolo,indirizzo) {
	title =titolo;
	url = indirizzo;
	if (window.sidebar) {
	// Preferiti per Mozilla Firefox 
	window.alert('Clicca con il tasto destro del mouse e scegli Aggiungi pagina nei segnalibri\noppure premi Ctrl D')
	} else if( window.external ) {
	// Preferiti per MSIE 
	window.external.AddFavorite(url, title);
	}
	// Preferiti per altri browser
	else {window.alert('Aggiungi questo sito ai preferiti')
	}
}

/* COMMENTI */
$(function(){
	$('#formcommenti').submit( function(){
		if($.trim($('#formcommenti input[name="name"]').val()) == '') {
			alert('il campo Nome deve essere valorizzato');
			return false;
		} else if ($.trim($('#formcommenti input[name="email"]').val()) == '')  {
			alert('il campo email deve essere valorizzato');
			return false;
		} else  {
			return true;
		}
	});
	
	$('.open-comment').click(function(){
		$(this).next('div:first').slideToggle('normal');
	});
});

function slideTopImgNew() {
	var i=1;
	
	$('#img-top-container img.img-top-home:first').show();
	
	if ( topimg.length > 1) {
		$('#img-top-container').append('<img src="'+topimg[2]+'" id="topimg2" class="img-top-home hide"/>');
		var timer = setInterval(function(){
			$('#img-top-container img.img-top-home:visible').fadeOut(2000);
			if ( i >= topimg.length-1) i = 0;
			i++;
			$('#topimg'+i).fadeIn(2000);
			var next = i+1;
			if (!$('#topimg'+next).length>0) {
				$('#img-top-container').append('<img src="'+topimg[next]+'" id="topimg'+next+'" class="img-top-home hide"/>');
			}
		},5000);
	}
}
/* NEWS TICKER DELLA NUOVA HOME*/
var newsitems;
var curritem=0;
var previtem;

$(document).ready(function(){
	/* controllo per far stampare il fix sui PNG solo se sono in HOME 2010 */
	var boxnewsection=$('.newsection');
	if (boxnewsection.length >0) {
		$(document).pngFix();
		newsitems = $("#ticker li").hide().size();
		$("#ticker li:eq("+curritem+")").fadeIn(3000);
		$("#news-0").addClass('newsitemactive');
		setInterval(ticknews,9000); //time in milliseconds
		$(".newsticker").click(function () {
			var idtick=$(this).attr('id');
			var subidtick=idtick.substr(5,1);
			ticknewsX(subidtick);
		});
	}
});

function ticknews() {
	$("#ticker li:eq("+curritem+")").fadeOut(2000); 
	curritem = ++curritem%newsitems;
	$("#ticker li:eq("+curritem+")").fadeIn(3000);
	previtem=curritem-1;
	
	if(previtem==-1){
		var lastnews=newsitems-1;
		$("#news-"+lastnews).removeClass('newsitemactive');
		$("#news-"+curritem).addClass('newsitemactive');
	}
	else{
		$("#news-"+previtem).removeClass('newsitemactive');
		$("#news-"+curritem).addClass('newsitemactive');
	} 
} 
function ticknewsX(idnews) {
	$("#ticker li:eq("+curritem+")").hide(); 
	
	$("#news-"+curritem).removeClass('newsitemactive');
	$("#news-"+idnews).addClass('newsitemactive');
	
    curritem = idnews;
    $("#ticker li:eq("+curritem+")").show();
	  $("#ticker li:eq("+curritem+")").stopTime();
	
} 

function LinkEvento(idevento) {
	location.href = "http://www.festivalscienza.it/site/fscienza/matchevent.jsp?id="+idevento;
}
jQuery.preloadImages = function() {
	var a = (typeof arguments[0] == 'object')? arguments[0] : arguments;
	for(var i = a.length -1; i > 0; i--)
	{
	    jQuery("<img>").attr("src", a[i]);
	}
}
$(function(){
	if ($('#slider').length >0) {
	
		//preloader
		var img_gal = [];
		$('.gallery-home').each(function(){
		    img_gal.push($(this).html());
		});
		$.preloadImages(img_gal);
		var maxcounter = img_gal.length +2;
		var icounter = 3;
		
		//parte lo slide
		setInterval(function(){
			$('#slider :first-child').show();
			$('#slider :first-child').fadeOut(3000).siblings(':first.spot').fadeIn(3000,function() { 
				var nextsrc = $('#fh-'+icounter).html();
				$('#slider :first-child').attr("src",nextsrc);
				$('#slider :first-child').appendTo('#slider');
				if (icounter==maxcounter) {
					icounter = 1;
				} else {
					icounter++;
				}
		})},5000);
	}
});
		
