function relative_time(pastTime){
    // Generate a JavaScript relative time for the tweets     
    var origStamp = Date.parse(pastTime);
    var curDate = new Date();
    var currentStamp = curDate.getTime();
    var difference = parseInt((currentStamp - origStamp)/1000); 
    if(difference < 0)           return "gerade jetzt"; 
    if(difference <= 5)          return "gerade jetzt";
    if(difference <= 20)         return "vor wenigen Sekunden";
    if(difference <= 60)         return "vor 1 Minute";
    if(difference < 3600)        return "vor "+parseInt(difference/60)+" Minuten";
    if(difference <= 1.5*3600)   return "vor 1 Stunde";
    if(difference < 23.5*3600)   return "vor "+Math.round(difference/3600)+" Stunden";
    if(difference < 1.5*24*3600) return "vor 1 Tag";
    
    // If the tweet is older than a day;     
    var days = difference / (1.5*24*3600);   
     if (days <= 10){  
         return "vor " + Math.round(days) +  " Tagen";  
     }  
     else {  
         var dateArr = pastTime.split(' ');
         var t = dateArr[4].split(":");  
         var time_format = "";  
         if (parseInt(t[0]) > 12){  
             var a =  parseInt(t) - 12  
             time_format = dateArr[2] + " " + dateArr[1] + " " + a + ":" + t[1] + " PM";  
         }  
         else {  
             var first = "";  
             if (t[0].substring(0, 1) == "0"){  
                 first = t[0].substring(1, 2);  
             }  
            else {  
                 first = t[0];  
             }
             time_format = dateArr[2] + " " + dateArr[1] + " " + first + ":" + t[1] + " AM";  
         }   
         return time_format;
     }
}

jQuery.fn.reverse=Array.prototype.reverse;String.prototype.linkify=function()
{return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/g,function(m)
{return m.link(m);});};String.prototype.linkuser=function()
{return this.replace(/[@]+[A-Za-z0-9-_]+/g,function(u)
{var username=u.replace("@","")
return u.link("http://twitter.com/"+username);});};String.prototype.linktag=function()
{return this.replace(/[#]+[A-Za-z0-9-_]+/,function(t)
{var tag=t.replace("#","%23")
return t.link("http://search.twitter.com/search?q="+tag);});};function fetch_tweets(elem)
{elem=$(elem);input=elem.attr('title');lang=elem.attr('lang');if(input!=window.monitter['text-'+input])
{window.monitter['last_id'+input]=0;window.monitter['text-'+input]=input;window.monitter['count-'+input]=12;;}

var url="http://search.twitter.com/search.json?q="+input+"&lang="+lang+"&rpp="+rrp+"&since_id="+window.monitter['last_id'+input]+"&callback=?";$.getJSON(url,function(json)
{$('div.tweet:gt('+window.monitter['limit']+')',elem).each(function(){$(this).fadeOut('slow')});$(json.results).reverse().each(function()
{if($('#tw'+this.id,elem).length==0)
{window.monitter['count-'+input]++;var thedate=new Date(Date.parse(this.created_at));var thedatestr=relative_time(thedate);var divstr='<div id="tw'+this.id+'" class="tweet"><img width="48" height="48" src="'+(this.profile_image_url).replace('_normal', '_bigger')+'" ><p class="text">'+this.text.linkify().linkuser().linktag()+'<br /><span class="tweeter"><a href="http://twitter.com/'+this.from_user+'" target="_blank">'+this.from_user+'</a>&nbsp;'+thedatestr+'</span><div style="clear:left;"></div></div>';window.monitter['last_id'+input]=this.id;elem.prepend(divstr);$('#tw'+this.id,elem).hide();$('#tw'+this.id+' img',elem).hide();$('#tw'+this.id+' img',elem).fadeIn(1500);$('#tw'+this.id,elem).slideDown('slow');}});input=escape(input);rrp=4;setTimeout(function(){fetch_tweets(elem)},3000);});return(false);}
$(document).ready(function(){window.monitter={};$('.monitter').each(function(e){rrp=50;fetch_tweets(this);});});

/*
jQuery.fn.reverse=Array.prototype.reverse;String.prototype.linkify=function()
{return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/g,function(m)
{return m.link(m);});};String.prototype.linkuser=function()
{return this.replace(/[@]+[A-Za-z0-9-_]+/g,function(u)
{var username=u.replace("@","")
return u.link("http://twitter.com/"+username);});};String.prototype.linktag=function()
{return this.replace(/[#]+[A-Za-z0-9-_]+/,function(t)
{var tag=t.replace("#","%23")
return t.link("http://search.twitter.com/search?q="+tag);});};function fetch_tweets(elem)
{elem=$(elem);input=elem.attr('title');lang=elem.attr('lang');if(input!=window.monitter['text-'+input])
{window.monitter['last_id'+input]=0;window.monitter['text-'+input]=input;window.monitter['count-'+input]=12;;}

var url="http://search.twitter.com/search.json?q="+input+"&lang="+lang+"&rpp="+rrp+"&since_id="+window.monitter['last_id'+input]+"&callback=?";$.getJSON(url,function(json)
{$('div.tweet:gt('+window.monitter['limit']+')',elem).each(function(){$(this).fadeOut('slow')});$(json.results).reverse().each(function()
{if($('#tw'+this.id,elem).length==0)
{window.monitter['count-'+input]++;var thedate=new Date(Date.parse(this.created_at));var thedatestr=thedate.getHours()+':'+thedate.getMinutes();var divstr='<div id="tw'+this.id+'" class="tweet"><img width="48" height="48" src="'+(this.profile_image_url).replace('_normal', '_bigger')+'" ><p class="text">'+this.text.linkify().linkuser().linktag()+'<br /><span class="tweeter">By <a href="http://twitter.com/'+this.from_user+'" target="_blank">'+this.from_user+'</a>&nbsp;um&nbsp;'+thedatestr+'</span><div style="clear:left;"></div></div>';window.monitter['last_id'+input]=this.id;elem.prepend(divstr);$('#tw'+this.id,elem).hide();$('#tw'+this.id+' img',elem).hide();$('#tw'+this.id+' img',elem).fadeIn(1500);$('#tw'+this.id,elem).slideDown('slow');}});input=escape(input);rrp=1;setTimeout(function(){fetch_tweets(elem)},1000);});return(false);}
$(document).ready(function(){window.monitter={};$('.monitter').each(function(e){rrp=10;fetch_tweets(this);});});
*/
