JJBanners = new Array();

function JJStartBanner( id ){
  if ( id ){
    JJBanners[ id ].Start();
  }
}

function JJBannerStep( id ){
  if ( JJBanners[ id ] ){
    JJBanners[ id ].NextStep();
  }
}

function JJBanner ( id, timeout ){
  this.ID = id;

  this.Element = document.getElementById( "JJBannerHolder" + id );

  if ( this.Element ){
 
    this.OkToRun = false;

    this.Childs = new Array();
  
    this.InitBanner();

    if ( this.OkToRun ){
      if ( timeout > 0 ){ 
        this.StartTimeout = setTimeout( "JJStartBanner( " + id + " )", timeout );
      } else { 
        JJStartBanner( id );
      }
    }  
  }
}

JJBanner.prototype.InitBanner = function ( ){
  this.MouseOn = false;
  this.ItemsVisible = 1;
  this.Speed = 5;
  this.IntervalTime = 100;
  this.UpTime = 2000;
  this.StartTimeout = null;
  this.RunnerInterval = null;
  JJBanners[ this.ID ] = this;
  this.Element.Parent = this;

  // find the childs
  Childs = this.Element.childNodes; 
  teller = 0;
  for( var i in Childs ){ 
    Child = Childs[ i ];
    if ( Child.className == 'BannerItem' ){ 
      this.Childs[ teller ] = Child;
      teller++;
    }
  }  
 
  if ( this.Childs.length > 0 ){

    while ( this.Childs.length < 6 * this.ItemsVisible ){ 
      var t = this.Childs.length;
      for( i=0;i<t;i++ ){ 
        this.Childs[ teller ] = this.Childs[ i ].cloneNode( true );
        this.Element.appendChild( this.Childs[ teller ] );
        teller ++;
      }
    }
 
    this.CurrentActive = new Array();
   
    this.BoxHeight = this.Element.offsetHeight;
    this.ItemHeight = this.Childs[ 0 ].offsetHeight;
 
    this.UsedIndexes = new Array();

    for( i = this.Childs.length - 1; i >= this.Childs.length - this.ItemsVisible; i -- ){
      this.SetItemTop( i, -1 * i * this.ItemHeight );
      this.UsedIndexes.push( i );
    }

    for( i = 0; i < this.ItemsVisible * 2; i ++ ){
      this.SetItemTop( i, i * this.ItemHeight );
      this.UsedIndexes.push( i );
    }
 
    this.UnUsedIndexes = new Array();

    for( i = this.ItemsVisible * 2; i < this.Childs.length - this.ItemsVisible; i ++ ){
      this.SetItemTop( i, this.BoxHeight );
      this.UnUsedIndexes.push( i );
    }

    this.OkToRun = true;

    this.Element.onmouseover = function (){
      this.Parent.MouseOver();
    }

    this.Element.onmouseout = function (){
      this.Parent.MouseOut();
    }


  }
}

JJBanner.prototype.MouseOver = function(){
  this.MouseOn = true;
}

JJBanner.prototype.MouseOut = function(){
  this.MouseOn = false;
  if ( ! this.RunnerInterval ){ 
//    this.StartTimeout = setTimeout( "JJStartBanner( " + this.ID + " )", this.UpTime );
    JJStartBanner( this.ID );
  }
}

JJBanner.prototype.SetItemTop = function ( index, Top ){
  if ( this.Childs[ index ] ){
    this.Childs[ index ].style.top = Top + 'px';
  }
}

JJBanner.prototype.UpdateItemTop = function ( index, Top ){
  if ( this.Childs[ index ] ){
    Top = Top + parseInt( this.Childs[ index ].style.top );
    this.Childs[ index ].style.top = Top + 'px';
  }
}


JJBanner.prototype.Start = function(){
  if ( this.StartTimeout ){
    this.StartTimeout = clearTimeout( this.StartTimeout );
  }

  if( this.MouseUp ){  
    return;
  }

  this.RunnerInterval = setInterval( "JJBannerStep( " + this.ID + " )", this.IntervalTime ); 

  this.CurrentIndex = 0;

}

JJBanner.prototype.NextStep = function(){
  if ( this.CurrentIndex < this.BoxHeight ){ 
    for( var i in this.UsedIndexes ){ 
      idx = this.UsedIndexes[ i ];
      this.UpdateItemTop( idx, -1 * this.Speed );
    }
    this.CurrentIndex += this.Speed;
  } else { 
    this.Stop();
  }
}

JJBanner.prototype.Stop = function(){
  this.RunnerInterval = clearInterval( this.RunnerInterval );

  for( i = 0; i < this.ItemsVisible; i ++ ){

    PrevID = this.UsedIndexes[ 0 ];
    for( var j=0; j < this.UsedIndexes.length - 1; j ++){
      this.UsedIndexes[ j ] = this.UsedIndexes[ j + 1 ];
    }

    this.UsedIndexes[ this.UsedIndexes.length - 1 ] = this.UnUsedIndexes[ 0 ];
    this.SetItemTop( this.UsedIndexes[ this.UsedIndexes.length - 1 ], this.BoxHeight + i * this.ItemHeight );

    for( var j=0; j < this.UnUsedIndexes.length - 1; j ++){
      this.UnUsedIndexes[ j ] = this.UnUsedIndexes[ j + 1 ];
    }
    this.UnUsedIndexes[ this.UnUsedIndexes.length -1 ] = PrevID;
  } 
  if ( ! this.MouseOn ){
    this.StartTimeout = setTimeout( "JJStartBanner( " + this.ID + " )", this.UpTime );  
  }
}

