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');
  input0='from:jsmooth995';
  input1='from:noreaga';
  input2='from:asherroth';
  input3='from:qoolquest';
  input4='from:mistahfab';
  input5='from:realtalibkweli';
  input6='from:rhymefest';
  input7='from:waledc';
  input8='from:missinfo';
  input9='from:charleshamilton';
  input10='from:kanye_west';
  input11='from:50cent';
  input12='from:common';
  input13='from:kidcudi';
  input14='from:snoopdogg';
  input15='from:angiemartinez';
  input16='from:souljaboytellem';
  input17='from:janellemonae';
  input18='from:black_milk';
  input19='from:dj_greenlantern';
  input20='from:ghostfacekillah';
  input21='from:immortaltech';
  input22='from:jeangrae';
  input23='from:koolherc';
  input24='from:officialnas';
  input25='from:snoopdogg';
  input26='from:swaycalloway';
  input27='from:clintonsparks';
  switch (cnt %28) {
    case 0:
      input=input0;
      break;
    case 1:
      input=input1;
      break;
    case 2:
      input=input2;
      break;
    case 3:
      input=input3;
      break;
    case 4:
      input=input4;
      break;
    case 5:
      input=input5;
      break;
	case 6:
      input=input6;
      break;
	case 7:
      input=input7;
      break;
	case 8:
      input=input8;
      break;
	case 9:
      input=input9;
      break;
	case 10:
      input=input10;
      break;
	case 11:
      input=input11;
      break;
	case 12:
      input=input12;
      break;
	case 13:
      input=input13;
      break;
	case 14:
      input=input14;
      break;
	case 15:
      input=input15;
      break;
	case 16:
      input=input16;
      break;
	case 17:
      input=input17;
      break;
	case 18:
      input=input18;
      break;
	case 19:
      input=input19;
      break;
	case 20:
      input=input20;
      break;
	case 21:
      input=input21;
      break;
	case 22:
      input=input22;
      break;
	case 23:
      input=input23;
      break;
	case 24:
      input=input24;
      break;
	case 25:
      input=input25;
      break;
	case 26:
      input=input26;
      break;
	case 27:
      input=input27;
      break;
    default:
      input=input0;
  }
  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]>50) {
    //elem.prepend('<div class="tweet"><img src="http://monitter.com/widget/favicon.gif" align="absmiddle" />real time twitter 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=?";
  $.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 hh=thedate.getHours();
        if (hh<10) {hh = "0" + hh}
        var mm=thedate.getMinutes();
        if (mm<10) {mm = "0" + mm}
        var thedatestr='at '+hh+':'+mm;
        var divstr='<div id="tw'+this.id+'" class="tweet"><img width="48" height="48" src="'+this.profile_image_url+'" ><p class="text">'+this.text.linkify().linkuser().linktag()+'<br />&nbsp;<b><a href="http://twitter.com/'+this.from_user+'" target="_blank">'+this.from_user+'</a></b> &nbsp;-&nbsp;<b>'+thedatestr+'</b></p></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(2000);
        $('#tw'+this.id,elem).fadeIn('slow');
      }
    });
    input=escape(input);
	cnt++;
    setTimeout(function(){fetch_tweets(elem)},2000);
  });
  return(false);
}

$(document).ready(function() {
  window.monitter={};
  $('.monitter').each(function(e) {
    cnt=0;
    rrp=1;
    fetch_tweets(this);
  });
});