/**
 * funzione che gestisce i tweets caricati e li mostra uno per volta
 * @author musings
 * date: 2010/07/30
 * update: 2010/09/23
 * altezza dei tweets diinamica e idem per altezza del div che li contiene e che fa da finestra
 */


	
var TweetCorrente = 1;
var ProssimoTweet = 2;
var AltezzaTweet = 160; /*altezza iniziale fissa dei tweets*/
var AltezzaFinestra = 165; /*altezza iniziale del div che contiene i tweets*/
	
function next(NumeroTweets){
	AltezzaTweet = $('.tweet li:nth-child(' + TweetCorrente + ')').outerHeight();
	
	TweetCorrente++;
	ProssimoTweet++;
	
	var TopCorrente = '-='+AltezzaTweet;
	
	if(TweetCorrente > NumeroTweets){
			TweetCorrente = 1;
			ProssimoTweet = 2;
			$('.tweet').animate({top: TopCorrente},500,function(){$(this).css({'top': 0}).animate({top: 0},500)});
			AltezzaFinestra = $('#news h2').outerHeight() + $('.tweet li:nth-child(1)').outerHeight() + $('.tweet li:nth-child(2)').outerHeight();
			$('#last_tweets').css({'height': AltezzaFinestra});
			}
	else	
	
	$('.tweet').animate({top: TopCorrente},500);		
	if(TweetCorrente == NumeroTweets)
		ProssimoTweet =1;
	AltezzaFinestra = $('#news h2').outerHeight() + $('.tweet li:nth-child(' + TweetCorrente + ')').outerHeight() + $('.tweet li:nth-child(' + ProssimoTweet + ')').outerHeight();
	$('#last_tweets').css({'height': AltezzaFinestra});
	}
/*
	provo a vedere se funziona l'auto increment (funziona!)
*/
//var id = setInterval(function(){next(NumeroTweets)},5000);

function stop(id){
	clearInterval(id);
}

function start(NumeroTweets){
	var id = setInterval(function(){next(NumeroTweets)},7000);
	return id;
}	


//nota per me: percorso 'php/...' per la versione definitiva
//percorso '../php/...' per la versione in v5

$(document).ready(function(){
	//per sistemare quelli che sono nelle sottocartelle it e en
	var PathToGo = '';
	var ActualPathname = window.location.pathname;
	if ((ActualPathname.search('en') == -1)&&((ActualPathname.search('it') == -1)))
		PathToGo = 'www/php/logic/TwitterCaching2.php';
	else 
		PathToGo = '../php/logic/TwitterCaching2.php'
	
	$('.tweet').load(PathToGo,function(){
		/*imposto l'altezza della finestra al valore dei primi 2 tweets*/
		AltezzaFinestra = $('#news h2').outerHeight() + $('.tweet li:nth-child(1)').outerHeight() + $('.tweet li:nth-child(2)').outerHeight();
		$('#last_tweets').css({'height': AltezzaFinestra});
		/*il numero di passaggi di scorrimento è uguale a quello dei li che ci sono nella coda*/
		var NumeroTweets = $('.tweet ul li').size();
		start(NumeroTweets);
		// faccio qui il backup del font size per resize font, se no caricato dinamicamente non ce la fa!
		//$('.tweet li').attr("font-size_backup","14px");
		$('a[href^="http://"]').attr("target", "_blank");
	});
});

