(function($) {
    $.fn.extend({
        twitter: function(username, list) {
            var self = $(this);
            var url = "http://api.twitter.com/1/" +
                    (!!list ? encodeURIComponent(username) + "/lists/" + encodeURIComponent(list) + "/statuses" :
                            "statuses/user_timeline/" + encodeURIComponent(username)) + ".json";

            $.ajax({
                url: url,
                data: {
                    count: 20
                },
                success: function(data) {
                    for (var i = 0, a = data.length; i < a; i++) {
                        var item = data[i];
                        var className = "tweet";
                        if ("retweeted_status" in data[i]) {
                            item = item.retweeted_status;
                            className = "retweet";
                        }

                        self.append($("<li class='" + className + "'/>").html("<a href='http://twitter.com/" + item.user.screen_name + "' class='tweet-author'>" + item.user.screen_name + "</a> " + item.text
                                .replace(/((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?Â«Â»â€œâ€â€˜â€™]))/gi, "<a href='$1' class='tweet-link'>$1</a>")
                                .replace(/@([a-z0-9-_]+)/gi, "<a href='http://twitter.com/$1' class='tweet-user'>@$1</a>") +
                                " <a href='http://twitter.com/" + item.user.screen_name + "/status/" + item.id_str + "' class='tweet-permalink'>#</a>"));
                    }

                    var sh = 0;
                    self.siblings().each(function() { sh += $(this).outerHeight(); })
                    self.height(self.closest("div").innerHeight() - sh);
                },
                dataType: "jsonp"
            });

            return this;
        },

        pipe: function() {
            var self = $(this);

            $.ajax({
                url: "http://pipes.yahoo.com/pipes/pipe.run?_id=ead178c13ef55fbcac3f05afd54ba066&_render=json&_callback=?",
                success: function(data) {
                    for (var i = 0, a = Math.min(data.count, 5); i < a; i++) {
                        var item = data.value.items[i];
                        $("<li><a href='" + item.link + "'>" + item.title + "</a></li>").appendTo(self);
                    }
                },
                dataType: "jsonp"
            });

            return this;
        }
    });

    $("#about .note ul").pipe();
    $("#portfolio .twitter ul").twitter("hoopeekoo", "lifeline-companies");
    $("#team .twitter ul").twitter("hoopeekoo", "lifeline-ventures");
})(jQuery);
