(function($){
 $.fn.jTweet = function(options) {
    
  var defaults = {
   num: 30,
   id: "thespacedmonkey",
   showdate: true,
   showsource: true,
   showreply: true,
   showavatar: true

  };
  
  var options = $.extend(defaults, options);
    return this.each(function() {
    	obj = $(this);
    	var $thetweet = $("<div class='alljtweets'></div>");
    	var $thetweet2 = $("<div class='jtweets'></div>");
		var num1 = options.num;
		if(num1 > 3){
			num1 = options.num + 1;	
		}
		var url = "http://twitter.com/status/user_timeline/"+options.id+".json?count="+num1+"&callback=?";
		$.getJSON(url,
        function(data){
        	
			$.each(data, function(i, item) {

			
				var reply = item.in_reply_to_screen_name;
				var status =  item.in_reply_to_status_id
				var twitterim = item.text;
				var avatarurl = item.user.profile_image_url;
				var screenname = item.user.screen_name;
				var dateim = item.created_at;
				var sourceim = " via "+ item.source;
				
				var avatar = "";
			
				
				var endtweetcontainer = "";
				
				var	replymess = "";
				
				if(reply != null){
					replymess = " <a href='http://twitter.com/"+ reply +"/status/"+ status +"'>reply to "+ reply +"</a>";
				}
				
				if(options.showavatar){
					avatar = $("<img />").attr({
 						src: avatarurl,
 						alt: screenname
					}).addClass('jtweet-avator');
				}
				if(options.showdate){
					endtweetcontainer = endtweetcontainer + dateim;
				}
				if(options.showsource){
					endtweetcontainer = endtweetcontainer + sourceim;
				}
				if(options.showreply){
					endtweetcontainer = endtweetcontainer + replymess;
				}

				var endtweet = $("<span class='jtweet-reply'></span>").html(endtweetcontainer);
				

					var $tweet = $("<div class='jtweets'></div>").html(twitterim);
						$tweet.html(
							$tweet.html()
								.replace(/((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi,'<a href="$1">$1</a>')
								.replace(/(^|\s)#(\w+)/g,'$1<a href="http://search.twitter.com/search?q=%23$2">#$2</a>')
								.replace(/(^|\s)@(\w+)/g,'$1<a href="http://twitter.com/$2">@$2</a>')
								
							)
				$tweet.wrapInner("<p class='jtweet-body'></p>");
				$tweet.prepend(avatar).append(endtweet);
				$thetweet.append($tweet);

								});});

		obj.append($thetweet);
		$('#text-3').show();
		});
 };
})(jQuery);



	
String.prototype.linkify = function() {
  return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(m) {
  return m.link(m);
  });
}; 
 
 
 String.prototype.atify = function() {
return this.replace(/@[\w]+/g, function(m) {
return "<a href='http://www.twitter.com/"+m.replace('@','')+"'>"+m+"</a>";
});
};
String.prototype.linktag = function() {
  return this.replace(/[#]+[A-Za-z0-9-_]+/, function(t) {
    var tag = t.replace("#","%23")
    return t.link("http://summize.com/search?q="+tag);
  });
};  
  function relative_time(time_value) {
	  var values = time_value.split(" ");
	  time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
	  var parsed_date = Date.parse(time_value);
	  var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
	  var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
	  delta = delta + (relative_to.getTimezoneOffset() * 60);
	  
	  var r = '';
	  if (delta < 60) {
	    r = 'a minute ago';
	  } else if(delta < 120) {
	    r = 'couple of minutes ago';
	  } else if(delta < (45*60)) {
	    r = (parseInt(delta / 60)).toString() + ' minutes ago';
	  } else if(delta < (90*60)) {
	    r = 'an hour ago';
	  } else if(delta < (24*60*60)) {
	    r = '' + (parseInt(delta / 3600)).toString() + ' hours ago';
	  } else if(delta < (48*60*60)) {
	    r = '1 day ago';
	  } else {
	    r = (parseInt(delta / 86400)).toString() + ' days ago';
	  }
	  
	  return r;
}
function twitter_callback ()
{
	return true;

}





	
String.prototype.linkify = function() {
  return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(m) {
  return m.link(m);
  });
}; 
 
 
 String.prototype.atify = function() {
return this.replace(/@[\w]+/g, function(m) {
return "<a href='http://www.twitter.com/"+m.replace('@','')+"'>"+m+"</a>";
});
};
String.prototype.linktag = function() {
  return this.replace(/[#]+[A-Za-z0-9-_]+/, function(t) {
    var tag = t.replace("#","%23")
    return t.link("http://summize.com/search?q="+tag);
  });
};  
  function relative_time(time_value) {
	  var values = time_value.split(" ");
	  time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
	  var parsed_date = Date.parse(time_value);
	  var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
	  var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
	  delta = delta + (relative_to.getTimezoneOffset() * 60);
	  
	  var r = '';
	  if (delta < 60) {
	    r = 'a minute ago';
	  } else if(delta < 120) {
	    r = 'couple of minutes ago';
	  } else if(delta < (45*60)) {
	    r = (parseInt(delta / 60)).toString() + ' minutes ago';
	  } else if(delta < (90*60)) {
	    r = 'an hour ago';
	  } else if(delta < (24*60*60)) {
	    r = '' + (parseInt(delta / 3600)).toString() + ' hours ago';
	  } else if(delta < (48*60*60)) {
	    r = '1 day ago';
	  } else {
	    r = (parseInt(delta / 86400)).toString() + ' days ago';
	  }
	  
	  return r;
}
function twitter_callback ()
{
	return true;

}

