var tweets;
var cuentatwitter = "alvarofaesf1";
var num_tweets='5';
var indice=0;
var tiempo=7;
var velocidad=500;

function hora(time_value) {	
	var meses=new Array();
    meses["Jan"] = "01";
    meses["Feb"] = "02";
    meses["Mar"] = "03";
    meses["Apr"] = "04";
    meses["May"] = "05";
    meses["Jun"] = "06";
    meses["Jul"] = "07";
    meses["Aug"] = "08";
    meses["Sep"] = "09";
    meses["Oct"] = "10";
    meses["Nov"] = "11";
    meses["Dec"] = "12";
	
	var fecha_cachos=time_value.split(" ");
	var mes=meses[fecha_cachos[1]];
	var dia=fecha_cachos[2];
	var hora=fecha_cachos[3].split(":");
	var horas=hora[0];
	var minutos=hora[1];
	var segundos=hora[2];
	var anio=fecha_cachos[5];
	//$("#debug").append(time_value);
	return dia+'/'+mes+'/'+anio+' '+(++horas) + ':' + minutos;
}

function formatearFecha(time_value) {
	var meses=new Array();
    meses["Jan"] = "01";
    meses["Feb"] = "02";
    meses["Mar"] = "03";
    meses["Apr"] = "04";
    meses["May"] = "05";
    meses["Jun"] = "06";
    meses["Jul"] = "07";
    meses["Aug"] = "08";
    meses["Sep"] = "09";
    meses["Oct"] = "10";
    meses["Nov"] = "11";
    meses["Dec"] = "12";
	
	var fecha_cachos=time_value.split(" ");
	var mes=meses[fecha_cachos[1]];
	var dia=fecha_cachos[2];
	var hora=fecha_cachos[3].split(":");
	var horas=hora[0];
	var minutos=hora[1];
	var segundos=hora[2];
	var anio=fecha_cachos[5];
	//$("#debug").append(time_value);
	return new Date(anio, mes-1, dia, horas, minutos, segundos);
}

function relative_time(time_value) {
	var time_value=formatearFecha(time_value);
	
	//var parsed_date = Date.parse(time_value);
	var parsed_date = time_value.getTime();
	
	// Ajustar zona horaria restando 2 horas
	parsed_date=parsed_date+(1000*60*60*2);
	//$("#debug").append(" "+parsed_date);
	var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
	var tiempo = parseInt((relative_to.getTime() - parsed_date) / 1000);
	
	if(tiempo < 60) {
		return 'hace unos segundos';
	} else if(tiempo < 120) {
		return 'hace un minuto';
	} else if(tiempo < (45*60)) {
		return 'hace ' + (parseInt(tiempo / 60)).toString() + ' minutos';
	} else if(tiempo < (90*60)) {
		return 'hace una hora';
	} else if(tiempo < (24*60*60)) {
		if(parseInt(tiempo / 3600)==1)
			return 'hace una hora';
		else
			return 'hace ' + (parseInt(tiempo / 3600)).toString() + ' horas';
	} else if(tiempo < (48*60*60)) {
		return 'ayer';
	} else {
		return 'hace ' + (parseInt(tiempo / 86400)).toString() + ' dias';
	}
}

function url2link(text)  
{  
	var expr = /(https?:\/\/\S+)/gi;  
	var anchr= '<a target="_blank" href="$1">$1</a>';  
	return text.replace(expr,anchr);  
} 

function cargarTweet() {
	if(indice>=0 && indice<tweets.length) {
		var tweet = tweets[indice].text;
		//var tweetTime = relative_time(tweets[indice].created_at);
		var tweetTime = hora(tweets[indice].created_at);
		// Comprobar si es la primera vez
		if($("#twitblock").length > 0) {
			$("#bloque_movimiento").animate({marginTop: "-100px"}, velocidad, function() {
				$("#twitblock").remove();
				$("#tweet").append('<div id="twitblock"></div>');
				$("#twitblock").append('<span class="twittime">'+tweetTime+'</span><div class="twittext">'+url2link(tweet)+'<div class="clearfix"></div></div>');
				$("#bloque_movimiento").animate({marginTop: "-10px"}, velocidad);
			});
		} else {
			$("#tweet").append('<div id="twitblock"></div>');
			$("#twitblock").append('<span class="twittime">'+tweetTime+'</span><div class="twittext">'+url2link(tweet)+'<div class="clearfix"></div></div>');
			$("#bloque_movimiento").animate({marginTop: "-10px"}, velocidad);
		}
		indice++;
		if(indice>=tweets.length)
			indice=0;
	}
}

function llamadaTwitter() {
	$.getJSON("http://twitter.com/statuses/user_timeline/"+ cuentatwitter +".json?count="+num_tweets+"&callback=?", function(json){
		tweets=json;
		if(tweets.length>0) {
			cargarTweet();
			if(tweets.length>1) setInterval("cargarTweet()", tiempo*1000);
		}
	});
}

function do_onload() {
	if($("#tweet").length > 0){
		llamadaTwitter();
	}
}

$(document).ready(function(){
	do_onload();
});
