$(function(){
	
	$("#search").click(function () {
		$("#search-popup").toggle();
    });
	
	//ricerca nel sito
	$('#g_q').click(function () {
		$(this).attr("value","");
	});
	
});



/* 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;
var mapobj;
overlayObj = "#overlay";
overlayWrapper = ".overlay-wrapper";
overlayContent = "#overlay-content";
overlayClose = ".overlay-close";


function setTo(lat,lon){
	var centro = new google.maps.LatLng(lat, lon);
	map.setCenter(centro);
	map.setZoom(19);
}

function setToGe(lat,lon,zoom){
	var centro = new google.maps.LatLng(lat, lon);
	map.setCenter(centro);
	map.setZoom(zoom);
}

function setLayer(kml) {
	var georssLayer = new google.maps.KmlLayer(kml);
	georssLayer.setMap(map);
}

function initialize() {
        var latlng = new google.maps.LatLng(44.406982, 8.932429); //SETTO LA MAPPA SU GENOVA
	var myOptions = {
		zoom: 14,
		center: latlng ,
		mapTypeId: google.maps.MapTypeId.HYBRID,
		mapTypeControl: true
	};
	map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);
}

function addBig(lat,lng,title,body){
	var myLatlng = new google.maps.LatLng(lat,lng);
	var infowindow = new google.maps.InfoWindow({
		content: body
	});
	var marker = new google.maps.Marker({
		position: myLatlng, 
		map: map,
		icon: "http://festival.ariadne.it/contents/instance1/images/markers/markerB.png",
		title:title
	});
	google.maps.event.addListener(marker, 'click', function() {
	      infowindow.open(map,marker);
	});
	/*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 myLatlng = new google.maps.LatLng(lat,lng);
	var infowindow = new google.maps.InfoWindow({
		content: body
	});
	var marker = new google.maps.Marker({
		position: myLatlng, 
		map: map,
		icon: "http://festival.ariadne.it/contents/instance1/images/markers/markerI.png",
		title: title
	});
	google.maps.event.addListener(marker, 'click', function() {
	      infowindow.open(map,marker);
	});
	
	/*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 myLatlng = new google.maps.LatLng(lat,lng);
	var marker = new google.maps.Marker({
		position: myLatlng, 
		map: map,
		icon: "http://festival.ariadne.it/contents/instance1/images/markers/marker"+num+".png",
		title: title
	});
	var infowindow = new google.maps.InfoWindow({
		content: body
	});
	google.maps.event.addListener(marker, 'click', function() {
	      infowindow.open(map,marker);
	});
	
	/*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);*/
}




function overlay(obj,container,lat,lng,zoom) {

	oggetto = $(obj);
	mapobj = $(container);
	var contenuto = oggetto.html();
	objClose = "<a href='javascript:void(0);' class='overlay-close'>&#160;</a>";
	screenWidth = $(window).width();
	screenHeight = $(window).height();
	documentHeight = $(document).height();
	scrolltop = $(window).scrollTop();		
	$(overlayContent).html("");
	$(overlayContent).html(contenuto);
	$(overlayContent).after(objClose);

	var sinistra = (screenWidth - $(overlayWrapper).width()) / 2 + "px";
	var alto = (screenHeight - $(overlayWrapper).height()) / 2;
	var difference = scrolltop + alto;
	$(overlayObj).css("height",documentHeight);
	$(overlayObj).fadeIn('fast',function(){
		$(overlayWrapper).css("left",sinistra);
		$(overlayWrapper).css("top",difference);
		$(overlayWrapper).show();
	});

	$(document).keyup(function(event) {
		if ( event.which == 27 ) {
			closeOverlay();
		}
	});
	
	$(overlayClose).click(function() {
		closeOverlay();
	});
	
	function closeOverlay() {
		mapobj.css("top","-1000px"); //NASCONDO LA MAPPA
		$(overlayWrapper).fadeOut('fast',function(){
			$(overlayObj).hide();
			$(overlayClose).remove();
		});

	}
	view_map('mappa-evento',lat,lng,zoom);
}

function view_map(container,lat,lng,zoom) {
	var latlng = new google.maps.LatLng(lat, lng); //SETTO LA MAPPA SU GENOVA
	var myOptions = {
		zoom: zoom,
		center: latlng ,
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		mapTypeControl: true
	};
	map = new google.maps.Map(document.getElementById(container), myOptions);
	var marker = new google.maps.Marker({
		position: latlng, 
		map: map
	});
	
	google.maps.event.addListener(map,"tilesloaded",function(){
		// AL CARIMENTO DELLA MAPPA LA SPOSTO
		if( $(overlayObj).is(":visible") == false ){
			mapobj.css("top","-5000px");
		}
		var mappaTop = scrolltop + (screenHeight - $(overlayWrapper).height()) / 2 + 40;
		var mappaLeft = (screenWidth - $(overlayWrapper).width()) / 2 + 40;
		mapobj.css("top",mappaTop);
		mapobj.css("left",mappaLeft);
	});
}

function view_map2(container,lat,lng,zoom) {
	var latlng = new google.maps.LatLng(lat, lng); //SETTO LA MAPPA SU GENOVA
	var myOptions = {
		zoom: zoom,
		center: latlng ,
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		mapTypeControl: true
	};
	map = new google.maps.Map(document.getElementById(container), myOptions);
	var marker = new google.maps.Marker({
		position: latlng, 
		map: map
	});
	
}

/* 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','');
		});
	});
});


/* 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');
	});
});


/* 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=$('#ticker');
	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;
}

/*SLIDER DESTRO DELLA NUOVA HOME*/
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);
	}
});
		
