////////////////////////////////////////////////////////////////////////////////////////////////////
// Module: Twitter
//
function CTwitterBox(boxid) {
	this.base_url = 'http://api.twitter.com';
	this.action = '';
	this.params = '';
	this.box = $('#'+boxid);
	this.username = null;
	this.noftweets = 0;
	this.showinterval = 2000;
	this.nofloops = 30;
	this.currentloop = null;
	this.tweets = null;
	this.tweetsview = null;
	this.currenttweet = null;
	this.showtimer = null;
	this.months = Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
	
	if (!this.box) { alert('No HTML object with id ' + boxid + ' found') };

}

CTwitterBox.prototype.getTweets = function () {
	if (this.action == '') { alert('No action defined! Do not know what to do.'); return; }
	
	var receiver = this; // 'this' can not be used in a handler routine of another object
	var params = 'callback=?';
	if (this.params != '') params += '&' + this.params;
	
	$.getJSON(
		this.base_url + this.action,
		params,
		function (data) {
			receiver.processTweets(data);
		}
	);
}

CTwitterBox.prototype.processTweets = function (data) {
	var tweet;
	var retweet;
	var tweetdate;
	
	if (data && data.length) {
		this.tweets = data;
		this.currenttweet = null;
		this.tweetsview = Array(data.length);
		
		for (var idx = 0; idx < data.length; idx++) {
			tweet = this.tweets[idx];
			
			var usertweetlink = '<span class="tweetheader">';
			if (typeof(tweet.retweeted_status) != 'undefined') {
				retweet = tweet.retweeted_status;
				usertweetlink += '<a href="http://twitter.com/' + retweet.user.screen_name + '" target="_blank">' + retweet.user.screen_name + '</a>';
				usertweetlink += ' Retweeted by ';
			} else {
				retweet = false;
			}
			
			usertweetlink += '<a href="http://twitter.com/' + tweet.user.screen_name + '" target="_blank">' + tweet.user.screen_name + '</a>';
			usertweetlink += '</span>';
			
			if (retweet) tweet = retweet;
			
			txt = tweet.text;
			txt = txt.replace(/(http:\/\/\S+)/, '<a href="$1" target="_blank">$1</a>');
			
			var created = tweet.created_at.replace(/(\+\S+) (.*)/, '$2 $1');
			tweetdate = new Date(Date.parse(created));
			
			txt += '<br />';
			txt += '<span class="tweetfooter">';
			txt += tweetdate.getDate() + ' ' + this.months[tweetdate.getMonth()];

			txt += ' - ';

			txt += '<a href="http://twitter.com/?status=@' + tweet.user.screen_name + '%20&in_reply_to_status_id=' + tweet.id + 'in_reply_to=' + tweet.user.screen_name + '" target="_blank">';
			txt += 'Beantwoorden';
			txt += '</a>';

			txt += '</span>'

			contents = "";
			contents += "<table cellpadding='0' cellspacing='0' class='tweet'>";
			contents += "<tr>";
			contents += "<td class='tweeticon'><img alt='" + tweet.user.name + "' title='" + tweet.user.name + "' src='" + tweet.user.profile_image_url + "' /></td>";
			contents += "<td class='tweettext'>" + usertweetlink + '<br />' + txt + "</td>";
			contents += "</tr>";
			contents += "</table>";

			this.tweetsview[idx] = $('<div class="tweet">'+contents+'</div>');
		}
		
		this.box.empty();
	}
	
	this.showTweets();
}

CTwitterBox.prototype.refresh = function () {
	if (this.showtimer != null) {
		clearInterval(this.showtimer);
		this.showtimer = null;
	}
	
	this.currentloop = null;
	this.getTweets();
}

CTwitterBox.prototype.showTweets = function () {
	if (this.tweets == null || this.tweets.length == 0) return;
	
	for (var idx = 0; idx < this.toshow; idx++) {
		if (this.currenttweet == null) {
			this.currenttweet = 0;
		} else {
			this.currenttweet++;
		}
		
		if (this.currenttweet >= this.tweets.length) {
			this.currenttweet = 0;

			if (this.currentloop == null) {
				this.currentloop = 0;
			} else {
				this.currentloop++;
				if (this.currentloop == this.nofloops) {
					this.refresh();
					return;
				}
			}
		}
		
		tweetbox = this.tweetsview[this.currenttweet];
		this.box.prepend(tweetbox);
		//this.box.children(':nth-child(4)').css('background-color', 'red');
		this.box.children(':nth-child(5)').remove();
		var h = tweetbox.height();
		tweetbox.css('height', h + 'px');
		//alert(tweetbox.height());
		tweetbox.hide();
		tweetbox.slideDown(1000);
	}
	
	if (this.showtimer == null) {
		var shower = this;
		this.showtimer = setInterval(function () {
			shower.showTweets();
		}, this.showinterval);
	}
}

CTwitterBox.prototype.showUserTimeline = function (username, noftweets, toshow) {
	if (!this.box) return;
	
	if (typeof(noftweets) == 'undefined') noftweets = 0;
	if (typeof(toshow) == 'undefined' || toshow < 1) toshow = 1;
	
	this.username = username;
	this.noftweets = noftweets;
	this.toshow = toshow;
	
	this.action = '/1/statuses/user_timeline.json';
	this.params = 'screen_name='+this.username;
	this.params += '&include_rts=true';
	if (this.noftweets > 0) this.params += '&count='+this.noftweets;
	
	this.refresh();
}


