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) 
  {
  	m = m.link(m);
  	m = m.replace('href="','target="_blank" href="');
  	return m;
  });
};

String.prototype.linkuser = function() 
{
  return this.replace(/[@]+[A-Za-z0-9-_]+/g, function(us) 
  {
    var username = us.replace("@","");
    
    us = us.link("http://twitter.com/"+username);
  	us = us.replace('href="','target="_blank" onclick="loadProfile(\''+username+'\');return(false);"  href="');
    return us;
  });
};

String.prototype.linktag = function() 
{
  return this.replace(/[#]+[A-Za-z0-9-_]+/g, function(t) 
  {
    var tag = t.replace(" #"," %23");
    t = t.link("http://summize.com/search?q="+tag);
  	t = t.replace('href="','target="_blank" href="');
  	return t;
  });
};

/*String.prototype.urlencode = function() 
{
  return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/g, function( en ) 
  {
    return en.replace("#","%23").replace(" ","%20").replace("@","%40").replace('"','%22');
  });
};*/
String.prototype.urlencode = function()
{
	return this.replace( /#/g,"%23" ).replace( /\s/g, "%20" ).replace( /@/g, "%40" ).replace( /"/g, '%22' );
}

function fetch_tweets(elem) {
	elem=$(elem);
	input=elem.attr('searchq');
	lang=elem.attr('lang');
	if(input!=window.utweet['text-'+input]){
		window.utweet['last_id'+input]=0;
		window.utweet['text-'+input]=input;
		window.utweet['count-'+input]=12;
		;}
	if(window.utweet['count-'+input]>10)
	var url="http://search.twitter.com/search.json?"+lang+"="+input+"&rpp="+rrp+"&since_id="+window.utweet['last_id'+input]+"&callback=?";$.getJSON(url,function(json){
//		$('div.tweet_body:gt('+window.utweet['limit']+')',elem).each(function(){
//			$(this).fadeOut('slow')});
		$(json.results).reverse().each(function()
		{
			if($('#tw'+this.id,elem).length==0)
			{
				window.utweet['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_body"><div class="profile_image"><a href="http://twitter.com/'+this.from_user+'" target="_blank"><img width="48" height="48" src="'+this.profile_image_url+'" alt="@'+this.from_user+'" ></a></div><div class="username"><a href="http://twitter.com/'+this.from_user+'" target="_blank" title="@'+this.from_user+'">'+this.from_user+'</a></div><div class="content">'+this.text.linkify().linktag()+'<div class="hour">'+thedatestr+'</div></div><span class="icons"><a href="http://twitter.com/home/?status=@'+this.from_user+'&in_reply_to_status_id='+this.id+'&in_reply_to='+this.from_user+'" class="reply" title="Reply" target="_blank">Reply</a><a href="http://twitter.com/home/?status=RT @'+this.from_user+': '+this.text.urlencode()+'" class="retweet" title="ReTweet" target="_blank">Retweet</a><a href="http://twitter.com/'+this.from_user+'" class="profile" target="_blank" title="Vezi profil">Vezi profil</a></span><div class="clearfix"></div></div>';
				window.utweet['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(1000);
				$('#tw'+this.id,elem).fadeIn('slow');}
				twttr.anywhere( function(T){ 
					T.hovercards(); 
				//	T(".profile_image>a>img").hovercards( {username: function(node) { return node.alt;} });
					T(".username").hovercards( {username: function(node) { return node.title;} });
				});
				$(".tweet_body").hover
				(
					function () 
					{
					  $(this).find("span").addClass("hover");
					  $(this).find(".hour").addClass("hover");
					},
					function () 
					{
					  $(this).find("span").removeClass("hover");
					  $(this).find(".hour").removeClass("hover");
					}
				);
			});
		input=escape(input);
		rrp=1;
		setTimeout(function(){
			fetch_tweets(elem)},5000);
		});
	return(false)
;}

$(document).ready(function(){
	window.utweet={};
	$('.containers').each(function(e){
		rrp=15;
		fetch_tweets(this);
	});
});

