MitterWidget = {
  BASE_URL: 'http://mitter.jp',
  CONTENT_URI: '/widget/user',
  VARIABLE_PREFIX: 'mitter_',
  PARAMS: ['username', 'type', 'max_entries', 'width', 'padding', 'separate', 'encoding', 'border_color', 'background_color', 'text_color', 'link_color', 'footer_text_color', 'footer_link_color'],

  init: function() {
    this.requestContent(this.url_for(this.CONTENT_URI) + this.prepareParams());
  },

  requestContent: function(content_url) {
    script = "<script type='text/javascript' src='" + content_url + "'></script>";
    document.write(script);
  },
  
  prepareParams: function() {
    params = ''
    for (var i=0; i<this.PARAMS.length; i++) {
      if (value = this.defined(this.VARIABLE_PREFIX + this.PARAMS[i])) {
      params += '&' + this.PARAMS[i] + '=' + escape(value);
      }
    }

        // load from query string
    params += '&' + 'v=2';
    
    if (params.length > 0) {
      params = '?' + params.substr(1);
    }
    return params;
  },

  url_for: function(uri) {
    return this.BASE_URL + uri;
  },

  defined: function(name) {
    try {
      return eval(name);
    } catch (e) {};
  }
}

MitterWidget.init();
