/*

Requires:
	/js/jquery*.js
	/js/date.format.js

*/


var rwmTweets = {

	/*XmlProxy: "/GetText.asp?url="
,*/

	Test: function () {
		alert("rwmTweets is loaded");
	}
,

	Feed: function (url, callback) {
		var me = this;
		this.data = null;

		var xmlhttp = $.ajax({
			/*url: rwmTweets.XmlProxy + url,*/
			url: url,
			dataType: "jsonp",
			type: "GET",
			success: function (json, result) {
				me.data = eval(json);
				callback();
			}
		});

		this.OutputHtml = function (jqo, count, title, link) {
			jqo.hide();

			var div = $(document.createElement("div"));

			if (this.data == null || this.data.length == 0) {
				div.append("<h3>No tweets</h3>");
			}
			else {
				var a1 = a2 = "";
				if (link) {
					a1 = "<a href='" + link + "'>";
					a2 = "</a>";
				}
				if (title) div.append("<h3>" + a1 + title + a2 + "</h3>");
				div.append("<p style='text-align: center'>" + a1 + "<img src='" + this.data[0].user.profile_image_url + "' title='" + this.data[0].user.screen_name + "'/>" + a2 + "</p>");

				for (var i = 0; i < Math.min(this.data.length, count); i++) {
					var d = this.data[i];

					// Fix for twitter's non-standard dates: http://stackoverflow.com/questions/2611415/parsing-twitter-api-datestamp
					var date = new Date(d.created_at.replace(/^\w+ (\w+) (\d+) ([\d:]+) \+0000 (\d+)$/, "$1 $2 $4 $3 UTC"));
					
					var text2 = d.text;
					
					if (d.retweeted_status)
					{
						// Build text from original to avoid a possibly truncated version
						text2 = "RT @" + d.retweeted_status.user.screen_name + ": " + d.retweeted_status.text;
					}
					// Try to do something with links in the text
					var text2 = text2.replace(/(http:\/\/[^ ]*)/g, "<a href='$1'>link</a>");
					
					var p = $(document.createElement("p"));
					p.append("<p><span>" + date.format("ddd d mmm yyyy HH:MM") + "</span><br/>" + text2 + "</p>");
					p.hide().delay(1000 + i * 400).fadeIn(800);
					div.append(p);
				}

			}

			jqo.append(div);
			jqo.fadeIn(500);
		};

	}

};

/*

[
	{
		"created_at":"Mon Jul 26 23:05:01 +0000 2010",
		"in_reply_to_screen_name":"beerhaiku",
		"user":{
			"profile_image_url":"http://a0.twimg.com/profile_images/78053092/EdTheDuck_85px_normal.gif",
			"screen_name":"raithy",
			"name":"raith munro",
			"id":20806855,
		},
		"id":19609220042,
		"text":"@beerhaiku Only five percent? I think i could go higher! Get me to Belgium...",
		"retweet_status":{
			"screen_name":"tweeter",
			"text":"this is the original tweet but you need to add RT @screen_name: http://if/lucky/this/will/even/work!",
		}
	},
]

*/
