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

		var this_this = $(this);
		
		// add twitter list to container element
		$(this).append('<ul id="twitter_update_list"><li></li></ul>');

		// hide twitter list
		$("ul#twitter_update_list").hide();

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

		// show container element
		$(this).show();
	
		function relative_time(time_value) {
		  var values = time_value.split(" ");
		  time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
		  var parsed_date = Date.parse(time_value);
		  var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
		  var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
		  delta = delta + (relative_to.getTimezoneOffset() * 60);

		  if (delta < 60) {
			return 'il y a moins d\'une minute';
		  } else if(delta < 120) {
			return 'il y a environ une minute';
		  } else if(delta < (60*60)) {
			return 'il y a ' + (parseInt(delta / 60)).toString() + ' minutes';
		  } else if(delta < (120*60)) {
			return 'il y a environ une heure';
		  } else if(delta < (24*60*60)) {
			return 'il y a environ ' + (parseInt(delta / 3600)).toString() + ' heures';
		  } else if(delta < (48*60*60)) {
			return 'il y a un jour';
		  } else {
			return 'il y a ' + (parseInt(delta / 86400)).toString() + ' jours';
		  }
		}
		
		var url="http://twitter.com/statuses/user_timeline/"+o.userName+".json?callback=?&count="+(o.numTweets);
		$.getJSON(url, function(data){
			$.each(data, function(i, tweet){
				var this_tweet = explode(' ', tweet.text);
				var i;
				var n_medias = 0;
				
				for (i=0; i<count(this_tweet); i++) {
					if (is_numeric(stripos(this_tweet[i], 'http://'))) {
						if (is_numeric(stripos(this_tweet[i], 'twitpic.com')) || is_numeric(stripos(this_tweet[i], 'yfrog.com')) ||
							is_numeric(stripos(this_tweet[i], 'tweetphoto.com')) || is_numeric(stripos(this_tweet[i], 'twitgoo.com')) ||
							is_numeric(stripos(this_tweet[i], 'tinypic.com')) || is_numeric(stripos(this_tweet[i], 'twitsnaps.com')) ||
							is_numeric(stripos(this_tweet[i], 'pikchur.com')) || is_numeric(stripos(this_tweet[i], 'picktor.com')))
						{
							n_medias ++;
							
							if (n_medias > 1) n_media = ' '+n_medias; else n_media = '';
							this_tweet[i] = '<img src="img/icons/mini_media.png" alt="Image" align="absbottom" /><a href="'+ this_tweet[i] +'" onclick="window.open(this.href); return false;"><small>Voir l\'image '+n_media+'</small></a>';
						}
						else
						{
							this_tweet[i] = '<img src="img/icons/mini_link.png" alt="Image" align="absbottom" /><a href="'+ this_tweet[i] +'" onclick="window.open(this.href); return false;"><small>'+ this_tweet[i] +'</small></a>';
						}
					}
				}
				
				this_tweet = implode('&nbsp;', this_tweet);
				var this_date = relative_time(tweet.created_at);
				this_tweet = '<div style="width=350;word-wrap: break-word;">'+ this_tweet +'</div><a href="http://twitter.com/'+o.userName+'/statuses/'+tweet.id+'" onclick="window.open(this.href); return false;"><small>'+ this_date +'</small></a><span class="twitter_close_button">x</span>';
				
				// remove preLoader from container element
				$(pl).remove();
				$("ul#twitter_update_list").show();
				$("ul#twitter_update_list").html('<li>'+this_tweet+'</li>');
				
				$('span.twitter_close_button').click(function() {
					this_this.fadeOut('slow');
				});
			});
		});
		
		
	};

	// plugin defaults
	$.fn.getTwitter.defaults = {
		userName: null,
		numTweets: 5,
		preloaderId: "preloader",
		loaderText: "Chargement en cours..."
	};
})(jQuery);
