//
// Twitter jQuery plugin
// Paul Philippov, <themactep@gmail.com>
// Last Change: 21-Mar-2010.
//

(function($){
  $.fn.extend({
    twitter: function(username) {

      // exit is caller element does not exists
      if (this.length == 0) return false;

      // debug with Firebug
      if (typeof(console) != 'undefined') {
        console.log("Flickr plugin loaded");
        console.log(this);
      }

      if (this.length == 0) return false;
      if (typeof(console) != 'undefined') {
        console.log("Twitter plugin loaded");
        console.log(this);
      }

      $.ajax({
        url: 'http://www.twitter.com/statuses/user_timeline.json',
        data: { 'id': username, 'callback': 'twitterCallback' },
        dataType: 'jsonp',
        jsonp: 'fake_callback',
        cache: true,
        ifModified: true
      });
      return true;
    }
  });

  twitterCallback = function(data){
    var twit = data[0];
    var d = twit.created_at.split(" ");
    var t = d[3].split(":");
    var date = d[2] + ' ' + d[1] + ' ' + d[5] + ', ' + t[0] + ':' + t[1];
    $("#twitter em").append(', ' + date);
    $("#twitter a").attr("src", "http://twitter.com/" + twit.user.screen_name);
    $("#twitter").append('<p>' + twit.text + '</p>');
  };
})(jQuery);
