﻿(function($) {

    $.fn.getTwitter = function(options) {
        var o = $.extend({}, $.fn.getTwitter.defaults, options);

        // hide container element
        $(this).hide();

        // add heading to container element
        if (o.showHeading) {
            $(this).append('<h2>' + o.headingText + '</h2>');
        }

        // add twitter list to container element
        $(this).append('<ul id="twitter_update_list"><li></li></ul>');

        // hide twitter list
        $("ul#twitter_update_list").hide();

        // add preLoader to container element
        var pl = $('<p id="' + o.preloaderId + '">' + o.loaderText + '</p>');
        $(this).append(pl);

        // add Twitter profile link to container element
        if (o.showProfileLink) {
            $(this).append('<a id="profileLink" href="http://twitter.com/' + o.userName + '">http://twitter.com/' + o.userName + '</a>');
        }

        // show container element
        $(this).show();

        var parseHtml = function() {
            // remove preLoader from container element
            $(pl).remove();
            $('ul#twitter_update_list li').each(function () {
                $('a', $(this)).attr('target', '_blank');
                texto = $(this).find('a:last').html();
                texto = texto.replace('about', 'aproximadamente');
                texto = texto.replace('less than', 'menos de');
                texto = texto.replace(' an ', ' um(a) ');
                texto = texto.replace(' a ', ' um(a) ');
                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');
                $(this).find('a:last').html(texto);
            });
            // show twitter list
            if (o.slideIn) {
                $("ul#twitter_update_list").slideDown(1000);
            }
            else {
                $("ul#twitter_update_list").show();
            }

            // give first list item a special class
            $("ul#twitter_update_list li:first").addClass("firstTweet");

            // give last list item a special class
            $("ul#twitter_update_list li:last").addClass("lastTweet");
        }
        $.getScript("http://twitter.com/javascripts/blogger.js");

        if (typeof json_twitter == 'object') {
			setTimeout(function(){
				if(typeof twitterCallback2 == 'function'){
					twitterCallback2(json_twitter);
					parseHtml();
				}
			}, 1000);
        }
        else {
            $.getScript("http://twitter.com/statuses/user_timeline/" + o.userName + ".json?callback=twitterCallback2&count=" + o.numTweets, function() {
                parseHtml();
            });
        }
    };

    // plugin defaults
    $.fn.getTwitter.defaults = {
        userName: "",
        numTweets: 5,
        preloaderId: "preloader",
        loaderText: "Carregando tweets...",
        slideIn: true,
        showHeading: false,
        headingText: "Últimos Tweets",
        showProfileLink: false
    };
})(jQuery);

