/**
 * th.communities
 * 
 * interface to user communities like facebook, youtube, and twitter
 * 
 * TODO: Realize OO-Interface Structure or Inheritance
 * 
 * 
 * 
 */
 
  th.communities.twitter = {

	_nickname				: null,
	_password				: null,
	_data					: null,

	init: function(nickname, password) {
		
		if (typeof nickname == 'undefined' ||	nickname == '') return false;
		
		this._nickname = nickname;
		
		this.setPassword(password);
		
		return this;
		
	},
	
	setPassword: function(password) {

		if (typeof password != 'undefined' &&	password != '') this._password = password;
		
	},
	
	callAjax: function(options) {
		
		if (typeof options == 'undefined') return;
		if (typeof this._nickname == 'undefined' || typeof this._password == 'undefined') return; // if noresult target->shownoresulttarget

		if (typeof options.result_target != 'undefined') $(options.result_target).hide();
		if (typeof options.no_result_target != 'undefined') $(options.no_result_target).hide();

		switch (options.route) {
			case 'get':
				var tmp = '?nickname=' + this._nickname
				break;
			case 'add':
				var tmp = '?nickname=' + this._nickname + '&password=' + this._password;
				break;
			default:
				return false;
		}
		
		for(param in options.params) {
			tmp += '&' + param.toString() + '=' + options.params[param].toString();
		}
		
		this_tmp = this;
			
		var ajax_options = {
			
			type: 'POST',
		    url: '/twitter/' + options.route + encodeURI(tmp),
			success: function(response) {
				
				try {
					th.log(response);
					this_tmp._data = JSON.parse(response);
				}
				catch (e) {
					th.log(e.toString());
					this_tmp._data = '{ "success" : "false" }';
				}
				
				if (this_tmp._data.success == 'true') {
					th.log('success');
					if (typeof options.result_target != 'undefined') this_tmp.showResult(options.route, options.result_target);
					th.log('after show');
					if (typeof options.success_callback == 'function') options.success_callback();
					th.log('after callback'); 
					
				} else {
					th.log('failure');
					if (typeof options.no_result_target != 'undefined') this_tmp.showNoResult(options.route, options.no_result_target);
					th.log('after show');
					if (typeof options.error_callback == 'function') options.error_callback(); 
					th.log('after callback'); 
				}
				
			}
		};
		
		$.ajax(ajax_options);
		
	},

	add: function(text, result_target, no_result_target, success_callback, error_callback) {

		var options = { 
			
			route				: 'add',
			result_target		: result_target,
			no_result_target	: no_result_target,
			success_callback	: success_callback,
			error_callback		: error_callback,
			params	: 
				{
					text : text 
				}

		};;
		
		this.callAjax(options);
		
	},

	get: function(result_target, no_result_target, count, success_callback, error_callback) {
		
		var options = { 
			
			route				: 'get',
			result_target		: result_target,
			no_result_target	: no_result_target,
			success_callback	: success_callback,
			error_callback		: error_callback,
			params				:
				{
					count : count
				}

		};
		
		this.callAjax(options);

	},
	
	showResult: function(route, target) {
		
		th.log('Show Result ' + route);
		
		if ($(target).length < 1) return;
		
		switch (route) {
			case 'get':
				if(typeof this._data == 'undefined') return; 
				
				var html = '<h2>' + this._data.user.realname +'\'s Twitter Updates:</h2>';
				
				html += '<ul>'
				
				for (var tweet in this._data.tweets) {
					html += '<li>';
					html += '<h3>' + this._data.tweets[tweet].text + '</h3>';
					html += '<span>Created: ' + this._data.tweets[tweet].datecreated + '</span><br />';
					html += '<span>From: ' + this._data.tweets[tweet].source + '</span><br />';
					if (this._data.tweets[tweet].in_reply_to !='') 
						html += '<span>In Reply to: ' + this._data.tweets[tweet].in_reply_to + '</span>';
					html += '</li>';
				}
				
				html += '</ul>';
				
				$(target).html(html);
				
				break;			
			case 'add':
				break;
			
		}
		
		$(target).show();

	},
	
	showNoResult: function(route, target) {

		th.log('Show NoResult ' + route);
		
		if ($(target).length < 1) return;

		switch (route) {
			case 'get':
				if(typeof this._data == 'undefined') return; 
		
				break;
			case 'add':
				break;
		}

		$(target).show();
	
	}
	
 };