var JJ_NewsBanners = new Array();
function JJ_NewsBanner_Start( id ){
  if ( JJ_NewsBanners[ id ] ){ 
    JJ_NewsBanners[ id ].Start();
  }
}

function JJ_NewsBanner_Run( id ){
  if ( JJ_NewsBanners[ id ] ){
    JJ_NewsBanners[ id ].Run();
  }
}

function JJ_NewsBanner( idToStart, NewWidth ){
  this.Setup( idToStart, NewWidth );

  if ( this.Ok ){ 
    clearTimeout( this.StartTimeout );
    this.StartTimeout = 0;//setTimeout( 'JJ_NewsBanner_Start(\'' + this.id + '\')', this.UpTime );
  }

}

JJ_NewsBanner.prototype.SetDirection = function( Speed ){
  this.NextSpeed = Speed;
}

JJ_NewsBanner.prototype.Setup = function( id, ElementWidth ) {
  this.id = id;
  this.Element = document.getElementById( this.id );
  this.Childs = new Array(); 
  this.Width = ElementWidth;

  this.Pauze = false;

  this.Speed = -15;
 
  this.NextSpeed = this.Speed;

  this.InterValTime = 25;
  this.UpTime = 12000;
  this.RunnerInterval = null;
  this.StartTimeout = null;
 
  this.CurrentWidth = 0;
  
  this.Left = 0;
 
  this.Ok = false;

  if ( this.Element ){
     this.Element.style.width = ElementWidth + 'px';
/*
     this.Element.onmouseover = function(){ 
       JJ_NewsBanners[ this.id ].PauseBanner();
     };
     this.Element.onmouseout = function(){
       JJ_NewsBanners[ this.id ].ContinueBanner();
     };
*/

     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.left = this.Width + 'px';
         }
         this.Childs.push( Child );
       }
     } 

     if ( this.Childs.length ){
       this.Ok = true;
       this.CurrentVisible = 0;  
       this.NextVisible = 0;
       JJ_NewsBanners[ this.id ] = this;
     }
  }
}

JJ_NewsBanner.prototype.Start = function( ) {
  var CurrentLeft = parseInt( this.Childs[ this.CurrentVisible ].style.left );

  if ( this.Speed > 0 ){

    if ( CurrentLeft > -230 && CurrentLeft < 0 ){
      return false;
    }
  } else {
    if ( CurrentLeft > 0 && CurrentLeft < 230 ){
      return false;
    }
  }

  if ( this.Pauze ){
    return;
  }

  this.Speed = this.NextSpeed;


  clearTimeout( this.StartTimeout );
  clearInterval( this.RunnerInterval );

  this.CurrentVisible = this.NextVisible;

  if ( this.Speed > 0 ){
    this.NextVisible = this.Childs.length - 1 == this.CurrentVisible ? 0 : this.CurrentVisible + 1;
  } else {
    this.NextVisible = this.CurrentVisible == 0 ? this.Childs.length - 1 : this.CurrentVisible - 1;
  }

  if ( this.NextVisible != this.CurrentVisible  ){ 
    this.CurrentWidth = this.Childs[ this.CurrentVisible ].offsetWidth;

    if ( this.Speed > 0 ){
      this.Childs[ this.NextVisible ].style.left = this.Width + 'px';
      this.Left = this.Width;
    } else {
      this.Left = this.CurrentWidth * -1;
      this.Childs[ this.NextVisible ].style.left = this.Left + 'px';
    }
    clearInterval( this.RunnerInterval );

    this.RunnerInterval = setInterval( 'JJ_NewsBanner_Run(\'' + this.id + '\')', this.InterValTime );
  }
}

JJ_NewsBanner.prototype.Run = function( ) {
  if ( this.Speed > 0 ){
    this.Left = Math.max( this.Left - this.Speed, 0 );
    this.Childs[ this.NextVisible ].style.left = this.Left + 'px';

    if ( this.Left > 0 ){ 

      RestRuimte = this.Left;
      if ( RestRuimte < this.CurrentWidth ){
        CurrentLeft = RestRuimte - this.CurrentWidth;
        this.Childs[ this.CurrentVisible ].style.left = CurrentLeft + 'px';
      }

    } else {
      clearInterval( this.RunnerInterval );
      clearTimeout( this.StartTimeout );
      this.StartTimeout = 0;//setTimeout( 'JJ_NewsBanner_Start(\'' + this.id + '\')', this.UpTime );
      this.Childs[ this.CurrentVisible ].style.left = '-230px';
    }
  } else {
    this.Left = Math.min( this.Left + ( - 1 * this.Speed ), 0 );
    this.Childs[ this.NextVisible ].style.left = this.Left + 'px';

    if ( this.Left < 0 ){

      RestRuimte = this.Left * -1;
      if ( true ){
        CurrentLeft = RestRuimte * -1 + 230;
        this.Childs[ this.CurrentVisible ].style.left = CurrentLeft + 'px';
      }
    } else {
      clearInterval( this.RunnerInterval );
      clearTimeout( this.StartTimeout );
      this.StartTimeout = 0;//setTimeout( 'JJ_NewsBanner_Start(\'' + this.id + '\')', this.UpTime );
      this.Childs[ this.CurrentVisible ].style.left = '230px';
    }
  }
}

JJ_NewsBanner.prototype.PauseBanner = function( ){
  this.Pauze = true;
}

JJ_NewsBanner.prototype.ContinueBanner = function( ){
  this.Pauze = false;
  if ( ! this.RunnerInterval ){
    this.Start();
  }
}
