/**
 * 	
 *  Copyright(c) 2011: Consultores Prime C.A.
 *  url: www.consultoresprime.com
 *  author: Nilda Ching
 * 
 */

var url_tweets = "http://api.twitter.com/1/statuses/user_timeline.json?screen_name=";
var url_counter = "http://members.sitegadgets.com/scripts/tcounter.cgi?username=dominuspolux";
var t1, t2, t3, t4, t5, t6, t7;
 core = {};
 core.twitter = {};

$(document).ready(function() {
	// obtener el counter
	//getCounter();
	loadTimeline();
	setInterval("loadTimeline()",600000);
	/*t1 = core.twitter('redalpa');
	t2 = core.twitter('lecherialatina');
	t3 = core.twitter('PorcinoTips');
	t4 = core.twitter('GanaderiaTesoro');
	t5 = core.twitter('GanaderiaMex');
	t6 = core.twitter('infocampoweb');
	t7 = core.twitter('criaderors');
	refreshTweets();
	setInterval("refreshTweets()",600000);
	*/
});

function loadTimeline(){
	var div_tweet, duv_wrap, link, i,f, l, content;
	
	$('#example-preview-widget .twtr-tweets').empty();
	$('#example-preview-widget .twtr-tweets').append('<div class="loading"></div>');
	$.getJSON('get_tweets.php',function(data){
		$('#example-preview-widget .twtr-tweets').empty();
		$.each(data,function(index,obj){
			i = obj.text.indexOf("http://");
			f = obj.text.indexOf(" ",i);
			if (f == -1) f = obj.text.length;
			l = f - i;
			link = obj.text.substr(i,l);
			content = obj.text.replace(link,'<a href="' + link + '" target="_blank">' + link + '</a>');
			div_tweet = $('<div class="twtr-tweet"></div>');
			div_wrap = $('<div class="twtr-tweet-wrap"></div>');
			div_wrap.append('<div class="twtr-avatar"><div class="twtr-img"><a href="http://twitter.com/intent/user?screen_name=' + obj.user.screen_name + '" target="_blank"><img src="' + obj.user.profile_image_url + '" alt="Perfil ' + obj.user.screen_name + '"></a></div></div>');
			div_wrap.append('<div class="twtr-tweet-text"><p><a class="twtr-user" href="http://twitter.com/intent/user?screen_name=' + obj.user.screen_name + '" target="_blank">' + obj.user.screen_name + '</a>&nbsp;' + content + '<em><a href="http://twitter.com/intent/tweet?in_reply_to=' + obj.id + '" class="twtr-reply" target="_blank">reply</a> . <a href="http://twitter.com/intent/retweet?tweet_id=' + obj.id + '" class="twtr-rt" target="_blank">retweet</a></em></p></div>');
			div_tweet.append(div_wrap);
			$('#example-preview-widget .twtr-tweets').append(div_tweet);
		});
	});
}

/**
 * description: Función que retorna un número de tweets 
 * @requires lib/jquery/jquery-1.6.1.min.js
 * @param {Object} account
 */
 
core.twitter = function(account){
  	return {
		account: account,
		gettweets: function(cant, lang, query_selector, truncate, account_ellipsis, callback, put_last, separator_bar){
			var account = this.account;
			var ellipsis;
			put_last = put_last || true;
			separator_bar = separator_bar || '';
			url =  url_tweets + 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 + ' .loading').remove();
				$.each(data, function(i, item){
					var tag = '';
					if (i == 0 && separator_bar != ''){
						$(query_selector).append(separator_bar);
					}
					if (i == (cant - 1) && put_last)
						tag = $('<li id="tweet_' + i + '" class="tweet tweet-last"></li>');
					else 
						tag = $('<li id="tweet_' + i + '" class="tweet"></li>');
					tag.append(core.twitter.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>');
			});
		}
		
	}
}

core.twitter.truncate =  function(text, length, ellipsis, ellipsis_always){
	// Set length and ellipsis to defaults if not defined
	if (typeof length == 'undefined') var length = 100;
	if (typeof ellipsis == 'undefined') var ellipsis = '';

	// if the text is already lower than the cutoff
	if (text.length > length) {
		// Otherwise, check if the last character is a space.
		// If not, keep counting down from the last character
		// until we find a character that is a space
		if (text.indexOf(' ') != -1 && text.indexOf(' ') < length) {
			for (var i = length - 1; text.charAt(i) != ' '; i--) {
				length--;
			}
		}
		ellipsis = '... ' + ellipsis;
	}else{
		if(!ellipsis_always) ellipsis = '';
		else ellipsis = ' ' + ellipsis;
	}
	// The for() loop ends when it finds a space, and the length var
	// has been updated so it doesn't cut in the middle of a word.
	return text.substr(0, length) + ellipsis;
}

function getCounter(){
	var msj = "nuestra página ha sido visitada {counter} veces desde Abril de 2003";
	$.ajax({
	  url: url_counter + "&callback=?",
	  //context: document.body,
	  success: function(data){
		$('p#counter').empty();
		msj = msj.replace('{counter}',data);
		$('p#counter').html(msj);
	  }
	});
	
}

function refreshTweets(){
	var ul_tweets;
	$('.tweet ul#tweets').remove();
	ul_tweets = $('<ul id="tweets"><div class="loading"></div></ul>');
	$('.tweet .bottom').append(ul_tweets);
	
	$('.tweet_others ul#tweets_others').remove();
	ul_tweets = $('<ul id="tweets_others"><div class="loading"></div></ul>');
	$('.tweet_others .bottom_others').append(ul_tweets);
	
	t1.gettweets(15,null,'.tweet ul#tweets',70, true, 'scrollPane("ul#tweets")' );
	
	t2.gettweets(5,null,'.tweet_others ul#tweets_others',70, true,'tweetsPorcinoTips()', false, '<li><div class="separator">@lecherialatina</div></li>');
	
}

function tweetsPorcinoTips(){
	t3.gettweets(5,null,'.tweet_others ul#tweets_others',70, true,'tweetsGanaderiaTesoro()', false, '<li><div class="separator">@PorcinoTips</div></li>');
}

function tweetsGanaderiaTesoro(){
	t4.gettweets(5,null,'.tweet_others ul#tweets_others',70, true,'tweetsGanaderiaMex()', false, '<li><div class="separator">@GanaderiaTesoro</div></li>');
}

function tweetsGanaderiaMex(){
	t5.gettweets(5,null,'.tweet_others ul#tweets_others',70, true,'tweetsinfocampoweb()', false, '<li><div class="separator">@GanaderiaMex</div></li>');
}

function tweetsinfocampoweb(){
	t6.gettweets(5,null,'.tweet_others ul#tweets_others',70, true,'tweetscriaderors()', false, '<li><div class="separator">@infocampoweb</div></li>');
}
function tweetscriaderors(){
	t7.gettweets(5,null,'.tweet_others ul#tweets_others',70, true, 'scrollPane("ul#tweets_others")', true, '<li><div class="separator">@criaderors</div></li>' );
}

function scrollPane(selector){
	$(selector).jScrollPane(
		{
			showArrows: true,
			horizontalGutter: 10
		}
	);

}




