BookjettyWidget = {
  BASE_URL: 'http://www.bookjetty.com',
  CONTENT_URI: '/widget/content.js',    
  PARAMS:  ['username', 'type', 'hide', 'filter_category', 'filter_tag', 'filter_isbn', 'sort_by', 'max_entries', 'border_color', 'background_color', 'text_color', 'text_align', 'link_color', 'bookjetty_text_color', 'bookjetty_link_color', 'bookjetty_link_align', 'padding', 'cover_border_color', 'cover_background_color', 'cover_padding', 'cover_size', 'cover_type', 'cover_space_width', 'cover_margin_right', 'cover_align', 'cover_image_class', 'width', 'show_text_below_cover', 'hide_bookjetty_link', 'link_to', 'amazon_associate_id', 'amazon_country', 'include_no_cover'],
  VARIABLE_PREFIX: 'bj_',
  
  init: function() {    
    if (!this.getIndex()) document.bjWidgetIndex = 0;
    document.bjWidgetIndex += 1;
    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 += '&' + 'username=jugend&filter_category';
        
    params += '&index=' + this.getIndex();
    params = '?' + params.substr(1);
    return params;
  },
 
  url_for: function(uri) {
    return this.BASE_URL + uri;
  },
  
  defined: function(name) {
    if (eval("typeof " + name + "!= 'undefined'")) {
      return eval(name);
    }
  },
  
  getIndex: function() {
    return document.bjWidgetIndex;
  }
}

BookjettyWidget.init();