//Start Twitter
$.fn.extend({
      linkUrl: function() {
        var returning = [];
        var regexp = /((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi;
        this.each(function() {
          returning.push(this.replace(regexp,"<a href=\"$1\">$1</a>"));
        });
        return $(returning);
      } });

// Need searchquery to be a global variable
function loadTwitter(searchquery) {
	// Once we have the query, send to twitter and get the results
	$.getJSON("http://search.twitter.com/search.json?q="+searchquery+"&rpp=1&lang=en&callback=?", function(data){ 
		//The query above limits the results to just one...but just in case multiple come back...
		$.each(data['results'], function(i,item){
			//Figure out if the returned tweet is already being displayed
			if ($("#twitter > .tweetdetails > .tweet").html() !== item.text) {
			//If it's a new tweet, fade out the old, fade in the new...but not until the previous is done fading out!
			$("#twitter > .tweetdetails > .username").fadeOut("fast", function(){$("#twitter > .tweetdetails >  .username").html(item.from_user);$("#twitter >.tweetdetails > .username").fadeIn("fast");});
			$("#twitter > .tweetdetails > .tweet").fadeOut("fast", function(){$("#twitter > .tweetdetails > .tweet").html(item.text); $("#twitter > .tweetdetails > .tweet").fadeIn("fast");});
			$("#twitter > img ").fadeOut("fast", function(){ $("#twitter > img").attr('src', item.profile_image_url);$("#twitter >  img ").fadeIn("fast"); });
			$("#twitter > .tweetdetails > .timeago").fadeOut("fast", function(){$("#twitter > .tweetdetails > .timeago").html(relative_time(item.created_at)); $("#twitter > .tweetdetails > .timeago").fadeIn("fast");});
			} else if ($("#twitter > .tweetdetails > .timeago").html() !== relative_time(item.created_at)) {
			// If the tweet returned is the same being displayed, then just update how long ago it was posted, of course beautifully fading out and then back in.  jQuery I love you.
			$("#twitter > .tweetdetails > .timeago").fadeOut("fast", function(){$("#twitter > .tweetdetails > .timeago").html(relative_time(item.created_at)); $("#twitter > .tweetdetails > .timeago").fadeIn("fast");});
			}
		});
	});


}; //End loadTwitter

// This function displays in friendly english how long ago the displayed twitter message was posted
    function relative_time(time_value) {
      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);
      if(delta < 60) {
      return 'less than a minute ago';
      } else if(delta < 120) {
      return 'about a minute ago';
      } else if(delta < (45*60)) {
      return (parseInt(delta / 60)).toString() + ' minutes ago';
      } else if(delta < (90*60)) {
      return 'about an hour ago';
      } else if(delta < (24*60*60)) {
      return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
      } else if(delta < (48*60*60)) {
      return '1 day ago';
      } else {
      return (parseInt(delta / 86400)).toString() + ' days ago';
      }
    }

