var tweetUsers = [''];
// The twitter accounts that will be included in the ticker

var buildString = "";

$(document).ready(function(){

	// After the page is loaded

	$('#twitter-ticker').slideDown('slow');
	// Show the ticker

	for(var i=0;i<tweetUsers.length;i++)
	{
		// Build the search api parameters
		if(i!=0) buildString+='+OR+';
		buildString+='from:'+tweetUsers[i];
	}

	var fileref = document.createElement('script');
	// Creating a new script element

	fileref.setAttribute("type","text/javascript");
	fileref.setAttribute("src", "http://api.twitter.com/1/statuses/user_timeline.json?id=readswart&callback=TweetTick");
	// Setting its src to the search API URL; We provide TweetTick as a callback

	document.getElementsByTagName("head")[0].appendChild(fileref);
	// Appending it to the head of the page and thus executing it
});

function TweetTick(ob)
{
	// This is the callback function

	var container=$('#tweet-container');
	container.html('');
	
	$(ob).each(function(el){
	
		var str = '	<div class="tweet">\
					<div class="avatar"><a href="http://twitter.com/'+this.user.screen_name+'" target="_blank"><img src="'+this.user.profile_image_url+'" alt="'+this.user.name+'" /></a></div>\
					<div class="user"><a href="http://twitter.com/'+this.user.screen_name+'" target="_blank">'+this.user.name+'</a></div>\
					<div class="time">'+relativeTime(this.created_at)+'</div>\
					<div class="txt">'+formatTwitString(this.text)+'</div>\
					</div>';
		
		container.append(str);
	
	});
	
	container.jScrollPane();
}

function formatTwitString(str)
{
	// This function formats the tweet body text

	str=' '+str;

	str = str.replace(/((ftp|https?):\/\/([-\w\.]+)+(:\d+)?(\/([\w/_\.]*(\?\S+)?)?)?)/gm,'<a href="$1" target="_blank">$1</a>');
	// The tweets arrive as plain text, so we replace all the textual URLs with hyperlinks

	str = str.replace(/([^\w])\@([\w\-]+)/gm,'$1@<a href="http://twitter.com/$2" target="_blank">$2</a>');
	// Replace the mentions

	str = str.replace(/([^\w])\#([\w\-]+)/gm,'$1<a href="http://twitter.com/search?q=%23$2" target="_blank">#$2</a>');
	// Replace the hashtags

	return str;
}

function relativeTime(pastTime)
{
	// Generate a JavaScript relative time for the tweets

	var origStamp = Date.parse(pastTime);
	var curDate = new Date();
	var currentStamp = curDate.getTime();
	var difference = parseInt((currentStamp - origStamp)/1000);

	if(difference < 0) return false;

	if(difference <= 5)			return "Precies net";
	if(difference <= 20)			return "Seconden oud";
	if(difference <= 60)			return "1 minuut oud";
	if(difference < 3600)		return parseInt(difference/60)+" minuten oud";
	if(difference <= 1.5*3600) 	return "1 uur oud";
	if(difference < 23.5*3600)	return Math.round(difference/3600)+" uren oud";
	if(difference < 1.5*24*3600)	return "1 dag oud";

	// If the tweet is older than a day, show an absolute date/time value;

	var dateArr = pastTime.split(' ');

	return dateArr[2]+' '+dateArr[1]+ ' ' + dateArr[5].replace(/\:\d+$/,'') +
	(dateArr[3]!=curDate.getFullYear()?' '+dateArr[3]:'');
}
