Fader = function(options)
{
  this.init(options);
  this.build();
};

Fader.prototype = {
  node_data: new Array(),
  opacity:   100,
  speed:     5,
  img_count: 2,
  img_index: -1,
  img_next:  1,
  fade:      null,
  
  // 設定
  init: function(options)
  {
    var nodes = jQuery(options.node_box).find('.slide');
    for (var i = 0; i < nodes.length; i ++) {
      this.node_data[i] = {
        node: nodes[i],
        cond: options.conds[i]
      };
      jQuery(nodes[i]).hide();
    }
    jQuery(nodes[0]).show();
    this.img_count = nodes.length;
  },
  
  // 再設定
  reinit: function()
  {
    this.opacity = 100;
    this.img_index += 1;
    if (this.img_index >= this.img_count) {
      this.img_index = 0;
    }
    
    this.img_next = this.img_index+1;
    if (this.img_next >= this.img_count) {
      this.img_next = 0;
    }
    
    this.fadeProccess();
  },
  
  build: function()
  {
    this.reinit();
  },
  
  // フェードアウト
  fadeProccess: function()
  {
    var ref = this;
    var data_index = this.node_data[this.img_index];
    var data_next  = this.node_data[this.img_next];
    this.fade = setInterval(function(){
      ref.opacity -= ref.speed;
      if(ref.opacity < 0) {
        clearInterval(ref.fade);
        if (ref.img_next == 0) {
          jQuery(data_next.node).show();
          jQuery(data_index.node).fadeOut(data_index.cond.fade_speed, function(){
            ref.reinit();
          });
        } else {
          jQuery(data_next.node).fadeIn(data_next.cond.fade_speed, function(){
            jQuery(data_index.node).hide();
            ref.reinit();
          });
        }
      }
    }, data_index.cond.time);
  }
}

