
/* Twitter RSS-parser
-----------------------------------------------------------*/
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);});};

	
/* define tweets
------------------------------------------------------------*/
function fetch_tweets(elem) {	
	elem=$(elem);input='from%3AGrannenfelt+OR+from%3Aesapeltonen+OR+from%3Atiinaruottinen+OR+from%3Amineom+OR+from%3AMama_Sheep+OR+from%3Apaikky+OR+from%3Arobinpe+OR+from%3Apetterilaamo+OR+from%3Atuuukka';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]=50;
}
				
if(window.monitter['count-'+input]>50) { //10
	
/* define div classes
------------------------------------------------------------*/
elem.prepend('<div class="twitter"></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=?";$.getJSON(url,function(json) {
	$('div.twit:gt('+window.monitter['limit']+')',elem).each(function(){$(this).fadeOut('fast')});$(json.results).reverse().each(function() {
					
	if($('#tw'+this.id,elem).length==0) {
		window.monitter['count-'+input]++;
		
/* get date and time
-------------------------------------------------------------*/
var thedate=new Date(Date.parse(this.created_at));
var thedatestr=thedate.getHours()+':'+thedate.getMinutes();
var theday=thedate.getDate();
var paate = '';

/* purkkaa
-------------------------------------------------------------*/
if (theday==1) {
	paate = 'st';		
} else if (theday==2) {
	paate = 'nd';	
} else if (theday==3) {
	paate = 'rd';
} else {
	paate = 'th';	
}

/* months array
-------------------------------------------------------------*/
var months = new Array(12);
months[0] = "January";
months[1] = "February";
months[2] = "March";
months[3] = "April";
months[4] = "May";
months[5] = "June";
months[6] = "July";
months[7] = "August";
months[8] = "September";
months[9] = "October";
months[10] = "November";
months[11] = "December";

month_value = thedate.getMonth();
var themonth = months[month_value]; 

/* create div string to display result
--------------------------------------------------------------*/					
var divstr='<br /><div id="twit'+this.id+'" class="all_twitter"><div class="picture"><img width="48" height="48" src="'+this.profile_image_url+'" ></div><div class="textholder"><div class="tweet_date"><p class="tw_date" style="font-size: 13px;color: #717c91;padding-bottom:0;">'+ theday + paate +'&nbsp;'+ themonth +'</p></div><p class="text">'+this.text.linkify().linkuser().linktag()+'</p>' +'<b><a href="http://twitter.com/'+this.from_user+'" target="_blank">'+this.from_user+'</a></b> &nbsp; - &nbsp;<b>'+thedatestr+'</b></div>&nbsp;<b></div><div class="line"></div>';

/* rrp = show tweets perpage
--------------------------------------------------------------*/									
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(0);
$('#tw'+this.id,elem).fadeIn('slow');}});
	input=escape(input);rrp=100;setTimeout(function(){fetch_tweets(elem)},2000);});return(false);}
$(document).ready(function(){window.monitter={};
$('.monitter_all').each(function(e){rrp=100;fetch_tweets(this);});});