/*
 * TweetFeed - jQuery Plugin
 * version: 1.0 (04/17/2011)
 * Created by: Matt Null
 * Examples and documentation at: http://mattnull.com/
 * c) 2008-2011 Matt Null - www.mattnull.com
 * Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
 */

(function($){
        
    $.fn.tweetFeed = function(params){
        params = params || {};
        params.container = $(this);

        var tf = new TweetFeed(params);
    };
    
    var TweetFeed = function(params){
        var config = {
            container : 'body',
            tweetItemClass : 'tweet-feed-item',
            tweetTextClass : 'tweet-feed-text',
            tweetTimeClass : 'tweet-feed-time',
            months : ['Jan','Feb', 'Mar', 'Apr', 'May', 'June', 'July', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec'],
            searchTerm : 'ladygaga',
            resultsPerPage : 15
        };
        
        this.config = $.extend({},config,params);
        this.init();
    };
    
    TweetFeed.prototype.init = function(){
        this.search(this.update);
    };
    
    TweetFeed.prototype.update = function(data){
        data = data || {};
        var tweetHtml = [],
            self = this,
            container = this.config.container,
            urlExp = /(\b(http?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig,
            len = data.results.length,
            j = 0,
            last = '';

        if(container.length > 0 && data){
           
           var firstId = parseInt(container.find('.'+this.config.tweetItemClass).first().attr('tid'));

           for(var i in data.results){
               var t = new Date(data.results[i].created_at);
               var tweetTime = '';
               tweetTime += this.config.months[t.getMonth()] + ' ';
               tweetTime += t.getDate() + ' ';
               tweetTime += t.getFullYear() + ' ';

               if((firstId && data.results[i].id > firstId && data.results[i].id != firstId) || container.html() == ''){
                   var text = data.results[i].text;
                   if(!text && text != '') continue; 
                 
                   if(j == len - 1){
                       last = 'tweet-feed-last';
                   }
               
                   tweetHtml.push('<div class="'+this.config.tweetItemClass+' '+last+'" tid="'+data.results[i].id+'">');
                   tweetHtml.push('<img src="'+data.results[i].profile_image_url+'" />');
                   tweetHtml.push('<div class="'+this.config.tweetTextClass+'">'+text.replace(urlExp,'<a href="$1" target="_blank">$1</a>')+'</div>');
                   tweetHtml.push('<div class="'+this.config.tweetTimeClass+'">'+tweetTime+'</div>');
                   tweetHtml.push('</div>');
                   container.find('.'+this.config.tweetItemClass).last().remove();
                   container.find('.'+this.config.tweetItemClass).last().addClass('tweet-feed-last');
                   
                   j++
               }
           }

           $(tweetHtml.join('')).hide().prependTo(container).slideDown(300);
        }
    };
    
    TweetFeed.prototype.search = function(callback){
        var self = this;
        $.getJSON('http://search.twitter.com/search.json?q='+escape(this.config.searchTerm)+'&rpp='+this.config.resultsPerPage+'&callback=?', function(data){
            callback.call(self, data);
        });
    };

})(jQuery);



