(function($){
  $.fn.twitterFeed = function(user, count) {
    var $this = $(this);

    function convertTime(time) {
        var time = time.split(':'), half;

        time[0] = (parseInt(time[0], 10) - 7);
        if (time[0] < 0) { time[0] = time[0] + 24}

        half = (time[0] >= 12 ? 'PM' : 'AM' );
        if (time[0] > 12) { time[0] = time[0] - 12 }
        if (time[0] == 0) { time[0] = 12 }

        return time[0] + ":" + time[1] + " " + half;
    };

    $.getJSON('http://twitter.com/statuses/user_timeline/' + user + '.json?count=' + count + '&callback=?', function(data){
        var url_regexp = /((?:https?:\/\/)(?:[-\w\.]+)+(?::\d+)?(?:\/(?:[\w\/_\.]*(?:\?\S+)?)?)?)/g,
            monthnames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
        $this.empty();

        $.each(data, function(i, val) {
            date_array = val.created_at.split(' ');
            date = date_array[1] + " " + date_array[2].replace(/^0([0-9])$/, "$1") + " " + convertTime(date_array[3])
            $this.append("<li>" + val.text.replace(url_regexp, "<a class='twitterlink' href='$1' target='_new'>$1</a>").replace(/\@(\w+)/g, "<a class='twitterlink' href='http://twitter.com/$1' target='_new'>@$1</a>") + "<br><span class='timestamp'>" + date + "</span>" + "</li>");
        });
    });
  }

})(jQuery);
