var fnQueue = [];
var loaded = false;
window.onload = function() {
	loaded = true;
	while(fnQueue.length > 0) {
		(fnQueue.pop())();
	}
}

function queue(fn) {
	if(!loaded) {
		fnQueue.push(fn);
	} else {
		fn.call();
	}
}

queue(function() {
	if (location.host.indexOf("42ab5f") > -1) {
		//document.body.className = "reversed";
	}
});

function twitterCallback(data) {
	queue(function() {
		var html = '';
		for(var i = 0; i < data.length; i++) {
			var tweet = data[i];
			var ts = parse_tweet_date(tweet.created_at);
			html += '<li><span>';
			html += posttext(tweet.text);
			html += '</span> <a href="http://twitter.com/' + tweet.user.screen_name + '/statuses/' + tweet.id + '" title="tweet permalink">';
			html += '<abbr title="' + new Date(ts).toRfc3339() + '">' + relative_time(ts) + '</abbr></a></li>';
		}

		var list = document.createElement('ul');
		list.innerHTML = html;

		var section = document.getElementById('twitter');
		section.insertBefore(list, section.getElementsByTagName('h2')[0].nextSibling);
	});
}

function deliciousCallback(data) {
	queue(function() {
		var html = ''
		for(var i = 0; i < data.length; i++) {
			var link = data[i];
			html += '<li><a href="' + link.u + '" title="' + link.d + '">' + link.d + '</a>';
			html += ' / ';
			for (var j = 0; j < link.t.length; j++) {
				var tag = link.t[j];
				html += '<a class="tag" href="http://delicious.com/badgrs/' + tag + '" title="view links tagged ' + tag + '">' + tag + '</a> ';
			}
			html += '<p class="desc">' + link.n + '</p>';
			html += '</li>';
		}
		
		var list = document.createElement('ul');
		list.innerHTML = html;

		var section = document.getElementById('delicious');
		section.insertBefore(list, section.getElementsByTagName('h2')[0].nextSibling);
	});
}

function lastfmCallback(data) {
	queue(function() {
		var html = '';
	
		var tracks = data.recenttracks.track;
		for(var i = 0; i < tracks.length; i++) {
			var track = tracks[i];
			var text = track.artist['#text'] + ' - ' + track.name;
			var title = text;
			html += '<li';
			if (track["@attr"] && track["@attr"].nowplaying == 'true') {
				html += ' class="playing"';
				title = 'Now playing: ' + title;
			}
			html += '><a href="' + track.url + '" title="' + title + '">' + text + '</a> ';
			if (track.date) {
				var ts = Date.parse(track.date['#text']);
				html += '<span><abbr title="' + new Date(ts).toRfc3339() + '">' + relative_time(ts) + '</abbr></span>';
			}
			html += '</li>';
		}
	
		var list = document.createElement('ul');
		list.innerHTML = html;

		var section = document.getElementById('lastfm');
		section.insertBefore(list, section.getElementsByTagName('h2')[0].nextSibling);
	});
}

/* 
$(document).ready(function() {
	$.getJSON('http://feeds.delicious.com/v2/json/badgrs?count=6&callback=?', function(data) {
		var list = $('<ul></ul>');
		$.each(data, function() {
			var item = $('<li></li>');
			item.append($('<a></a>').attr('href', this.u).attr('title', this.d).text(this.d));
			item.append(' / ')
			$.each(this.t, function() {
				item.append($('<a></a>').addClass('tag').attr('href', 'http://delicious.com/badgrs/' + this).attr('title', 'view links tagged ' + this).text(this.toString()));
				item.append(' ');
			});
			item.append($('<p></p>').addClass('desc').text(this.n));
			list.append(item);
		});
		list.insertAfter('#delicious h2');
	});
	
	$.getJSON('http://twitter.com/statuses/user_timeline/roryf.json?count=1&callback=?', function(data) {
		var list = $('<ul></ul>');
		
		$.each(data, function() {
			var item = $('<li></li>');
			item.append($('<span>' + posttext(this.text) + '</span>'));
			item.append(' ');
			var ts = parse_tweet_date(this.created_at);
			var time = $('<abbr></abbr>').attr('title', new Date(ts).toRfc3339()).text(relative_time(ts));
			item.append($('<a></a>').attr('href', 'http://twitter.com/' + this.user.screen_name + '/statuses/' + this.id).attr('title', 'tweet permalink').append(time));
			list.append(item);
		});
		
		list.insertAfter('#twitter h2');
	});
	
	$.getJSON('http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=badgrs&api_key=63765b941cac69084f3cd682901a8b5d&format=json&callback=?', function(data) {
		var list = $('<ol></ol>');
		
		$.each(data.recenttracks.track, function() {
			var item = $('<li></li>');
			var text = this.artist['#text'] + ' - ' + this.name;
			var title = text;
			if(this.nowplaying == 'true') {
				item.addClass('playing');
				title = 'Now playing: ' + title;
			}
			
			item.append($('<a></a>').attr('href', this.url).attr('title', title).text(text));
			item.append(' ');
			var ts = Date.parse(this.date['#text']);
			var time = $('<abbr></abbr>').attr('title', new Date(ts).toRfc3339()).text(relative_time(ts));
			item.append($('<span></span>').append(time));
			
			list.append(item);
		});
		
		list.insertAfter('#lastfm h2');
	});
});*/

/* 
Pinched from Dojo toolkit dojo.date.toRfc3339.
If the dojotoolkit.org website was working I might post the license text here...
*/

Date.prototype.toRfc3339 = function(dateObject){
        if(!dateObject){
                dateObject = new Date();
        }

        var _ = pad;
        var formattedDate = [];
        var date = [_(dateObject.getFullYear(),4), _(dateObject.getMonth()+1,2), _(dateObject.getDate(),2)].join('-');
        formattedDate.push(date);
        var time = [_(dateObject.getHours(),2), _(dateObject.getMinutes(),2), _(dateObject.getSeconds(),2)].join(':');
        var timezoneOffset = dateObject.getTimezoneOffset();
        time += (timezoneOffset > 0 ? "-" : "+") +
                                _(Math.floor(Math.abs(timezoneOffset)/60),2) + ":" +
                                _(Math.abs(timezoneOffset)%60,2);
        formattedDate.push(time);

        return formattedDate.join('T'); // String
};

function pad(text, size, ch, end) {
	var out = String(text);
    if(!ch){
            ch = '0';
    }
    while(out.length < size){
            if(end){
                    out += ch;
            }else{
                    out = ch + out;
            }
    }
    return out;
};