/*
*	jQuery Tweet v0.1
*	written by Diego Peralta
*
*	Copyright (c) 2010 Diego Peralta (http://www.bahiastudio.net/)
*	Dual licensed under the MIT (MIT-LICENSE.txt)
*	and GPL (GPL-LICENSE.txt) licenses.
*	Built using jQuery library 
*
*	Options:
*		- before (string): HTML code before the tweet.
*		- after (string): HTML code after the tweet.
*		- tweets (numeric): number of tweets to display.
*	
*	Example: 
*	
*		<script type="text/javascript" charset="utf-8">
*   		jQuery(document).ready(function() {
*      			jQuery('#tweets').tweets({
*          			tweets:4,
*          			username: "diego_ar"
*      			});
*  			});
*		</script>
*
*/
(function(jQuery){
	jQuery.fn.tweets = function(options) {
		jQuery.ajaxSetup({ cache: true });
		var defaults = {
			tweets: 5,
			before: "<li>",
			after: "</li>"
		};
		var options = jQuery.extend(defaults, options);
		/*return this.each(function() { */
			var obj = jQuery(this);
			jQuery.getJSON('http://api.twitter.com/1/statuses/user_timeline.json?callback=?&screen_name='+options.username+'&count='+options.tweets,
		        function(data) {
		            jQuery.each(data, function(i, tweet) {
		                if(tweet.text !== undefined) {
							jQuery(obj).append(options.before+tweet.text+options.after);
		                }
		            });
		        }
		    );/*
		});*/
	};
	jQuery.fn.getTweets = function(options){
		
		jQuery.ajaxSetup({ cache: true });
		var defaults = {
			tweets: 5,
			before: "<li>",
			after: "</li>"
		};
		var options = jQuery.extend(defaults, options);
		//return this.each(function() {
			var obj = jQuery(this);
			var arr = new Array();
			jQuery.getJSON('http://api.twitter.com/1/statuses/user_timeline.json?callback=?&screen_name='+options.username+'&count='+options.tweets,
		        function(data){
		            jQuery.each(data, function(i, tweet) {
		                if(tweet.text !== undefined) {
		                	tweet.text = tweet.text.replace(/(\b(https?):\/\/[^\s]*)/ig, function(url) {
            return '<a href="'+url+'" target="_blank">'+url+'</a>';
        }).replace(/@([_a-z0-9]+)/ig, function(reply) {
            return  '<a href="http://www.twitter.com/'+reply.substring(1)+'" target="_blank">'+reply+'</a>';
        });
        
		                    jQuery('<div></div>',{'class':'tweet',html:tweet.text,css:{display:'none',overflow:'hidden',width:'auto'}}).appendTo(obj);
		                    
		                }
		            });
		            obj.animateTweets(obj);
		        }
		    );
		//});
	}
	
	jQuery.fn.animTweets = function(options){
		var obj = jQuery(this);
		//alert(obj.getTweets(options));
		jQuery.each(obj.getTweets(options),function(i,v){
			alert(v);
			jQuery('<div></div>',{'class':'tweet',html:v,css:{display:'none',width:'auto'}}).appendTo(obj); //}
		});
		this.animateTweets(this);
	}
	
	jQuery.fn.animateTweets = function(o){
		var obj = jQuery(o);
		v = jQuery(obj).find('.tweet:visible').eq(0);
		if(v.html()!=null){
			v.animate({opacity:0},'slow',function(){ v = jQuery(this); parentObj = v.parent(); v.css({display:'none'}).clone().appendTo(parentObj); v.remove(); jQuery.fn.animateTweets(parentObj); });
		} else {
			jQuery(obj).find('.tweet:hidden').eq(0).css({display:'block',opacity:0}).animate({opacity:1},'slow');
			setTimeout(function(){ jQuery.fn.animateTweets(obj); },3000);
		}
	}
})(jQuery);

