/*
* 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:		5,					//number of tweets to show
			username:	'belgobill'		//@username tweets to display
		};

		//overwrite the defaults
		var options = $.extend(defaults, options);
		
		//loop through each instance
		return this.each(function () {
		
			//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=40";
			
			act.height(act.find('.loading').innerHeight());
			
			//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('p.loading').animate({
					'left':	270
				},800,'easeInOutExpo');
				
				//loop through twitters response
				$.each(data,function(i, item){
					                    if( item.in_reply_to_status_id_str === null && realLimit <= options.limit ){
                    
						$tweetList.prepend('<p class="tweet">' + 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')+ '<span class="white_tail"></span></p>');
						
						realLimit++;
						
						if( realLimit - 1 == options.limit ){
							
							$tweetList.find('p:first').show().css({
								'left':	-270
							}).animate({
								'left':	0
							},800,'easeInOutExpo',function(){
								$tweetList.find('.loading').remove();
								cycle();
							});
							
							$tweetList.animate({
								'height':	$tweetList.find('p:first').innerHeight()
							},600,'easeInOutExpo');
								
	                    }

                    }
                    
				});
				
			});
			
		});
		
		function cycle(){
			
			var timer;
			var c = 1;
			var $t = $('#twitter div');
			
			function next(){
			
				clearTimeout(timer);
			
				timer = setTimeout(function(){
			
					c++;
					
					if( c == 6 ){
						c = 1;
					}
					
					var $new = $t.find('p:eq(' + ( c - 1 ) + ')');
					
					$new.show().css({
						'left':	-270
					}).animate({
						'left':	0
					},800,'easeInOutExpo').siblings('p').animate({
						'left':	270
					},800,'easeInOutExpo',function(){
						$(this).hide();
						next();
					});
					
					var newH = $new.innerHeight();
					
					$t.animate({
						'height':	newH
					},600,'easeInOutExpo');
			
				},5000);
				
			}
			
			next();
			
			$('#twitter').hover(function(){
				clearTimeout(timer);
			},function(){
				next();
			});
			
		}
		
	}

})(jQuery);
