var active = 0;

var imagefadertimeout = 3000;

var carrouselfader = false;

function switchhypotheekcarrousel(n)
{
  clearTimeout(hypotheekcarrouseltimer);
  if (carrouselfader) {
    carrouselfader.cancel();
  }
  for (var i = 0; i < 3; i++) {
    $('hypotheekcarrouselimage' + i).setOpacity(1);
    $('hypotheekcarrouseltext' + i).setOpacity(1);
    if (n == i) {
      $('hypotheekcarrouselimage' + i).show();
      $('hypotheekcarrouseltext' + i).show();
      $('hypotheekcarrouselmenu' + i).addClassName('active-item');
    } 
    else {
      $('hypotheekcarrouselimage' + i).hide();
      $('hypotheekcarrouseltext' + i).hide();
      $('hypotheekcarrouselmenu' + i).removeClassName('active-item');
    }
    active = n;
  }
  hypotheekcarrouseltimer = setTimeout('switchnexthypotheekcarrousel()', imagefadertimeout)
}

function fadehypotheekcarrousel(n) {
  clearTimeout(hypotheekcarrouseltimer);
  
  $('hypotheekcarrouselmenu' + active).removeClassName('active-item');
  $('hypotheekcarrouselmenu' + n).addClassName('active-item');
  if (active == n) {
    hypotheekcarrouseltimer = setTimeout('switchnexthypotheekcarrousel()', imagefadertimeout)
  }
  else {
    if (active > n) {
      $('hypotheekcarrouselimage' + n).show();
      //$('hypotheekcarrouseltext' + n).show();
      carrouselfader = new Effect.Parallel([
        new Effect.Fade('hypotheekcarrouselimage' + active, { sync: true }),
        new Effect.Fade('hypotheekcarrouseltext' + active, { sync: true }),
        new Effect.Appear('hypotheekcarrouseltext' + n, { sync: true })
      ], {
        afterFinish: function() {
          active = n;
          hypotheekcarrouseltimer = setTimeout('switchnexthypotheekcarrousel()', imagefadertimeout)
          carrouselfader = false;
        }
      });
    }
    else {
      carrouselfader = new Effect.Parallel([
        new Effect.Appear('hypotheekcarrouselimage' + n, { sync: true }),
        new Effect.Appear('hypotheekcarrouseltext' + n, { sync: true }),
        new Effect.Fade('hypotheekcarrouseltext' + active, { sync: true })
      ], {
        afterFinish: function() {
          $('hypotheekcarrouselimage' + active).hide();
          //$('hypotheekcarrouseltext' + active).hide();
          active = n;
          hypotheekcarrouseltimer = setTimeout('switchnexthypotheekcarrousel()', imagefadertimeout)
          carrouselfader = false;
        }
      });
    }
  }
}

function switchnexthypotheekcarrousel()
{
  var n = active;
  n++;
  n = n % 3;
  fadehypotheekcarrousel(n);
}

var hypotheekcarrouseltimer = setTimeout('switchnexthypotheekcarrousel()', 3000);