var copyrightshown=false;
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');
	input2=elem.attr('title2'); // Added by MrDX 25.06.2009
	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;;
	}
	if((window.monitter['count-'+input]>10)&&(!copyrightshown))
	{
		copyrightshown=true;
		elem.prepend('<div class="tweet"><img src="http://monitter.com/widget/favicon.gif" align="absmiddle" />powered by <a href="http://monitter.com" target="_blank">monitter.com</a></div>');
		window.monitter['count-'+input]=0;
	}
	var url="http://search.twitter.com/search.json?q="+input+"&lang="+lang+"&rpp="+rrp+"&since_id="+window.monitter['last_id'+input]+"&callback=?";
	var url2="http://search.twitter.com/search.json?q="+input2+"&lang="+lang+"&rpp="+rrp+"&since_id="+window.monitter['last_id'+input]+"&callback=?";

	var json2;

	$.getJSON(url2,function(jsontemp) {
		$(jsontemp.results).reverse(); 
		json2 = jsontemp;
	});

	$.getJSON(url,function(json)
		{
			$('div.tweet:gt('+window.monitter['limit']+')',elem).each(function(){$(this).fadeOut('slow')});
			
			$(json.results).reverse();
			
			var resultsArr1 = jQuery.makeArray(json.results);
			var resultsArr2 = jQuery.makeArray(json2.results);

			$.merge(resultsArr1, resultsArr2);
			
			resultsArr1.sort(function(a, b){
				dateA = new Date(Date.parse(a.created_at));
				dateB = new Date(Date.parse(b.created_at));
				return dateA - dateB;
			});
				
			$(resultsArr1).each(function()
				{
					if($('#tw'+this.id,elem).length==0)
					{
						window.monitter['count-'+input]++;
						var thedate=new Date(Date.parse(this.created_at));
						var theminutes=thedate.getMinutes();
						var theHours=thedate.getHours();
						if (theminutes<10) {theminutes="0"+theminutes;}
						if (theHours<10) {theHours="0"+theHours;}
						if (theHours<12) {theminutes=theminutes+"am";}
						else {theminutes=theminutes+"pm";}
						if (theHours==0) {theHours=12;}
						if (theHours>12) {theHours=theHours-12;}
						var thedatestr=theHours+':'+theminutes+'&nbsp;'+thedate.getDate()+'/'+(thedate.getMonth()+1);
						var divstr='<div id="tw'+this.id+'" class="tweet" title="What D3neters are doing with their lives.."><img width="24" height="24" src="'+this.profile_image_url+'" ><p class="text">'+'<a href="http://twitter.com/'+this.from_user+'" target="_blank">'+this.from_user+'</a> '+this.text.linkify().linkuser().linktag()+'&nbsp; <span class="tweet-time">'+thedatestr+'</span></p><div style="clear:both;padding:0"></div></div>';
						divstr=divstr.replace(/normal/i, "mini");
						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(4000);
						$('#tw'+this.id,elem).fadeIn('slow');
					}
				}
			);
			input=escape(input);
			rrp=1;
			setTimeout(function(){fetch_tweets(elem)},6000);
		}
	);
	return(false);
}
$(document).ready(function()
{
	window.monitter={};
	$('.monitter').each(function(e)
	{
		rrp=7;
		fetch_tweets(this);
	});
	}
);