function processTweet(sScreenName, sUserAvatar, sEncodedText, sId, sCreatedAt, oCloud) {
var sMessageText = Utf8.decode(sEncodedText);
var dTimestamp = new Date(sCreatedAt);
var arrWords = sMessageText.toLowerCase().split(/(\s|\?|\,|\;|\!)+/);

for (var i in arrWords)
{
  (oCloud.add(arrWords[i], dTimestamp));
}

return "<div class='message'>" +
		"	<div class='profile'><a href='http://twitter.com/" + sScreenName + "' target='_blank'>" +
		"		<img alt='" + sScreenName + "' width='48' height='48' src='" + sUserAvatar + "' class='avatar' />" +
		"	<a></div>" +
		"	<div class='messagebody'>" +
		"		<span class='screenName'><a href='http://twitter.com/" + sScreenName + "' target='_blank'>@" + sScreenName + "</a></span>" +
		"		<span class='text'>" + sMessageText + "</span>" +
		"		<span class='time'>at <a target='_blank' href='http://twitter.com/"+sScreenName+"/statuses/"+sId+"'>"+sCreatedAt+"</a></span>" +
		"	</div>" +
		"	<div class='clear' />" +
		"</div>";
}


function TweetListener(oCloud) {
  this.count = 0;
	this.max = 10;
	
	this.topicUpdated = function(oSub, mUpdate) {
			var oData = document.getElementById('tweet_data_'+ this.count);
			var oCloudEl = document.getElementById('tweet_cloud');
			
			oData.innerHTML = processTweet(mUpdate["ScreenName"], mUpdate["UserProfileImageUrl"], mUpdate["Text"], mUpdate["Id"], mUpdate["CreatedAt"], oCloud);
			oCloudEl.innerHTML = oCloud.html();
			this.count = (this.count + 1 ) % this.max;
			
	};

	this.topicError = function(oSub, sError) {
		log("Error: " + sError);
	};
};

function SubscriptionListener() {

  // convert yyyyMMddThhmmss+TZD format to Javascript date object
  function makedate(sDate)
	{
    var dDate = new Date();
		dDate.setFullYear(parseInt(sDate.substr(0,4)),parseInt(sDate.substr(4,2))-1,parseInt(sDate.substr(6,2)));
		dDate.setUTCHours(parseInt(sDate.substr(9,2))-1,parseInt(sDate.substr(11,2)),0,0);
		return (dDate);
	}
		
	function statsRow(sProperty, sCaption, mUpdate) {
	  var sHTML ="<tr class='stats'><td>";
		if (parseInt(mUpdate["MatchTime"]) > 0)
		{
  		if (mUpdate["HomeTeam"+sProperty])
  		{
  		  sHTML += mUpdate["HomeTeam"+sProperty] + "</td><td colspan='3'>" + sCaption + "</td><td>";
  		}
  		else
  		{ 
  		  sHTML += "&nbsp;</td><td colspan='3'>" + sCaption + "</td><td>";
  		}
  		if (mUpdate["AwayTeam"+sProperty])
  		{
  		  sHTML += mUpdate["AwayTeam"+sProperty] + "</td></tr>";
  		}
  		else
  		{
  		  sHTML += "&nbsp;</td></tr>";
  		}
		}
		else
		{
		  sHTML += "&nbsp;</td><td colspan='3'>" + sCaption + "</td><td>&nbsp;</td></tr>";
		}
		return sHTML;
	}
	
	this.topicUpdated = function(oSub, mUpdate) {
			var oData = document.getElementById('match_data');
			var oMatch = document.getElementById('match_details');
			
			var sMatchTime = mUpdate["MatchTime"];
			var sMatchPeriod = mUpdate["MatchPeriod"];
			var sVenue = mUpdate["Venue"];
			var sKickoff = mUpdate["MatchDate"];
			var dKickoff = makedate(sKickoff);
			var iAttendance = mUpdate["MatchAttendance"];
			
			var sDetails = sMatchPeriod + " (" + sMatchTime + "mins), "+ sVenue + "<br><nobr>Kickoff: " + dKickoff.toLocaleString() +"</nobr>";
			oMatch.innerHTML = sDetails;
			
			var sHTML = "<table><tr id='headline'><td>" + mUpdate["HomeTeam"] + "</td><td>";
			if (mUpdate["HomeTeamScore"])
			{
			  sHTML += mUpdate["HomeTeamScore"] + "</td><td>-</td><td>";
			}
			else
			{
			  sHTML += "0</td><td>-</td><td>";
			}
			if (mUpdate["AwayTeamScore"])
			{
			  sHTML += mUpdate["AwayTeamScore"] +"</td><td>";
			}
			else
			{
			  sHTML += "0</td><td>";
			}
			sHTML += mUpdate["AwayTeam"] + "</td></tr>\n";
			sHTML += statsRow("TotalScoringAtt", "Total shots", mUpdate);
			sHTML += statsRow("OntargetScoringAtt", "Shots on target", mUpdate);
			sHTML += statsRow("PossessionPercentage", "Possession %", mUpdate);
			sHTML += statsRow("WonCorners", "Corners", mUpdate);
			sHTML += statsRow("FkFoulLost", "Fouls committed", mUpdate);
			sHTML += statsRow("TotalPass", "Passes completed", mUpdate);
			sHTML += statsRow("TotalLongBalls", "Long balls", mUpdate);
			sHTML += statsRow("AccurateLongBalls", "Accurate long balls", mUpdate);
			sHTML += statsRow("GoalKicks", "Goal kicks", mUpdate);
			sHTML += statsRow("TotalThrows", "Throw-ins", mUpdate);
			sHTML += statsRow("Interception", "Interceptions", mUpdate);
			sHTML += statsRow("SubsMade", "Substitutions", mUpdate);
			sHTML += statsRow("TotalYelCard", "Yellow cards", mUpdate);
			sHTML += "</table>\n";
			oData.innerHTML = sHTML;
			
	};

	this.topicError = function(oSub, sError) {
		log("Error: " + sError);
	};
};


/**
*
*  UTF-8 data encode / decode
*  http://www.webtoolkit.info/
*
**/
 
var Utf8 = {
 
	// public method for url encoding
	encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// public method for url decoding
	decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}

function cloudElement(sWord, dTime) {
  this.Word = sWord;
	this.Timestamp = dTime;
	this.Freq = 1;

}

