JJ_FotoLijst_Runner = null;
JJ_FotoLijst_Timeout_Start = null;

JJ_FotoLijst_Afbeelding1 = null;
JJ_FotoLijst_Afbeelding2 = null;

JJ_FotoLijst_Current = null;

JJ_FotoLijst_Current_Pic = null;

JJ_FotoLijst_Swapper = false;
JJ_FotoLijst_NextAfbeelding = null;
JJ_FotoLijst_OtherAfbeelding = null;

function JJ_FotoFader( ParentNode ){

  this.Element = ParentNode;
  this.Settings = {
    Interval: 75,
    Uptime: 1000,
    Step: 10, 
    MaxValue: 100,
    MinValue: 10,
    FirstZIndex: 1000,
    SecondZIndex: 1500,
    Current: null
  };

  this.Runner = null; 
  this.Timeout = null;

  this.Childs = new Array();

  this.CurrentIndex = 0;
  this.NextIndex = 1;

  this.Ok = this.Setup();
}

JJ_FotoFader.prototype.Setup = function(){
  if ( this.Element == null ){
    return false;
  }

  for( var i = 0; i < this.Element.childNodes.length; i++ ){
    var Child = this.Element.childNodes[ i ];
    if ( Child.tagName == 'DIV' ) {
      if ( this.Childs.length == 0 ){
        Child.style.display = 'block';
      }

      this.Childs.push( {Element: Child, Filter: 0} );
    }
  }

  if ( this.Childs.length < 2 ){
    return false;
  }
  
  return true;
}

JJ_FotoFader.prototype.Start = function(){
  this.Timeout = clearTimeout( this.Timeout );

  var self = this;
  this.Timeout = setTimeout( function(){ self.StartSwapping() }, this.Settings.Uptime );
}

JJ_FotoFader.prototype.StartSwapping = function(){
  this.Childs[ this.CurrentIndex  ].Filter = this.Settings.MaxValue;
  this.Childs[ this.NextIndex  ].Filter = this.Settings.MinValue;
  this.SetFilter( this.Childs[ this.NextIndex  ].Element, this.Settings.MinValue );
  this.Childs[ this.CurrentIndex ].Element.style.zIndex = 1000;
  this.Childs[ this.NextIndex  ].Element.style.zIndex = 2000;
  this.Childs[ this.NextIndex ].Element.style.display = 'block';

  var self = this;
  this.Runner = setInterval( function(){ self.Swapper() }, this.Settings.Interval );
}

JJ_FotoFader.prototype.Swapper = function(){
  if ( this.Childs[ this.NextIndex  ].Filter < this.Settings.MaxValue ){
    this.Childs[ this.NextIndex  ].Filter += this.Settings.Step;
    this.SetFilter( this.Childs[ this.NextIndex  ].Element, this.Childs[ this.NextIndex  ].Filter );
  }

  if ( this.Childs[ this.CurrentIndex ].Filter > this.Settings.MinValue ){
    this.Childs[ this.CurrentIndex ].Filter -= this.Settings.Step;
    this.SetFilter(  this.Childs[ this.CurrentIndex ].Element, this.Childs[ this.CurrentIndex ].Filter );
  }

  if ( this.Childs[ this.NextIndex  ].Filter >= this.Settings.MaxValue && this.Childs[ this.CurrentIndex ].Filter <= this.Settings.MinValue ){ 
    this.EndSwapping();
  }     
}

JJ_FotoFader.prototype.EndSwapping = function(){
  this.Runner = clearInterval( this.Runner );
  this.Childs[ this.CurrentIndex ].Element.style.display = 'none';
  this.CurrentIndex = this.NextIndex;
  this.NextIndex = this.NextIndex < this.Childs.length - 1 ? this.NextIndex + 1 : 0;
  var self = this;
  this.Timeout = setTimeout( function(){ self.Start() }, this.Settings.Uptime );
}

JJ_FotoFader.prototype.SetFilter = function( Element, Value ){
  Value = Value >= 100 ? 100 : Value;
  if ( Element.filters && Element.filters.alpha )
    Element.filters.alpha.opacity = Value;
  Element.style.MozOpacity = Value / 100;
  Element.style.opacity = Value / 100;
}

function JJ_FotoLijst_Start( ParentID ){

  FotoFader = new JJ_FotoFader( document.getElementById( ParentID ) );

  if ( FotoFader.Ok ){
    FotoFader.Start();
  }
}


