/**
 * 	
 *  Copyright(c) 2009: Mantecao / Grupo Alion C.A.
 *  url: www.mantecao.com.ve/licensing
 *  email: licensing@mantecao.com.ve   
 *  author: Alexander Cabezas / Carlos Mantilla 
 * 
 */

if (typeof(core) == "undefined") { core = {}; }

core.news = {};

/**
 * description: Función que retorna un número de tweets 
 * @requires config/config.js
 * @param {Object} account
 * @param {Object} cant
 */
core.news.twitter = function(account){
  	return {
		account: account,
		gettweets: function(cant, lang, query_selector, truncate, account_ellipsis, callback){
			var account = this.account;
			var ellipsis;
			url =  core.config.services.search_twitter + this.account;
			if (lang) 
				url = url + "&lang=" + lang;
			
			if(account_ellipsis)ellipsis = '<a href="http://twitter.com/' + account + '" target="_blank">ver m&aacute;s</a>';
			else ellipsis = '';
			
			$.getJSON(url + "&callback=?", function(data){
				var i;
				//$("ul#news","." + div_class).empty();
				$(query_selector).empty();
				$.each(data, function(i, item){
					var tag = '';
					if (i == (cant - 1)) 
						tag = $('<li id="tweet_' + i + '" class="tweet tweet-last"></li>');
					else 
						tag = $('<li id="tweet_' + i + '" class="tweet"></li>');
					tag.append(core.utils.truncate(item.text, truncate, ellipsis, true));
					$(query_selector).append(tag);
					//$("ul#news","." + div_class).append(tag);
					return (i < cant - 1);
				});
				if(callback) eval(callback);
			});
			
		},
		getfollowers_count: function(selector){
			var url =  core.config.services.get_twitteruser + this.account + ".json";
			$.getJSON(url + "?count=1&callback=?", function(data){
				$(selector).append('<p>Seguidores (' + data[0].user.followers_count + ')</p>');
			});
		}
	} 
}

/**
 * description: Función que retorna un número de topicos de getsatisfaction
 * @requires http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js, config/config.js, core.js
 */
core.news.getsatisfaction = function(){
	return {
		gettopics: function(topic, cant, html_iteration,container, truncate_subject, truncate_content ){
			var length_subject = truncate_subject || 0;
			var length_content = truncate_content || 0;
			var subject, content;
			
			if (topic != ''){
				url =  core.config.services.getsatisfaction + '?style=' + topic + '&sort=recently_created&limit=' + cant;	
			}else url =  core.config.services.getsatisfaction + '?style=question,idea,praise&sort=recently_created&limit=' + cant;	
			
			
			$.getJSON(url + "&callback=?", function(data){
				var html = '', html_i;
				$(container).empty();
				$.each(data.data, function(i, item){
					
					subject = core.utils.strip_html(item.subject);
					content = core.utils.strip_html(item.content);
					
					if(length_subject > 0) subject = core.utils.truncate(subject,length_subject,'',false);
					else subject = subject;
					
					if(length_content > 0) content = core.utils.truncate(content,length_content,'',false);
					else content = content;
					
					html_i = html_iteration
					html_i = html_i.replace('[subject]',subject);
					html_i = html_i.replace('[content]',content);
					html_i = html_i.replace('[url]',item.at_sfn);
					html_i = html_i.replace('[style]',item.style);
					html = html + html_i;
				});
				$(container).append(html);
			});
			
		}
	} 
}



