/*
	
	jquery.jbird.js - v1.0
	Load your twitter updates dynamically into your website. Also supports jQuery Tools Scrollable for scrolling your tweets. See <http://www.flowplayer.org/>.
	Copyright (C) 2011 - Stephen Clark

	This program is free software: you can redistribute it and/or 
	modify it under the terms of the GNU General Public License as 
	published by the Free Software Foundation, either version 3 of 
	the License, or (at your option) any later version.
	
	This program is distributed in the hope that it will be useful, 
	but WITHOUT ANY WARRANTY; without even the implied warranty of 
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
	See the GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License 
	along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

/* 

	Include jQuery Tools Scrollable
	
	If you do not wish to use the scroller, comment out the line below,
	and the scrollable call starting on line xx.
	
*/
$.getScript('http://cdn.jquerytools.org/1.2.5/tiny/jquery.tools.min.js');


(function($) {
	
	

	$.fn.jbird = function(options) {
		
		var o = $.extend({}, $.fn.jbird.defaults, options);
	
		// hide container element
		$(this).hide();

		// add twitter list to container element
		$(this).append('<div id="tweet_list"></div>');

		// hide twitter list
		$('#tweet_list').hide();

		// add preloader to container element
		var pl = $('<p id="'+o.preloaderId+'">'+o.loaderText+'</p>');
		$(this).append(pl);

		// show container element
		$(this).show();
		
		function format_date(created_at)
		{
			var values = created_at.split(" ");
			created_at = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
			var parsed_date = Date.parse(created_at);
			
			var weekdays = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
			var new_date = new Date(parsed_date);
			var dow = weekdays[new_date.getDay()];
			
			return new_date.toLocaleString();
		}
		
		$.ajax({
			type: "GET",
			url: "http://api.twitter.com/1/statuses/user_timeline.json",
			data: { screen_name : o.userName, count : o.numTweets, callback : '?' },
			dataType: "jsonp",
			error: function()
			{
				$('<div></div>')
					.html('There was an issue loading your tweets. Please try again.')
					.appendTo('#tweet_list');
			},
			success: function(jdata)
			{
				
				// remove preloader from container element
				$(pl).remove();
				
				// loop through number of tweets set
				for (var i = 0; i < o.numTweets; i++)
				{
					
					// set data for each loop
					var row = $(jdata).get(i);
					
					// if there is data
					if (row)
					{
						// set vars
						var date = format_date(row.created_at);
						var status_text = row.text;
						var status = status_text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url)
						{
      						return '<a href="'+url+'">'+url+'</a>';
    					});
						var tweet_item = $('<div class="tweet_item"></div>')
							.html('<div class="tweet_date">' + date + '</div><div class="tweet_text">' + status + '</div>');
						
						// append to twitter list
						tweet_item.appendTo('#tweet_list');
						
						
						/* comment out the following scrollable call if you do not wish to you the scroller */
						$('#tweets').scrollable({
							vertical: true
						});
						/* end scrollable */
					}
				}
				
				// show twitter list
				$("#tweet_list").show();
	
				// give first item a special class
				$("#tweet_list div:first-child").addClass("first_tweet");
				
			}
		});
		
	};

	// plugin defaults
	$.fn.jbird.defaults = {
		userName: null,
		numTweets: 5,
		preloaderId: "preloader",
		loaderText: "Loading tweets..."
	};
	
})(jQuery);
