//when the dom is ready	
$(document).ready(function(){
	//configuration
	var screen_name = 'sofiavergara';
	var count = "200";
	var selector = "#tweet";

	//get tweets
	var json_uri = "http://api.twitter.com/1/statuses/user_timeline.json?screen_name="+screen_name+"&count="+count+"&include_entities=false&callback=?";
	$.getJSON(json_uri, function(data){
		//print non mentions tweets
    	$.each(data, function(i,data){
			var msg = data.text;
			var msg_start = msg.charAt(0);
			if(msg_start != "@"){
				//log(msg);
				var this_li = '<p>'+ify.clean(msg)+'</p>';
				$(selector).append(this_li);	
			}
		});
		
		//cycle 'n fx
		$('#tweet').cycle({ 
		    fx:      'fade', 
		    speed:    300, 
		    timeout:  5000 
		});
		//fuckin' MSIE needs this to remove the white background-color for the paragrahps... anyway, it doesnt work properly right now :(
		$('#tweet p').addClass('no_color');		
 	});
	
	//get profile pic
	var profilepic_uri = 'http://api.twitter.com/1/users/profile_image/'+screen_name+'.json?size=normal';
	$('.avatar').html('<img src="'+profilepic_uri+'" />');
});

//thanks to Dustin Diaz: http://www.dustindiaz.com/basement/ify.html
var ify = function() {
  return {
    "link": function(t) {
      return t.replace(/(^|\s+)(https*\:\/\/\S+[^\.\s+])/g, function(m, m1, link) {
        return m1 + '<a href=' + link + '>' + ((link.length > 25) ? link.substr(0, 24) + '...' : link) + '</a>';
      });
    },
    "at": function(t) {
      return t.replace(/(^|\s+)\@([a-zA-Z0-9_]{1,15})/g, function(m, m1, m2) {
        return m1 + '@<a href="http://twitter.com/' + m2 + '">' + m2 + '</a>';
      });
    },
    "hash": function(t) {
      return t.replace(/(^|\s+)\#([a-zA-Z0-9_]+)/g, function(m, m1, m2) {
        return m1 + '#<a href="http://search.twitter.com/search?q=%23' + m2 + '">' + m2 + '</a>';
      });
    },
    "clean": function(tweet) {
      return this.hash(this.at(this.link(tweet)));
    }
  };
}();

