/**
* Twitter widget client-side controller class
* 
* @author Gusts 'gusC' Kaksis
* @version 1.0
*/
if (typeof Widget_Twitter == 'undefined'){
	var Widget_Twitter = {
		_config : {
			'show_img' : true,
			'img_w' : 48,
			'img_h' : 48
		},
		_parseUrls : function (text){
			var urlReg = /(^|\s)(http:\/\/|https:\/\/|www\.)([^<> \n\r]+)($|\s)/i
			return text.replace(urlReg, '<a href="$2$3">$2$3</a>');
		},
		
		SetConfig : function (jsonConfig){
			this._config = jsonConfig;
		},
		LoadTweets : function (username, count){
			var user_url = 'http://twitter.com/#!/' + username;
			$.ajax({
				url : 'http://api.twitter.com/1/statuses/user_timeline.json',
				data : {
					screen_name : username,
					count : count
				},
				success : function(data){
					$block = $('#tweets-' + username);
					$(data).each(function(i, tweet){
						var tweet_url = user_url + '/status/' + tweet['id_str'];
						$block.append('<div class="tweet-index">'
							+ '<a href="' + user_url + '" class="image" target="_blank"><img src="' + tweet['user']['profile_image_url'] + '" /></a>'
							+ '<p>' + Widget_Twitter._parseUrls(tweet['text']) + '</p>'
							+ '<p><a href="' + tweet_url + '" target="_blank">Link to tweet »</a></p>'
				      + '</div>');
					});
				},
				dataType: 'jsonp'
			});
		}
	}
}
