function WidtgetsNotizie(params){
  this.widgets_container = null;
  this.elementi_caricati = new Array();
  this.num_elementi_caricati = 0;
  this.max_widgets_num = 2;
  this.current_widget_num = 0;
  this.tipo = '';
  this.http_address = '';
  this.widgets_predefiniti = new Array();
  this.spazioazzurro_ticker = null;
  this.twitter_helper = null;
  if(params){
    if(params.spazioazzurro_ticker && (typeof params.spazioazzurro_ticker == 'object') && (params.spazioazzurro_ticker instanceof TickerHelper == true)){
      this.spazioazzurro_ticker = params.spazioazzurro_ticker;
    }
    if(params.twitter_helper && (typeof params.twitter_helper == 'object') && (params.twitter_helper instanceof TwitterHelper == true)){
      this.twitter_helper = params.twitter_helper;
    }
  }
};

WidtgetsNotizie.prototype.CaricaWidgets = function(widgets_container, max_widgets_num, http_address, widgets_predefiniti){
  this.widgets_container = $('#' + widgets_container);
  this.current_widget_num = 0;
  this.max_widgets_num = max_widgets_num;
  
  if(widgets_predefiniti instanceof Array){
    this.widgets_predefiniti = widgets_predefiniti;
    this.current_widget_num = -1;
    this.max_widgets_num = this.widgets_predefiniti.length;
  }
  
  if(http_address)
  {
    this.http_address = http_address;
  }
  if($('#tipo').length>0){
    this.tipo = $('#tipo').val();
  }
  this.widgets_container.html('');
  this.CaricaProssimoWidget();
}

WidtgetsNotizie.prototype.CaricaProssimoWidget = function(){
  this.current_widget_num++;
  if(this.num_elementi_caricati < this.max_widgets_num && (this.widgets_predefiniti.length > 0 || this.current_widget_num <= 4)){
    var nome_file = '';
    if(this.widgets_predefiniti.length > 0 ){
      nome_file = this.http_address + '/data/widget-' + this.widgets_predefiniti[this.current_widget_num] + '.json';
    } else {
      nome_file = this.http_address + '/data/widget' + this.current_widget_num + '.json';
    }
    
    $.ajax({
      cache: false,
      dataType: 'jsonp',
      jsonp: false,
      jsonpCallback: "jsonpCB_Widget",
      url: nome_file,
      beforeSend : function(xhr) {
        xhr.overrideMimeType('text/html; charset=ISO-8859-1');
      },
      success: $.proxy(function(data) {
        if(data != ''){
          data = $(data.html);
          if(data.length != 0){
            var tipo_widget = data.attr('id');
            if(tipo_widget != this.tipo){
              if(!(tipo_widget in this.elementi_caricati)){
                $('a[rel="external"]', data).click(function(){
                  window.open(this.href);
                  return false;
                });
                this.widgets_container.append(data);
                this.elementi_caricati[String(tipo_widget)] = 1;
                this.num_elementi_caricati++;
                if(tipo_widget == "spazioazzurro" && this.spazioazzurro_ticker != null){
                  this.spazioazzurro_ticker.StartTicker()
                } else if(tipo_widget == "facebook"){
                  FBHelper.LoadFB('facebook-iframe', 226, 261, this.http_address + '/css/facebook-mini.css?8', '', 1, 0);
                } else if(tipo_widget == "twitter" && this.twitter_helper != null){
                  this.twitter_helper.StartFeed();
                }
              }
            }
          }
        }
        this.CaricaProssimoWidget();
      }, this)
    });
  }
}



