var stream = stream || {};

stream.Twitter = new Class({

    initialize: function(keywords, callback, container, options) {    
        this.options = jQuery.merge(this.options, options || {});
        this.params   = '?rpp='+ this.options.count +'&q=' + encodeURIComponent(keywords);
        this.callback = callback;
        this.container = container;
    },

    update: function() {

        if(this.options.clear) {
            jQuery(this.container).empty();
        }

        jQuery.ajax({
            'url': 'http://search.twitter.com/search.json' + this.params,
            'success': this.success.bind(this),
            'dataType': 'jsonp'
        });

    },

    success: function(response) {

        if(response['results']) {

            this.params = response.refresh_url;
            response.results.reverse();


            this.callback.apply(this.callback, [response.results, this.container]);

        }

      setTimeout(this.update.bind(this), this.options.interval * 1000);
    },

    options: {'count': 20, 'clear': false, 'interval': 20},
    params: null, 
    callback: null
});

function since(timestamp) {

    var now = new Date().getTime();
    var seconds = Math.round(now/1000 - timestamp/1000);

    if(seconds < 60)    return ' hace ' + seconds + ' segundos';
    if(seconds < 3600)  return ' hace ' + Math.round(seconds / 60) + ' minutos';
    if(seconds < 86400) return ' hace ' + Math.round(seconds / 3600) + ' horas';
    
    return '';
}


