function MK_Scroller()
{
  this.oTargetDivToScroll = null;
  this.iNumElements = 0;
  this.iCurrentElement = 0;
  this.iTargetElement = 0;
  this.fScrollStep = 32;
  this.fScrollSpeed = 4;
  this.fScrollAcceleration = 1;
  this.fScrollCurSpeed = 0;
  this.fScrollBreakAt = 0;
  this.iScrollDirection = 0;
  this.fTargetPos = 0;
  this.pTimeOut = null;
  this.bIsMoving = false;
  
  this.ScrollToPosition = MK_ScrollToPosition;
  this.ScrollHandler = MK_ScrollHandler;
  this.AutoScrollHandler = MK_AutoScrollHandler;
  
  this.iID = MK_GlobalScrollRegister(this);
}

function MK_Scroller(sDivToScroll, iInNumElements, fInScrollStep, iInScrollDirection, fInScrollSpeed, fInAcceleration)
{
  this.oTargetDivToScroll = document.getElementById(sDivToScroll);
  this.iNumElements = iInNumElements;
  this.iCurrentElement = 0;
  this.iTargetElement = 0;
  this.fScrollStep = fInScrollStep;
  this.fScrollSpeed = fInScrollSpeed;
  this.fScrollAcceleration = fInAcceleration;
  this.fScrollCurSpeed = 0;
  this.fScrollBreakAt = 0;
  this.iScrollDirection = iInScrollDirection;
  this.fTargetPos = 0;
  this.pTimeOut = null;
  this.bIsMoving = false;
  
  this.ScrollToPosition = MK_ScrollToPosition;
  this.ScrollHandler = MK_ScrollHandler;
  this.AutoScrollHandler = MK_AutoScrollHandler;
  
  this.iID = MK_GlobalScrollRegister(this);
  
  this.pTimeOut=setTimeout('MK_GlobalAutoScrollHandler('+this.iID+')', 5000);
}

function MK_ScrollerEx(sDivToScroll, iInNumElements, sInButtonStyleDisabled, sInButtonStyleEnabled, fInScrollStep, iInScrollDirection, fInScrollSpeed, fInAcceleration)
{
  this.oTargetDivToScroll = document.getElementById(sDivToScroll);
  this.iNumElements = iInNumElements;
  this.iCurrentElement = 0;
  this.iTargetElement = 0;
  this.fScrollStep = fInScrollStep;
  this.fScrollSpeed = fInScrollSpeed;
  this.fScrollAcceleration = fInAcceleration;
  this.fScrollCurSpeed = 0;
  this.fScrollBreakAt = 0;
  this.iScrollDirection = iInScrollDirection;
  this.fTargetPos = 0;
  this.pTimeOut = null;
  this.bIsMoving = false;
  this.sScrollerID = sDivToScroll;
  this.sButtonStyleDisabled = sInButtonStyleDisabled;
  this.sButtonStyleEnabled = sInButtonStyleEnabled;
  
  this.OldScrollToPosition = MK_ScrollToPosition;
  this.ScrollToPosition = MK_ScrollToPositionEx;
  this.ScrollHandler = MK_ScrollHandler;
  this.AutoScrollHandler = MK_AutoScrollHandler;
  
  this.iID = MK_GlobalScrollRegister(this);
  
  this.pTimeOut=setTimeout('MK_GlobalAutoScrollHandler('+this.iID+')', 5000);
}

function MK_ScrollHandler()
{
  this.bIsMoving = true;
  var fCurPos = parseFloat(this.oTargetDivToScroll.style.left);
  
  //console.log('Distance:', Math.abs(fCurPos - this.fTargetPos));
  //console.log('Speed:', this.fScrollCurSpeed);
  //console.log(this.fScrollCurSpeed);
  
  if(Math.abs(fCurPos - this.fTargetPos) <= this.fScrollBreakAt)
  {
    if(this.fScrollAcceleration < this.fScrollCurSpeed)
      this.fScrollCurSpeed -= this.fScrollAcceleration;
  }
  
  if(fCurPos <= (this.fTargetPos + 2))
  {
    fCurPos += this.fScrollCurSpeed;
    if(this.fTargetPos <= fCurPos)
    {
      fCurPos = this.fTargetPos;
      this.oTargetDivToScroll.style.left = fCurPos+'px'; 
      this.bIsMoving = false;
      this.fScrollCurSpeed = 0;
      this.iCurrentElement = this.iTargetElement;
      this.pTimeOut=setTimeout('MK_GlobalAutoScrollHandler('+this.iID+')', 5000);
      return;
    }
  }
  else if((this.fTargetPos - 2) <= fCurPos)
  {
    fCurPos -= this.fScrollCurSpeed;
    if(fCurPos <= this.fTargetPos)
    {
      fCurPos = this.fTargetPos;
      this.oTargetDivToScroll.style.left = fCurPos+'px'; 
      this.bIsMoving = false;
      this.fScrollCurSpeed = 0;
      this.iCurrentElement = this.iTargetElement;
      this.pTimeOut=setTimeout('MK_GlobalAutoScrollHandler('+this.iID+')', 5000);
      return;
    }
  }
  
  this.oTargetDivToScroll.style.left = fCurPos+'px'; 
  
  this.pTimeOut=setTimeout('MK_GlobalScrollHandler('+this.iID+')', 80);
}

function MK_ScrollToPosition(iTargetElem)
{
  clearTimeout(this.pTimeOut);
  
  if(isNaN(parseFloat(this.oTargetDivToScroll.style.left)))
    this.oTargetDivToScroll.style.left = '0px';
    
  var fTimeToBreak = this.fScrollSpeed / this.fScrollAcceleration;
    
  this.fScrollBreakAt = (this.fScrollSpeed * fTimeToBreak) / 2;
  
  this.fTargetPos = -(this.fScrollStep * iTargetElem);
  this.iTargetElement = iTargetElem;
  this.fScrollCurSpeed = this.fScrollSpeed;
  this.pTimeOut=setTimeout('MK_GlobalScrollHandler('+this.iID+')', 80);
}

function MK_ScrollToPositionEx(iTargetElem)
{
  var iX;
  var oCurElem;

  this.OldScrollToPosition(iTargetElem);
    
  for(iX = 0; iX < this.iNumElements; iX++)
  {
    oCurElem = document.getElementById(this.sScrollerID + '_btn_' + iX);
    if(iX != iTargetElem)
      oCurElem.className = this.sButtonStyleEnabled;
    else
      oCurElem.className = this.sButtonStyleDisabled;
  }
}

function MK_AutoScrollHandler()
{
  if((this.iCurrentElement + 1) < this.iNumElements)
    this.ScrollToPosition(this.iCurrentElement + 1);
  else
    this.ScrollToPosition(0);
}

var oScrolls = new Array();
var iNumScrolls = 0;

function MK_GlobalScrollRegister(oScrollToRegister)
{
  oScrolls[iNumScrolls] = oScrollToRegister;
  iNumScrolls++;
  return iNumScrolls - 1;
}

function MK_GlobalScrollHandler(iScrollNum)
{
  oScrolls[iScrollNum].ScrollHandler();
}

function MK_GlobalAutoScrollHandler(iScrollNum)
{
  oScrolls[iScrollNum].AutoScrollHandler();
}
