var tweeter,Twit = new Class({
    Implements: Options,

    options:{
        user:'44723640',
        count:2,
        interval:1800000 // 30 min
    },

    initialize:function(container,options){
        this.container = document.id(container);
        if (!this.container) return;

        this.setOptions(options);

        this.start();
        setInterval("this.update()",this.options.interval);
    },

    stop: function(){
        $clear(this.timer);
    },
    start: function(){
        this.update();
        this.timer = this.update.periodical(this.options.interval, this);
    },
    update:function(){
        new Asset.javascript('https://twitter.com/statuses/user_timeline/'+this.options.user+'.json?callback=Twitt&count=' + this.options.count);
    },
    receive:function(response){
        this.container.empty();
        response.each(function(t){
            this.container.set('html',this.linkify(t.text));
            this.container.adopt(
                new Element('small',{'class':'date','html':new Date(t.created_at).timeDiffInWords()})
            );
        }.bind(this));
    },
    linkify: function(text) {
        //courtesy of Jeremy Parrish (rrish.org)
        return text.replace(/(https?:\/\/\S+)/gi,'<a href="$1">$1</a>').replace(/(^|\s)@(\w+)/g,'$1<a href="http://twitter.com/$2">@$2</a>').replace(/(^|\s)#(\w+)/g,'$1#<a href="http://search.twitter.com/search?q=%23$2">$2</a>');
    }
});
var Twitt = function(response){
    tweeter.receive(response);
}

window.addEvent('domready',function(){
    tweeter = new Twit('tweet');
});
