var Twitterminator = new Class({
  Implements:[Events,Options,Chain,Log],
  Binds:['storeResponse','storeTweet','failure','success'],
  options: {
    reqOpts: {
      url: false, /*'http://twitter.com/statuses/user_timeline/mootooled.json'*/
      data: {},
      timeout: 0,
      retries: 0,
      callbackKey: false
    },
    onUpdate: $empty,
    tweetHash: false,
    requestMethod: 'JSONP',
    refresh: false
  },
  initialize:function(el, options) {
    this.el = $(el) || $$('div[name='+el+']').getFirst();
    this.setOptions(options);
    this.requestMethod = this.options.requestMethod;
    this.reqOpts = this.options.reqOpts;
    this.tweetHash = this.options.tweetHash || new Hash;
    this.tweets = this.options.tweets || new Request.JSONP;
    switch (this.requestMethod) {
      case 'JSONP':
      if(this.reqOpts.callbackKey) this.callbackKey = this.reqOpts.callbackKey;
      this.req(this.reqOpts,'a');
      break;
      case 'IFRAME':
      this.failure({reason:'IFRAME not yet implemented'});
      break;
      case 'LOCAL':
      this.storeResponse(window['tweets']);
      break;
      case false:default:
      this.failure({reason:'Invalid requestmethod'});
      break;
    }
    return this;
  },
  req: function(opts,type) {
    this.tweets.setOptions({
      url: this.reqOpts.url,
      callbackKey: this.callbackKey,
      data: this.reqOpts.data,
      timeout: this.reqOpts.timeout,
      retries: this.reqOpts.retries,
      onComplete: this.storeResponse
    }).send(); /*.send(); */
  },
  storeResponse:function(response) { 
    if(response.length > 0) {
    response.each(this.storeTweet);
    this.success({reason:'Success', r:response, t:this.tweetHash})
    } else {
    this.failure({reason:'Unexpected', r:response});
    }
    response = false;
  },
  storeTweet:function(k,v) {
   return this.tweetHash.set(v,k);
  },
  failure:function(gimme) {
    this.fireEvent('failure', gimme.reason);
    return false;
  },
  parseLinks:function(d) {
   var r = /((http\:\/\/|https\:\/\/|ftp\:\/\/)|(www\.))+(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/gi;
     d = d.replace(r, function(v){
        v = v.toLowerCase();
        var m = v.match(/^([a-z]+:\/\/)/);
        var h,u;
        if(m) { h = v.replace(m[1],''); u = v; }
        else  { h = v; url = 'http://' + h; }
        return '<a href="' + u + '"' + '>' + h + '</a>';
    });
    return d;
  },
  success:function(gimme) {
    this.fireEvent('success', gimme.t);
    for(var i = gimme.t.getLength()-1; i >= 0; i--) {
      var v = gimme.t[i];
      //this.log(gimme.t[i]);
       var viesti = new Element('li', {'html':'<div class="wrap">'+this.parseLinks(v.text)+'<br/><span style="font-weight:bold; font-size:80%;"><em>on '+v.created_at.split('+')[0]+'</em></span></div>',styles:{'opacity':0,'visibility':'hidden'}}).addClass('item').inject(this.el,'top').get('morph').start({'opacity':1});
    }
   
    this.el.set('morph', {transition:Fx.Transitions.Elastic.easeInOutExpo});
    this.el.get('morph').start({'height':this.el.getScrollSize().y.toInt()});
  }
});