// var digi_slideshow_conf = [ interval, duration, [
//   [ 'name1', 'link1', 'path1', [ 'Bild11', 'Bild12', 'Bild13' ] ],
//   [ 'name2', 'link2', 'path2', [ 'Bild21', 'Bild22', 'Bild23' ] ]
// ]

window.addEvent('domready', function() {

  var digi_slideshow = {
    conf: [],
    current: 0, // current gallery
    next: 1, // next gallery
    hidden: 1, // hidden box
    options: {},
    init: function(conf){
      this.conf = conf;
      this.options = {property:'opacity',duration:this.conf[1]};
      this.box = [$('box1'),$('box2')];
      this.fx = [new Fx.Tween(this.box[0],this.options),new Fx.Tween(this.box[1],this.options)];
      this.fx[0].addEvent("onComplete", function() { digi_slideshow.step(); });
      this.link = [$$('#box1 a'),$$('#box2 a')];
      this.img = [$$('#box1 img'),$$('#box2 img')];
      this.load(0,0); // init box 0 with gallery 0
      this.load(1,1); // init box 1 with gallery 1
      this.fx[0].set('visibility','visible');// show box 0
      this.fx[1].set('visibility','hidden');// hide box 1
      this.toggle.delay(this.conf[0],this);
    },
    toggle: function(){// toggle boxes visibility
      this.fx[1-this.hidden].start('1','0');//fadeout
      this.fx[this.hidden].start('0','1');//fadein
      this.debug('Fading...');
    },
    step: function(){// advance gallery
      this.fx[1-this.hidden].set('visibility','hidden');//hide
      this.fx[this.hidden].set('visibility','visible');//show
      this.hidden = ( this.hidden + 1 ) % 2;//toggle
      this.current = ((this.current+1)%this.conf[2].length);//cycle
      this.next = ((this.current+1)%this.conf[2].length);//cycle
      this.load( this.hidden, this.next );
      this.toggle.delay(this.conf[0],this);
    },
    load: function(box,gallery){// load gallery into box
      this.debug('Loaded gallery '+gallery+' ('+this.conf[2][gallery][0]+') into box '+box+'. Showing gallery '+this.current+' ('+this.conf[2][this.current][0]+') in box '+(1-this.hidden)+'.');
      for( var i = 0 ; i < 4 ; i++ )if(this.conf[2][gallery][2][i]&&this.conf[2][gallery][2][i][0]){// if image present
        this.img[box][i].src = 'typo3temp/pics/' + this.conf[2][gallery][2][i][0];// load image
        if(this.conf[2][gallery][2][i][1])
          this.img[box][i].title = 'ID-'+this.conf[2][gallery][2][i][1]+": "+this.conf[2][gallery][2][i][2];
        else
          this.img[box][i].title = '';
      }else{// if image missing
        this.img[box][i].src = 'typo3conf/ext/digi_slideshow/res/digitage.gif';// load default image
      }
      this.link[box][0].href = this.conf[2][gallery][1];
    },
    debug: function(msg){/*
      var now = new Date();
      window.status = '['+('0'+now.getHours()).substr(-2)+':'+('0'+now.getMinutes()).substr(-2)+':'+('0'+now.getSeconds()).substr(-2)+'] '+msg;
    /**/}
  };

  digi_slideshow.init(digi_slideshow_conf);
  
});
