/*
 * jQuery acfTwitter plugin 1.0
 *
 * Copyright (c) 2010-2012 André Ferraro - aferrarobr@gmail.com
 *
 * Licencidado sob MIT licence
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * jquery.acfTwitter.js 2010-05-05 18:45GMT-3 tecidosfiama
 *
 * -----------------------------------------------------------
 * Original
 *
 * http://coda.co.za/blog/2008/10/26/jquery-plugin-for-twitter
 * jquery.twitter.js
 * -----------------------------------------------------------
 * Melhorias
 *
 * - Adicionado suporte a hashtags apontando diretamente para a
 * busca do Twitter;
 *
 * - Funcionalidade de Re-Tweet
 *
 * -Adiciona thumbnail de imagens adicionadas via Twitpic
 *
 */
(function($j) {

	$j.fn.getTwitter = function(options) {
		var o = $j.extend({}, $j.fn.getTwitter.defaults, options);

		// hide container element
		$j(this).hide();

		// add heading to container element
		if (o.showHeading) {
			$j(this).append('<h2>'+o.headingText+'</h2>');
		}

		// add twitter list to container element
		$j(this).append('<ul id="twitter_update_list"><li></li></ul>');

		// hide twitter list
		$j("ul#twitter_update_list").hide();

		// add preLoader to container element
		var pl = $j('<p id="'+o.preloaderId+'">'+o.loaderText+'</p>');
		$j(this).append(pl);

		// add Twitter profile link to container element
		if (o.showProfileLink) {
			$j(this).append('<div id="twitter-footer"> <a class="tweet-profile" https://twitter.com/'+o.userName+'" target="_blank"><img src="imagens/twitter.png" alt="Siga a Rinem no Twitter" title="Siga a Rinem no Twitter" border="0"/></a></div>');
		}

		// show container element
		$j(this).show();

    $j.getScript("https://twitter.com/javascripts/blogger.js");
		$j.getScript("https://twitter.com/statuses/user_timeline/"+o.userName+".json?callback=twitterCallback2&count="+o.numTweets, function() {
			// remove preLoader from container element
			$j(pl).remove();
			$j('ul#twitter_update_list li').each(function(){

			  //adiciona rel=external para validação xHTML 1.1 de target=_blank
			  $j(this).find('a').each(function(){
			    linkHRef=$j(this).attr('href');
			    if (linkHRef.indexOf('twitter.com') > 0) $j(this).addClass('tweet-friend');
          $j(this).attr('target','_blank');
          if (linkHRef.indexOf('twitpic.com') > 0) {
            $j(this).addClass('tweet-twitpic');
            twitpicID = linkHRef.replace('https://twitpic.com/','')
            $j(this).attr('title',twitpicID);
          }
			  });

			  //suporte a hashtag
			  $j(this).find('span').each(function(){
			    buscaHash = $j(this).html();
			    buscaHash = buscaHash.replace('#',' #');
			    buscaHash = buscaHash.replace(/([^\w])\#([\w\-]+)/gm,'$j1<a href="https://search.twitter.com/search?q=%23$j2" target="_blank" class="tweet-hash">#$j2</a>');
			    $j(this).html(buscaHash);
			  });

        //substitui texto em inglês para português
				texto = $j(this).find('a:last').html();
				if(texto){
					texto = texto.replace('about','aproximadamente');
					texto = texto.replace('less than','menos de');
					texto = texto.replace(' a ',' um ');
					texto = texto.replace('minute','minuto');
					texto = texto.replace('hour','hora');
					texto = texto.replace('day','dia');
					texto = texto.replace('week','semana');
					texto = texto.replace('months','meses');
					texto = texto.replace('month','mês');
					texto = texto.replace('ago','atrás');
					$j(this).find('a:last').html(texto);
					$j(this).find('a:last').removeClass('tweet-friend');
					$j(this).find('a:last').addClass('tweet-date');
					$j(this).find('a:last').before('<br>')
				}
			});
			// show twitter list
			if (o.slideIn) {
				$j("ul#twitter_update_list").slideDown(1000);
			}
			else {
				$j("ul#twitter_update_list").show();
			}

			// give first list item a special class
			$j("ul#twitter_update_list li:first").addClass("firstTweet");

			// give last list item a special class
			$j("ul#twitter_update_list li:last").addClass("lastTweet");

      $j("ul#twitter_update_list li").hover(
        function () {
          nMsg = "RT @" + o.userName + ": " + $j(this).find('span').text();
          nMsg = nMsg.replace('#','');
          nURL = "https://twitter.com/home?status=" + nMsg;
          $j(this).append($j("<div id='retweet-panel'><a href=\"" + encodeURI(nURL) + "\" class='tweet-retweet' target='_blank'>retweet</a></div>"));
          $j(this).addClass("selTweet");
        },
        function () {
          $j(this).find("div:last").remove();
          $j(this).removeClass("selTweet");
        }
      );

        $j('.tweet-twitpic').mouseover(function() {
          $j(this).after('<div id=' + $j(this).attr('title') + ' class="tweet-twitpic-box"><img class="tweet-twitpic-img" src=https://twitpic.com/show/thumb/' + $j(this).attr('title') + '></div>');
        }).mouseout(function(){
          $j('#'+$j(this).attr('title')).remove();
        });

		});
	};

	// plugin defaults
	$j.fn.getTwitter.defaults = {
		userName: null,
		numTweets: 5,
		preloaderId: "preloader",
		loaderText: "Loading tweets...",
		slideIn: false,
		showHeading: true,
		headingText: "Latest Tweets",
		showProfileLink: true
	};
})(jQuery);


