/*
* tweetable 1.6 - 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.
*
* With modifications from Philipp Robbel (http://www.robbel.com/) and Patrick DW (stackoverflow)
* for IE compatibility.
*
* And further modifications by engage interactive (with a little code borrowed from http://tweet.seaofclouds.com/ - Cheeers :D )
*
* Revision: $Id: jquery.tweetable.js 2011-01-06 $ 
*
*/

(function($){

	//define the tweetable plugin
	$.fn.tweet = function(options){

		//specify the plugins defauls
		var defaults = {
			limit:		3,					//number of tweets to show
			username:	'caferougetweet'	//@username tweets to display
		};

		//overwrite the defaults
		var options = $.extend(defaults, options);
		
		//loop through each instance
		return this.each(function (options) {
		
			//assign our initial vars
			var act = $(this);
			var $tweetList;
			var tweetMonth = '';
			var shortMonths = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
			var api = "http://api.twitter.com/1/statuses/user_timeline.json?screen_name=";
			var count = "&count=20";
			
			//do a JSON request to twitters API
			$.getJSON(api + defaults.username + count + "&exclude_replies=1&include_rts=1&callback=?", act, function (data) {

				//create an unordered list to store tweets in
				$tweetList = act;

				var realLimit = 1;
				
				act.find('.loading').fadeOut(200,function(){
				
					//loop through twitters response
					$.each(data,function(i, item){
						
						if( $('html.ie:not(.ie9)').size() != 0 ){
							var time = '<span class="time">' + relative_time(item.created_at) + '</span>';
						}else{
							var time = '<time datetime="' + item.created_at + '">' + relative_time(item.created_at) + '</time>';
						}
						
	                    if( item.in_reply_to_status_id_str === null && realLimit <= defaults.limit ){
	                    
	                    	realLimit++;
	                    	
	                    	var delay = 0;
	                    	
							$tweetList.append('<p class="white">' + 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') + time + '</p>').find('.white').each(function(){
							
								$(this).css({
									opacity:	0,
									top:		-15
		                        }).delay(delay).animate({
									opacity:	1,
									top:		0
		                        },delay * 2  + 300,'easeOutExpo');
		                        
		                        delay = delay + 100;
								
							});
	
	                    }
	                    
					});
				
				});

			});
			
		});
		
		function parse_date(date_str){
			// The non-search twitter APIs return inconsistently-formatted dates, which Date.parse
			// cannot handle in IE. We therefore perform the following transformation:
			// "Wed Apr 29 08:53:31 +0000 2009" => "Wed, Apr 29 2009 08:53:31 +0000"
			return Date.parse(date_str.replace(/^([a-z]{3})( [a-z]{3} \d\d?)(.*)( \d{4})$/i, '$1,$2$4$3'));
		}
		
		function relative_time(date){
		
			var date = parse_date(date);
		
			var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
			var delta = parseInt((relative_to.getTime() - date) / 1000, 10);
			var r = '';
			
			if (delta < 60) {
				r = delta + ' seconds ago';
			} else if(delta < 120) {
				r = 'a minute ago';
			} else if(delta < (45*60)) {
				r = (parseInt(delta / 60, 10)).toString() + ' minutes ago';
			} else if(delta < (2*60*60)) {
				r = 'an hour ago';
			} else if(delta < (24*60*60)) {
				r = '' + (parseInt(delta / 3600, 10)).toString() + ' hours ago';
			} else if(delta < (48*60*60)) {
				r = 'a day ago';
			} else {
				r = (parseInt(delta / 86400, 10)).toString() + ' days ago';
			}
			
			return 'about ' + r;
		}
		
	}

})(jQuery);
