(function($) {
 
  $.fn.tweet_status = function(o){
    var s = {
      username: "ficoba",                     // [string]   required, unless you want to display fidor tweets. :)
      count: 3,                               // [integer]  how many tweets to display?
      maxlength: 55,                          // [integer]  how many chars of a message to display at maximum?
      loading_text: null,                     // [string]   optional loading text, displayed while tweets load
      secure: true                            // [boolean]  use SSL?
    };

    function time_since(time_value) {
      var parsed_date = Date.parse(time_value);
      var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
      var since = parseInt((relative_to.getTime() - parsed_date) / 1000);
      var print = '';
      
      var chunks = new Array(
        new Array(60 * 60 * 24 * 365 , 'Jahr', 'Jahre'),
        new Array(60 * 60 * 24 * 30 , 'Monat', 'Monate'),
        new Array(60 * 60 * 24 * 7, 'Woche', 'Wochen'),
        new Array(60 * 60 * 24 , 'Tag', 'Tage'),
        new Array(60 * 60 , 'Stunde', 'Stunden'),
        new Array(60 , 'Minute', 'Minuten')
      );
      
      // search for highest date val
      for (var i = 0; i < chunks.length; i++) {
      
        var seconds = chunks[i][0];
        var name = chunks[i][1];
        var name_pl = chunks[i][2];
        
        // finding the biggest chunk (if the chunk fits, break)
        var count = Math.floor(since / seconds);
        if (count != 0) {
          // DEBUG alert(name);
          print = (count == 1) ? '1 ' + name : count + ' ' + name_pl
          break;
        }
      }
      
      if ((i + 1) < chunks.length) {
        // now getting the second item
        var seconds2 = chunks[i + 1][0];
        var name2 = chunks[i + 1][1];
        var name2_pl = chunks[i + 1][2];
        
        // add second item if it's greater than 0
        var count2 = Math.floor((since - (seconds * count)) / seconds2);
        if (count2 != 0) {
          print += (count2 == 1) ? ', 1 ' + name2 : ', ' + count2 + ' ' + name2_pl;
        }
      }
      return print;      
    }
    
    function truncate(text, length) {
      if (text.length > length) {
        text = text.substring(0, length);
        text = text.replace(/\w+$/, '');
        text = text.replace(/\s+$/, '...');
      }
      
      return text;
    }
    

    if(o) $.extend(s, o);
    return this.each(function(){
      var rootElem = $(this);
    
      var loading = $('<p class="loading">' + s.loading_text + '</p>');
      if (s.loading_text) {
        $(this).append(loading);
      }
      
      var url = (s.secure ? 'https' : 'http') + '://twitter.com/statuses/user_timeline/' + s.username + '.json?&callback=?'
      
      $.getJSON(url, function(data){
        if (s.loading_text) {
          loading.remove();
        } 
        $.each(data, function(i, item){
          if (i > (s.count - 1)) return false; // limit
        
          var text = truncate(item.text, s.maxlength);
          var date_since = time_since(item.created_at);
          
          var tweet = $('<p class="twitter"><a target="_blank" href="http://twitter.com/' + s.username + '">' + text + '</a></p><p class="twitterdate bottomline">Vor ' + date_since + '</p>');
          
          rootElem.append(tweet);
        });
      });
    });
  };
})(jQuery);
