/**
 * Twitter Feed Reader -- JavaScript Constructor
 *
 * @author: Rahmin Pavlovic
 * @date: 03 June 2008
 * @version: 03 April 2009
 *
 * copyright (c) 2008, Crain Communications
 *
 */

var twitterWdgt = null;
function TwitterWidget() {

	// configurable variables
	this.tweet_limit = 5;
	this.return_value = 'json';
	this.path = 'creativity-online';
	this.feedHash = 'superbowl_ads';
	this.container_id = 'twitter_creativity';
	this.loading_msg = 'Loading tweets...';
	this.no_results_msg = 'No one\'s saying anything yet :(';

	// "private" variables
	this.html = [];
	//this.hashes = [];
	this.response = null;

	this.output = function(html) {
		DOM.getElementById(this.container_id).innerHTML = html;
	};

	this.prepFeed = function() {
		this.output('<div align="center">' + this.loading_msg + '<br /><br /><img src="/images/global/ajax-wheel-036.gif" width="32" height="32" alt="" /></div>');
	};

	this.initialize = function(count) {

		// display loading message before fetching feed
		this.prepFeed();

		if(count) {
			this.tweet_limit = count;
		}

		/*
		if(this.hashes.length === 0) {
			this.getFeed();
		}
		*/
		this.getFeed();
	};
	
	this.matchAll = function(string){
		var pattern = new RegExp('@[^:\s<]*[:\s<]','g');
		var matches = [];
		matches = string.match(pattern);
		for (i in matches) {
			if(typeof matches[i] == 'string') {
				matches[i] = matches[i].substring(0,matches[i].length - 1);
			}
		}
		return matches;
	},
	
	this.str_replace = function(needle, replacement, haystack) {
		var temp = haystack.split(needle);
		return temp.join(replacement);
	},
	
	this.autolink = function(str) {
		var hlink = /\s(ht|f)tp:\/\/([^ \,\;\:\!\)\(\"\'\<\>\f\n\r\t\v\s])+/g;
		return str.replace(
			hlink, 
			function ($0, $1, $2) {
				s = $0.substring(1, $0.length);
				var cond_return = '';
				while(s.charAt(s.length-1) == '.'){
					s = s.substring(0,s.length-1);
					var cond_return = '.';
				}
				while (s.length > 0 && s.charAt(s.length-1) == '.') {
					s = str.substring(0, s.length-1);
				}
				return " " + s.link(s)+cond_return; 
			}
		);
	},

	this.formatFeed = function(str) {
		users = this.matchAll(str);
		if(users) {
			for(var i=0; i<users.length; i++) {
				str = this.str_replace(users[i], '<a href="http://twitter.com/' + users[i] + '">' + users[i] + '</a>', str);
			}
		}
		return this.autolink(str);
	};

	this.formatSource = function(str) {
		str = str_replace('&lt;', '<', str);
		str = str_replace('&gt;', '>', str);
		str = str_replace('&quot;', '"', str);
		return str;
	}; 

	this.getFeed = function() {

		// store object instance
		var twitterWdgt = this;

		// fetch feed
		new AJAX.FileRequest(
			'/ajax/get_twitter_hashtag.php',
			function() {
				if(this.request && this.response) {

					twitterWdgt.response=JSON.parse(this.response);
			
					if(twitterWdgt.response.results.length === 0) {
						twitterWdgt.html.push('<div align="center">', twitterWdgt.no_results_msg, '</div>');
					}else {
					
						for(var j=0; j<twitterWdgt.response.results.length; j++) {
						
							twitterWdgt.html.push('<div id="twitter_tweet_', twitterWdgt.feedHash, j, '" class="twitter_tweet">');
							
							twitterWdgt.html.push(twitterWdgt.formatFeed(twitterWdgt.response.results[j].text));
					
							twitterWdgt.html.push('</div>');

							twitterWdgt.html.push('<div class="twitter_tweet_created">');

							twitterWdgt.html.push('<em><a href="http://twitter.com/', twitterWdgt.response.results[j].from_user ,'/status/', twitterWdgt.response.results[j].id,'">', twitterWdgt.response.results[j].created_at, '</a>');

							//twitterWdgt.html.push(', from ', twitterWdgt.formatSource(twitterWdgt.response.results[j].source));
						
							if(twitterWdgt.response.results[j].from_user != null) {

								twitterWdgt.html.push(' by <a href="http://twitter.com/',  twitterWdgt.response.results[j].from_user,'">', twitterWdgt.response.results[j].from_user, '</a>');
							}
							twitterWdgt.html.push('</em></div>');
							// stop if limit exceeded
					
							if(j >= twitterWdgt.tweet_limit-1) {		
								break;
							}
						}
						
					}
					twitterWdgt.setFeed();
				}
			},
			null,
			'get',
			'feed=' + this.feedHash + '&return=' + this.return_value
		);
	};

	this.setFeed = function() {
		this.output(this.html.join(''));
		this.html.length=0;
		var twitterWdgt = null;
	};
};

