/*
 * body onload should call callTwitter() and twitterIsLoaded(tweets) should be implemented to proces the tweets.
 */

/*
 * When using twitter, body.onload should call this function.
 * name - The twitter page.
 * numberOfTweets - How many tweets should be loaded. Don't load what you don't need.
 * includeRetweets - A boolean that decides if retweets should be included.
 */
function callTwitter(name, numberOfTweets, includeRetweets) {
	var n = document.createElement('script');
	var p = document.getElementsByTagName('head')[0];
	n.type='text/javascript';
	n.src = 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=' + name + '&callback=twitterCallback' + (includeRetweets ? '&include_rts=true' : '') + '&count=' + numberOfTweets + '&clientsource=TWITTERINC_WIDGET' + '&' + (+new Date) + '=cachebust';
	p.insertBefore(n, p.firstChild);
}

/*
 * The javascript loaded from the twitter api will call this function.
 * The tweets are formatted and the function twitterIsLoaded(tweets) is called.
 * Implement twitterIsLoaded(tweets) to use this.
 */
function twitterCallback(response) {
	if (response.length > 0) {
		// Format tweets
		var tagChars = new Array('#', '@');
		var endingChars = new Array('.', ' ', '?');		
		for (var i in response) {
			response[i].screen_name = response[i].user.screen_name;
			response[i].name = response[i].user.name;
			response[i].tweet = response[i].text;
			response[i].rfc2822 = response[i].created_at;
			
			var tweet = response[i].tweet;
			var output = '';
			var pos = indexOfAny(tweet, tagChars);
			while (pos != -1) {
				output += tweet.substring(0, pos);
				var end = indexOfAny(tweet, endingChars, pos);
				var tag = (end == -1 ? tweet.substr(pos) : tweet.substring(pos, end));
				var firstChar = tag.substring(0, 1);
				if (firstChar == '#')
					tag = '<a href="http://twitter.com/#!/search/' + tag.substr(1) + '" target="_blank" class="hashtag">' + tag + '</a>';
				else if (firstChar == '@')
					tag = '<a href="http://twitter.com/' + tag.substr(1) + '" target="_blank" class="at">' + tag + '</a>';
				output += tag;
				tweet = (end == -1 ? '' : tweet.substr(end));
				pos = indexOfAny(tweet, tagChars);
			}
			response[i].tweet = output + tweet;
			
			var d = response[i].created_at.split(' ');
			var date = new Date(Date.parse(d[1] + ' ' + d[2] + ', ' + d[5] + ' ' + d[3] + ' UTC'));
			var month = date.getMonth() + 1;
			if (month < 10)
				month = '0' + month;
			response[i].date = date.getFullYear() + '-' + month + '-' + date.getDate();
			var hours = date.getHours();
			var minutes = date.getMinutes();
			var seconds = date.getSeconds();
			if (hours < 10)
				hours = '0' + hours;
			if (minutes < 10)
				minutes = '0' + minutes;
			if (seconds < 10)
				seconds = '0' + seconds;
			response[i].time = hours + '-' + minutes + '-' + seconds;
			
			var secondsDiff = Math.floor((new Date().getTime() - date.getTime()) / 1000);
			response[i].days = parseInt(secondsDiff / 86400);// a day has 86400 seconds
			response[i].hours = parseInt((secondsDiff / 3600) % 24);// a hour has 3600 seconds
			response[i].minutes = parseInt((secondsDiff / 60) % 60);
			response[i].seconds = secondsDiff % 60;
		}
	}
	twitterIsLoaded(response);
}

/*
 * Works like indexOf for multiple characters.
 */
function indexOfAny(text, chars, start) {
	if (start != undefined)
		text = text.substr(start);
	var firstPos = undefined;
	var pos = undefined;
	for (var i in chars) {
		pos = text.indexOf(chars[i]);
		if (pos != -1 && (firstPos == undefined || pos < firstPos))
			firstPos = pos;
	}
	if (firstPos == undefined)
		return -1;
	else
		return firstPos + (start == undefined ? 0 : start);
}
