(function($){
  $.fn.vounabalada = function(options) {

    var settings = $.extend({}, $.fn.vounabalada.defaults, options);

    return this.each(function () {
      var $container = $(this),
          term = settings.term || $container.attr('data-vounabalada-term'),
          method = settings.method || $container.attr('data-vounabalada-term'),
          limit = settings.limit,
          lastID = 0,
          firstRun = true,
          interval = settings.interval;
      if(term === undefined){
        alert('Em Breve');
        return;
      }

      function getTweets() {
        twitterlib[method](term, {page : 1, since : lastID}, function (result) {
          var tweets = [], i = result.length;
          lastID = result.length ? result[0].id : lastID;
          while (i--){
            tweets.push(twitterlib.render(result[i]));
          }
          if (firstRun) {
            init(tweets, limit);
            firstRun = false;
          }
          if (tweets.length == 0) {
            setTimeout(getTweets, interval);
          }else{
            theLoop(tweets);
          }
        });
      }
      
      function init(tweets, length){
        for (var i = 0; i < length; i++) {
          $container.prepend(tweets.shift());
        }
      }
      
      function theLoop(tweets) {
        var loop = setInterval(function () {
          $nextTweet = $(tweets.shift()).hide().prependTo($container);
		  
		  $.fn.myShow = function(duration) {
  		  return this.animate({height: "toggle", opacity: "toggle"}, 1000);
		
		
		}

		$nextTweet.myShow("slow");

        
		  


          $container.children(':last').slideUp(1000, function () {
            $(this).remove();
          });
  
          if (tweets.length == 0) {
            clearInterval(loop);
            getTweets();
          }
        }, interval);
      }
      getTweets();
    });
  };
  
  $.fn.vounabalada.defaults = {
    limit : 4,
    interval : 5000,
    method : 'search'
  };

})(jQuery);

$(document).ready(function() {

  $('#tweets').vounabalada();
  
});
