﻿function Scroller()
{
  var m_nSpeed;
  var m_nRepeatTime;
  var m_nWidth;
  var m_nCurrentVelocity;
  var m_strElementClientID;
  var m_nLeft;
  var m_Right;
}

Scroller.prototype.Init=function(strElementClientID, nSpeed)
{
  this.m_nSpeed=nSpeed;
  this.m_nRepeatTime=10;
  this.m_nWidth=document.getElementById(strElementClientID).scrollWidth;
  this.m_nCurrentVelocity=0;
  this.m_strElementClientID=strElementClientID;
  this.m_nRight=parseInt(document.getElementById(strElementClientID).offsetWidth)-parseInt(document.getElementById(strElementClientID).parentNode.clientWidth);
  
  document.getElementById(strElementClientID).parentNode.style.position="relative";
  document.getElementById(strElementClientID).style.position="relative";
}

Scroller.prototype.BeginScroll=function(nDirection)
{
  this.m_nCurrentVelocity=nDirection*this.m_nSpeed;
  
  var refSelf=this;
  var funcProcess=function()
  {
    refSelf.Scroll();
  }

  window.setTimeout(funcProcess, this.m_nRepeatTime);
}

Scroller.prototype.EndScroll=function()
{
  this.m_nCurrentVelocity=0;
}

Scroller.prototype.Scroll=function()
{
  if(this.m_nCurrentVelocity!=0)
  {
    var nCurrentLeft;
    var strCurrentLeft=document.getElementById(this.m_strElementClientID).style.left;
    if(strCurrentLeft=="")
    {
      nCurrentLeft=0;
    }
    else
    {
      nCurrentLeft=parseInt(strCurrentLeft.substring(0, strCurrentLeft.length-2));
    }
    
    nCurrentLeft-=this.m_nCurrentVelocity;
    if(nCurrentLeft>0)
    {
      nCurrentLeft=0;
    }
    else if(nCurrentLeft<-1*this.m_nRight)
    {
      nCurrentLeft=-1*this.m_nRight;
    }
    
    document.getElementById(this.m_strElementClientID).style.left=nCurrentLeft+"px";
    
    var refSelf=this;
    var funcProcess=function()
    {
      refSelf.Scroll();
    }

    window.setTimeout(funcProcess, this.m_nRepeatTime);
  }
}
