/*
* 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">
*				$(document).ready(function() {
*						$('#tweets').tweets({
*								tweets:4,
*								username: "diego_ar"
*						});
*				});
*		</script>
*
*/
(function($){
	$.fn.tweets = function(options, callback) {
		$.ajaxSetup({ cache: true });
		var defaults = {
			tweets: 5,
			before: "<li>",
			after: "</li>"
		};
		var options = $.extend(defaults, options);
		return this.each(function() {
			var obj = $(this);
			var username = options.username ? options.username : "";
			var text = options.search_text ? options.search_text : "";
			//var url = 'http://search.twitter.com/search.json?callback=?&rpp='+options.tweets+'&q='+text+''+username;
			var url = "http://twitter.com/statuses/user_timeline.json?callback=?&id="+username;
			$.getJSON(url,
				function(data) {
				  
					$.each(data, function(i, tweet) {
						if (i >= options.tweets) return;
						//var date = typeof prettyDate != "undefined" ? prettyDate(tweet.created_at) : tweet.created_at;
						var d = new Date(tweet.created_at.replace("+0000", ""));
						var date = "";
						//console.log(d.getMonth());
						if (d && typeof d == "object" && parseInt(d.getDate())) {
							var month = d.getMonth() + 1;
						  		month = month < 10 ? "0"+month : month;
  						var day = d.getDate() < 10 ? "0"+d.getDate() : d.getDate();
  						var hours = d.getHours() < 10 ? "0"+d.getHours() : d.getHours();
  						var min = d.getMinutes() < 10 ? "0"+d.getMinutes() : d.getMinutes();
              date = d.getFullYear() + "." + month + "." + day;// + ", KL " + hours + ":" + min;
            }
						// var source = tweet.source || tweet.text;
						//            source = source.replace(/&lt;/gi, "<");
						//            source = source.replace(/&gt;/gi, ">");
						//            source = source.replace(/&quot;/gi, "\"");

            // console.log(i);
            // console.log(data.results.length);
						var extra_class = i == options.tweets - 1 ? " last" : "";
						
						if (tweet.text !== undefined) {
						  tweet.text = tweet.text.substr(0,70);
							// var html = '<div class="left"><img src="'+tweet.profile_image_url+'"/></div><div class="left content"><p class="text"><span class="username">'+tweet.from_user+'</span> '+tweet.text+'</p><span class="date">'+date+' via '+source+'</span></div>';
							var html = '<div class="content clearfix'+extra_class+'"><span class="date">'+date+'</span><p class="text">'+tweet.text+'</p></div>';
							var tweet_el = $(options.before+html+options.after).hide();
							$(obj).append(tweet_el);
							tweet_el.fadeIn();
						}
					});
					if (callback) callback();
				}
			);
			//obj = null;
		});
	};
})(jQuery);


