/**
 * @author Marco Valori
 */

$(function() {		
		
	// initialize scrollable 
	$("#scroll").fadeIn('slow').serialScroll({
		items:'p',
		duration:4000,
		force:true,
		axis:'y',
		easing:'linear',
		lazy:true,
		interval:1,
		step:1, 
		onAfter:function( elem ){
			var currentView = $(elem).attr("id");
			if( currentView == "ultimo_scroll" ) { 
				$("#scroll").scrollTo($("#primo_scroll")); 
			}
		}

	}).hover(function(){
		$(this).trigger('stop');
	}, function(){
		$(this).trigger('start');
	});	
	
	
	$('a.lightbox_da_mostrare').show().lightBox();
	
	scrivi_ora();
	
	var refreshId = setInterval(function() {
		$.ajax({
		  type: "GET",
		  url: "keep_alive.php"
		});
	}, 60000);
	
	if ($('#scegliCitta').length > 0) {
		$('#scegliCitta').click(function() {
			
			$('#pannello_scegliCitta').show().animate({height: "400px", width: "400px"}, "slow").load('pannello_sceglicitta.php');
			return false;
			
		});
	}
	
});


function scrivi_ora(){
	today=new Date();
	var myDays=["Dom","Lun","Mar","Mer","Gio","Ven","Sab","Dom"];
	var myMonths=["Gennaio","Febbraio","Marzo","Aprile","Maggio","Giugno","Luglio","Agosto","Settembre","Ottobre","Novembre","Dicembre"];
	thisDay=today.getDay();
	thisDay=myDays[thisDay];
	thisMonth=myMonths[today.getMonth()];
	if (today.getMinutes() < 10) testoMin = '0'; 
	else testoMin = '';
	if (today.getSeconds() % 2 == 0) testoBlink = '.';
	else testoBlink = ' ';
	$('#ora').html(thisDay + ' ' + today.getDate() + ' ' + thisMonth + ' ' + today.getFullYear() + ' - ' + today.getHours() + testoBlink + testoMin + today.getMinutes());
	setTimeout("scrivi_ora()", 1000);
}

function attiva_img_lightbox() {
	i = 0;
	da_attivare = $('a.lightbox_da_mostrare');
	while (i < da_attivare.length) {
		da_attivare[i].style.display = "block";
		i++;
	}
}

var mintimer = 10;
var sectimer = 0;
var pausatime = 0;

function start_stop_time(){
	if (pausatime == 0) {
		pausatime = 1;
		$('#pausatimer').src='images/play.png';
		$('#apausatimer').title='avvia il timer...';
	} else {
		pausatime = 0;
		$('#pausatimer').src='images/pausa.png';
		$('#apausatimer').title='ferma il timer...';
	}
}

function timer_aggiornamento(){
	if (pausatime == 0) sectimer = sectimer - 1;
	if (sectimer == -1 & mintimer == 0) location.reload(); 
	if (sectimer == -1 & mintimer > 0) {
		mintimer = mintimer - 1;
		sectimer = 59;
	}
	if (sectimer < 0) sectimerout = 0;
	else sectimerout = sectimer;
	if (sectimerout < 10) sectimerout = '0' + sectimerout;
	$('#timer').html(mintimer + '\' ' + sectimerout + '"');
}

function aggiornamento_automatico(){
	setInterval('timer_aggiornamento()', 1000);
}

function carica_testo_commento(codice) {
	if ($('#testo_comm_' + codice).css('display') == 'none'){
		$('#testo_comm_' + codice).load('commenti_suggest.php?cm=' + codice).show('slow');
	} else {
		$('#testo_comm_' + codice).hide('slow');
	}
	
}

//Scrolling articoli
var pause = 0

function scorri(){
   copyspeed = 1
	scrollerheight = 0
	if (pause == 1) copyspeed = 0
	scroller = $("scroll");
	actualheight=scroller.offsetHeight
	if (parseInt(scroller.style.top)>(actualheight*(-1)+8)){
		scroller.style.top=parseInt(scroller.style.top)-copyspeed+"px";
		setTimeout("scorri()", 80);
		return;
	} else {
		setTimeout("scorri()", 6000);
		scroller.style.top=parseInt(scrollerheight)+8+"px";
		return;
	}
}

