var JJ_ItemScrollers = new Array();

function JJ_ItemScroller_Start( id ){
  if ( JJ_ItemScrollers[ id ] ){ 
    JJ_ItemScrollers[ id ].Start();
  }
}

function JJ_ItemScroller_Run( id ){
  if ( JJ_ItemScrollers[ id ] ){
    JJ_ItemScrollers[ id ].Run();
  }
}

function JJ_ItemScroller( idToStart ){
  this.Setup( idToStart );

  if ( this.Ok ){ 
    this.Run();
  }

}

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

  this.Pauze = true;

  this.Speed = -1;

  this.InterValTime = 20;
  this.RunnerInterval = null;
 
  this.CurrentWidth = 0;
  
  this.Left = 0;

  this.MaxLeft = 0;
 
  this.Ok = false;

  if ( this.Element ){

    this.Width = this.Element.offsetWidth;

    this.Element.onmouseover = function(){ 
      JJ_ItemScrollers[ this.id ].Pause();
    };

    this.Element.onmouseout = function(){
      JJ_ItemScrollers[ this.id ].Continue();
    };

    for( var i = 0; i < this.Element.childNodes.length; i++ ){ 
      var Child = this.Element.childNodes[ i ]; 
      if ( Child.tagName == 'DIV' ) {
        Child.style.left = this.Width + 'px';
        this.Childs.push( { 
          Element: Child,
          Left: this.Width,
          Width: Child.offsetWidth
        } );
      }
    } 

    if ( this.Childs.length ){
      this.Ok = true;

      var LeftTeller = 0;
      for( var i = 0; i < this.Childs.length; i ++ ){
        this.Childs[ i ].Left = LeftTeller;
        if ( this.Childs[ i ].Left < this.Width ){
          this.Childs[ i ].Element.style.left = LeftTeller + 'px';
        }
        LeftTeller += this.Childs[ i ].Width;
      }

      JJ_ItemScrollers[ this.id ] = this;
    }
  }
}

// Call at start or after pause
JJ_ItemScroller.prototype.Run = function( ){
  this.Pause();
  this.Pauze = false;
  var self = this;
  this.RunnerInterval = setInterval( function(){ self.Step(); }, this.InterValTime );
}

JJ_ItemScroller.prototype.Pause = function( ){
  this.Pauze = true;
  this.RunnerInterval =  clearInterval( this.RunnerInterval );
}

JJ_ItemScroller.prototype.Continue = function( ){
  if ( this.Pauze ){
    this.Run();
  }
}

JJ_ItemScroller.prototype.Step = function( ) {
  if ( this.Pauze ){ 
    this.Pause();
    return;
  }

  this.MinLeftIndex = null;
  this.MaxLeftIndex = null;

  for( var i = 0; i < this.Childs.length; i ++ ){
    this.Childs[ i ].Left += this.Speed;

    if ( this.Childs[ i ].Left < this.Width ){ 
      this.Childs[ i ].Element.style.left = this.Childs[ i ].Left + 'px';
    }
 
    this.MaxLeft = Math.max( this.MaxLeft, this.Childs[ i ].Left + this.Childs[ i ].Width );
    this.MinLeft = Math.min( this.MinLeft, this.Childs[ i ].Left );
    if ( this.MinLeftIndex == null || this.Childs[ i ].Left < this.Childs[ this.MinLeftIndex ].Left ){
      this.MinLeftIndex = i;
    }
    if ( this.MaxLeftIndex == null || this.Childs[ i ].Left > this.Childs[ this.MaxLeftIndex ].Left ){
      this.MaxLeftIndex = i;
    }
  }

  for( var i = 0; i < this.Childs.length; i ++ ){
    if ( this.Speed < 0 ){
      if ( this.Childs[ this.MaxLeftIndex ].Left == this.Width ){
        this.Childs[ this.MinLeftIndex ].Left = this.Childs[ this.MaxLeftIndex ].Left + this.Childs[ this.MaxLeftIndex ].Width;
      } 
    } else {
      if ( this.Childs[ this.MinLeftIndex ].Left > 0 ){
        this.Childs[ this.MaxLeftIndex ].Left = this.Childs[ this.MinLeftIndex ].Left - this.Childs[ this.MaxLeftIndex ].Width;
      } 
    }
  }

}

JJ_ItemScroller.prototype.SetSpeed = function( Speed ){
  this.Speed = Speed;
}

