﻿var dateTweet = "";
var abaAtiva = "";

function abrirBalaoPerf(numBalao) {
    $("#BALAO_PERFORM_" + numBalao).show("slow");
}

function getLastTweet(){
	$.getJSON("http://twitter.com/statuses/user_timeline/global_ad.json?count=1&callback=?",
		  function(data) {formatNewTweet(data);}
	  );
}

function formatNewTweet(response)
{
	if(response[0].created_at != dateTweet) {
		dataTweet = relative_time(response[0].created_at);
		tweet = response[0].text;		

		var pattern = new RegExp("^"+response[0].username+": ","g");
		
		var tweet = tweet.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
				return '<a href="'+url+'" class="fontLaranja" target="_blank">'+url+'</a>';
			}).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
				return  reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'" target="_blank" class="fontLaranja">'+reply.substring(1)+'</a>';
			}).replace(pattern,'');

        // mostramos el resultado:
		$(".BOX_TWITTER_TIMELINE").html(dataTweet);
        $(".BOX_TWITTER_TEXT").html(tweet);
	}
}

function relative_time(time_value) {
   var parsed_date = Date.parse(time_value);

   var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
   var tiempo = parseInt((relative_to.getTime() - parsed_date) / 1000);

   if(tiempo < 60) {
       return 'Há alguns segundos';
   } else if(tiempo < 120) {
       return 'Há um minuto';
   } else if(tiempo < (45*60)) {
       return 'Há ' + (parseInt(tiempo / 60)).toString() + ' minutos';
   } else if(tiempo < (90*60)) {
           return 'Há uma hora';
       } else if(tiempo < (24*60*60)) {
       return 'Há ' + (parseInt(tiempo / 3600)).toString() + ' horas';
   } else if(tiempo < (48*60*60)) {
       return 'Há um dia atrás';
   } else {
       return 'Há ' + (parseInt(tiempo / 86400)).toString() + ' dias';
   }
}

function relative_time_2(time_value) {
    var newtime = time_value(',','');
    var values = newtime.split(" ");
    time_value = values[2] + " " + values[1] + ", " + values[3] + " " + values[4];
    var parsed_date = Date.parse(time_value);
    var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
    var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
    delta = delta + (relative_to.getTimezoneOffset() * 60);
 
    if (delta < 60) {
        return 'less than a minute ago';
    } else if(delta < 120) {
        return 'about a minute ago';
    } else if(delta < (60*60)) {
        return (parseInt(delta / 60)).toString() + ' minutes ago';
    } else if(delta < (120*60)) {
        return 'about an hour ago';
    } else if(delta < (24*60*60)) {
        return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
    } else if(delta < (48*60*60)) {
        return '1 day ago';
    } else {
        return (parseInt(delta / 86400)).toString() + ' days ago';
    }
}

function showBaloon(idBalao)
{
	if(abaAtiva != idBalao) {
		$(".menu_balao").hide("2000");
		$("."+idBalao).show("2000");
		abaAtiva = idBalao;
	}
}
