  Fader = function(options)
  {
    this.init(options);
    this.build();
  };
  
  Fader.prototype = {
    opacity:    100,  // 不透明度
    speed:      5,    // フェードの速度
    fade_timer: 100,  // フェードの実行間隔
    meta_timer: 5000, // 画像切り替えの実行間隔
    
    img_outside: null,        // 表示される画像
    img_inside:  null,        // 待機している画像
    img_pathes:  new Array(), // 使用する画像のパス
    img_count:   1,           // 表示される画像の配列内番号
    img_index:   0,           // 使用する画像の個数
    
    // 設定
    init: function(options)
    {
      for (var i in options) {
        this[i] = options[i];
      }
      this.img_outside = $('#img_outside').get(0);
      this.img_inside  = $('#img_inside').get(0);
      this.img_count   = this.img_pathes.length;
      $(this.img_outside).attr('src', this.img_pathes[0]);
      $(this.img_inside).attr('src', this.img_pathes[1]);
    },
    
    // 再設定
    reinit: function()
    {
      this.opacity = 100;
      this.img_index += 1;
      if (this.img_index >= this.img_count) {
        this.img_index = 0;
      }
      
      var img_index = this.img_index;
      var img_next  = this.img_index+1;
      if (img_next >= this.img_count) {
        img_next = 0;
      }
      
      $(this.img_outside).attr('src', this.img_pathes[img_index]);
      $(this.img_outside).css('opacity', 1);
      $(this.img_inside).attr('src', this.img_pathes[img_next]);
    },
    
    build: function()
    {
      var ref = this;
      setInterval(function(){
        ref.FadeOut();
      }, this.meta_timer);
    },
    
    // フェードアウト
    FadeOut: function()
    {
      var ref = this;
      var fade = setInterval(function(){
        $(ref.img_outside).css('opacity', ref.opacity/100);
        ref.opacity -= ref.speed;
        if(ref.opacity < 0) {
          clearInterval(fade);
          ref.reinit();
        }
      }, this.fade_timer);
    }
  }
  
  $(document).ready(function(){
    var img_pathes = new Array(
      "images/Fader/img1.jpg",
      "images/Fader/img2.jpg",
      "images/Fader/img3.jpg",
      "images/Fader/img4.jpg"
    );
    
    Fader = new Fader({
      img_pathes: img_pathes
    });
  });

