// init.js
function loadTwitterFeed()
{
    var url = "http://twitter.com/status/user_timeline/benwong.json?count=5&callback=?";
    $.getJSON(url,
    function(data) {
        $.each(data,
        function(i, item) {
            //$("img#profile").attr("src", item.user["profile_image_url"]);
            $("#twitter ul").append("<li class='ui-corner-all'>"
            + item.text.linkify()
            + "<span class='meta' title='created at'>"
            + relative_time(item.created_at)
            + "<\/span><\/li>");
        });

        $("#twitter ul").fadeIn("slow");
        $("#twitter div.loading").css("display", "none");
        $("#twitter a.more").fadeIn("slow");
    });

    String.prototype.linkify = function() {
        return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/,
        function(m) {
            return m.link(m);
        });
    };
}

function loadDeliciousFeed()
{
    var url = "http://feeds.delicious.com/v2/json/ben.wong?count=5&callback=?";

    $.getJSON(url,
    function(data) {
        $.each(data,
        function(i, item) {
            var tags = "";

            for (var j = 0; j < item.t.length; j++)
            {
                tags = (j == item.t.length - 1) ? tags + item.t[j] : tags + item.t[j] + ", ";
            }

            $("#delicious ul").append("<li class='ui-corner-all'>"
            + "<a href='" + item.u + "'>" + item.d + "<\/a>"
            + "<span class='meta' title='tags'>" + tags + "<\/span>"
            + "<\/li>");
        });

		$("#delicious ul").fadeIn("slow")
	    $("#delicious div.loading").css("display", "none");
	    $("#delicious a.more").fadeIn("slow");
    });
}

function relative_time(time_value) 
{
    var values = time_value.split(" ");
    time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
    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);
    delta = delta + (relative_to.getTimezoneOffset() * 60);

    var r = '';
    if (delta < 60) {
        r = 'a minute ago';
    } else if (delta < 120) {
        r = 'couple of minutes ago';
    } else if (delta < (45 * 60)) {
        r = (parseInt(delta / 60)).toString() + ' minutes ago';
    } else if (delta < (90 * 60)) {
        r = 'an hour ago';
    } else if (delta < (24 * 60 * 60)) {
        r = '' + (parseInt(delta / 3600)).toString() + ' hours ago';
    } else if (delta < (48 * 60 * 60)) {
        r = '1 day ago';
    } else {
        r = (parseInt(delta / 86400)).toString() + ' days ago';
    }

    return r;
}

function loadFlickrFeed() 
{
    var url = "http://api.flickr.com/services/feeds/photos_public.gne?id=51035646340@N01&format=json&jsoncallback=?";

    $.getJSON(url,
    function(data) {
        $.each(data.items,
        function(i, item) {
			if (i == 0)
			{
            	$("#flickr ul").append("<li class='ui-corner-all'>"
	            	+ "<a href='" + item.link + "' class='img'>"
		            + "<img src='" + item.media.m + "' alt='" + item.title + "' />"
		            + "<\/a>"
		            + "<\/li>"
	            	);
			}
        });

	    $("#flickr ul").fadeIn("slow");
	    $("#flickr div.loading").css("display", "none");
	    $("#flickr a.more").fadeIn("slow");
    });
}

$(document).ready(function() {
    loadTwitterFeed();
    loadDeliciousFeed();
    loadFlickrFeed();
});