/*
 * tweetable 1.3 - jQuery twitter feed generator plugin
 *
 * Copyright (c) 2009 Philip Beel (http://www.theodin.co.uk/)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * Revision: $Id: jquery.tweetable.js 2010-08-09 $ 
 *
 */
 
 function relTime(time_value) { 
         time_value = time_value.replace(/(\+[0-9]{4}\s)/ig,""); 
         var parsed_date = Date.parse(time_value); 
         var relative_to = (arguments.length > 1) ? arguments[1] : new Date(); 
         var timeago = parseInt((relative_to.getTime() - parsed_date) / 1000); 
         if (timeago < 60) return 'less than a minute ago'; 
         else if(timeago < 120) return 'about a minute ago'; 
         else if(timeago < (45*60)) return (parseInt(timeago / 60)).toString() + ' minutes ago'; 
         else if(timeago < (90*60)) return 'about an hour ago'; 
         else if(timeago < (24*60*60)) return 'about ' + (parseInt(timeago / 3600)).toString() + ' hours ago'; 
         else if(timeago < (48*60*60)) return '1 day ago'; 
              else return (parseInt(timeago / 86400)).toString() + ' days ago'; 
         } 

 
(function($){
		$.fn.tweetable=function(options){
			var defaults={
				limit:5,
				username:'mygardenonline',
				time:false,
				replies:false
			};
			var options=$.extend(defaults,options);
			return this.each(function(options){
				var act=$(this);
				var api="http://api.twitter.com/1/statuses/user_timeline.json?screen_name=";
				var count="&count=";
				var returnContent = "";
				$.getJSON(api+defaults.username+count+defaults.limit+"&callback=?",act,function(data){
					$.each(data,function(i,item){
						if(i==0){
							//$(act).prepend('<ul class="tweetList">');
						}
						if(defaults.replies === false) {
							if(item.in_reply_to_status_id === null){
								returnContent = returnContent + '<li class="tweet_content"><span class="tweet_link_'+i+'">'+item.text.replace(/#(.*?)(\s|$)/g,'').replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig,'<a href="$&">$&</a> ').replace(/@(.*?)(\s|\(|\)|$)/g,'<a href="http://twitter.com/$1">@$1 </a>$2');
								//$('.tweetList').append('<li class="tweet_content_'+i+'"><span class="tweet_link_'+i+'">'+item.text.replace(/#(.*?)(\s|$)/g,'<span class="hash">#$1 </span>').replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig,'<a href="$&">$&</a> ').replace(/@(.*?)(\s|\(|\)|$)/g,'<a href="http://twitter.com/$1">@$1 </a>$2'));
								if(defaults.time==true){
									returnContent = returnContent + '<small> '+item.created_at.substr(0,20)+'</small>';
								}								
							}							
						} else {
							returnContent = returnContent + '<li class="tweet_content"><span class="tweet_link_'+i+'">'+item.text.replace(/#(.*?)(\s|$)/g,'').replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig,'<a href="$&">$&</a> ').replace(/@(.*?)(\s|\(|\)|$)/g,'<a href="http://twitter.com/$1">@$1 </a>$2');
							//$('.tweetList').append('<li class="tweet_content_'+i+'"><span class="tweet_link_'+i+'">'+item.text.replace(/#(.*?)(\s|$)/g,'<span class="hash">#$1 </span>').replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig,'<a href="$&">$&</a> ').replace(/@(.*?)(\s|\(|\)|$)/g,'<a href="http://twitter.com/$1">@$1 </a>$2'));
							if(defaults.time==true){
								returnContent = returnContent + '<small> '+item.created_at.substr(0,20)+'</small>';
							}								
						}
					});
					var tweets = '<ul class="tweetList">' + returnContent + '</ul>';
					$(act).append(tweets);
				});
			});
		}
	})(jQuery);
