//
// Created by KSI ( www.xksi.pl )
//

/*

Klasa obsługi scrollBoxow.
Autor : KSI ( www.xksi.pl )
Wszelkie prawa zastrzeżone.

*/
var mousePosX = 0 ;
var mousePosY = 0 ;

function GetMousePos()
//
//  funkcja pomocnicza pobiera pozycję kursora na stronie ( używa jquery )
//
{
  $().mousemove(function(e){
      mousePosX = e.pageX ;
      mousePosY = e.pageY ;
   });
//alert ( 'nie zaimplementowane GetMousePos' ) ;   
}

function scrolling( instanceName )
{
  this.loopCount     = 0 ;
  this.contentLeft   = 0 ;
  this.rollSpeed     = 0 ;
  this.instanceName  = instanceName ;
  this.MaxSpeed      = 5 ;
  this.speedIsNegative = - 1 ;
  this.standbySpeed  = - 0 ;  
  
  this.noMoveTimeAfterEvent = 5000 ; // miliseconds precision is defined by this.loopMainSpeed
  this.noMoveCounter = 5000 ;
  
  this.elementWidth  = 0 ; // single element width ( when divContent contains same width blocks )
  this.elementsCount = 0 ; // single element width ( when divContent contains same width blocks )
  this.jumpingScroll = true ;
  this.jumpingShift  = 1 ;
  this.lastElement   = 0 ;
  this.elementsInArea = 1 ;
  
  this.useLog        = false ;
  this.loopMainBusy  = false ;  
  this.loopMainSpeed = 25 ; // miliseconds
  
  this.divContent    = null ;
  
  this.divArea       = null ;
  this.divAreaLeft   = 0 ;
  this.divAreaTop    = 0 ;
 
  this.mouseLastX    = 0 ;
  this.mouseLastY    = 0 ; 

  this.areaWidth     = 0 ;
  this.contentWidth  = 0 ;

  this.isSliding = false ;
  this.slideTo   = 0 ;
  
  
  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  // 
  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  this.init = function ( areaId, contentId )
    {
      this.addLogLine ( 'init : begin;' );

      this.divArea      = document.getElementById ( areaId ) ;
      this.divAreaLeft  = this.divArea.style.left ;
      this.divAreaTop   = this.divArea.style.top ;
      this.addLogLine ( 'init : this.divAreaLeft ' + this.divAreaLeft ) ;
      this.addLogLine ( 'init : this.divAreaTop ' + this.divAreaTop ) ;

      this.divContent   = document.getElementById ( contentId ) ;
      //this.divContent.style.position = 'relative' ;
      this.divContent.style.left = 0 ;
      this.divContent.style.top = 0 ;
      this.addLogLine ( 'init : end;' );
    }

  this.initDivsParams = function ()
    {
      this.areaWidth       = parseInt ( this.divArea.style.width ) ;
      this.contentWidth    = parseInt ( this.divContent.style.width ) ;
    }

  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  // Main onTimer loop
  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  this.loopMain = function ()
    {
      if ( ( this.areaWidth==0 ) || ( this.contentWidth==0 ) )
        {
          this.initDivsParams();
        }
      if ( this.loopMainBusy==true )
        {
          setTimeout ( this.instanceName+".loopMain();", this.loopMainSpeed ) ;
          return(0);
        }
        else
        {
          this.loopMainBusy = true ;          
          dontMove = false ;

          //
          // Checking if move is paused
          //
          if ( this.noMoveCounter > 0 )
            {
              dontMove = true ;
              this.noMoveCounter = this.noMoveCounter - this.loopMainSpeed ;
            }
           
          if ( this.isSliding == true ) 
            {
              dontMove = true ;
              if ( Math.abs( this.contentLeft - this.slideTo ) > 0.49 )
                {
                  diff = ( this.contentLeft - this.slideTo ) / 4 ;
                  if ( diff > this.elementWidth/this.elementsInArea ) diff = this.elementWidth/this.elementsInArea ;
                  if ( diff < -this.elementWidth/this.elementsInArea ) diff = -this.elementWidth/this.elementsInArea ;
                  if (( diff > -1 )&&( diff < 0 )) diff = -1 ;
                  if (( diff < 1 )&&( diff > 0 )) diff = 1 ;
                  this.contentLeft = this.contentLeft - diff ;
                  //this.addLogLine ( diff ) ;
                }
                else
                {
                  this.isSliding = false ;
                }
            }
            
          if ( ( dontMove == false ) && ( this.jumpingScroll == true ) )
            {
              this.goToElementRel ( this.jumpingShift ) ;
            }

          //
          // if can move then is moving
          //
          if ( dontMove == false )
            {
              this.contentLeft = this.contentLeft + this.rollSpeed ;
              if ( this.contentLeft >= 0 )
                {
                  this.contentLeft = 0 ;
                  this.noMoveCounter = this.noMoveTimeAfterEvent ;
                  this.rollSpeed = - this.rollSpeed ;
                }
              if ( this.contentLeft <= -(this.contentWidth - this.areaWidth) )
                {
                  this.contentLeft = -(this.contentWidth - this.areaWidth ) ;
                  this.noMoveCounter = this.noMoveTimeAfterEvent ;
                  this.rollSpeed = - this.rollSpeed ;
                  
                }
            }
            
          this.divContent.style.left = Math.round(this.contentLeft)+'px' ;
          this.loopMainBusy = false ;
          setTimeout ( this.instanceName+".loopMain();", this.loopMainSpeed ) ;
        }
      this.loopCount ++ ;
      this.showDebug();
    };
    
  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  // navigate bar behaviours and events
  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  this.incSpeed = function ( incSpeed )
    {           
      this.rollSpeed = this.rollSpeed + incSpeed ;
      if ( this.rollSpeed < -this.MaxSpeed )
        {
          this.rollSpeed = - this.MaxSpeed ;
        }
      if ( this.rollSpeed > this.MaxSpeed )
        {
          this.rollSpeed = this.MaxSpeed ;
        }

      this.addLogLine ( "incSpeed" );
    }

  this.setSpeed = function ( setS )
    {
      this.noMoveCounter = 0 ;
      this.rollSpeed = setS ;
      this.addLogLine ( "setSpeed : " + setS );
    }

    
  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  // content, area behaviours and events
  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  this.goToElement = function ( elementNo )
    {
      this.slideTo = - this.elementWidth * elementNo ; 
      this.isSliding = true ;
      this.rollSpeed = this.standbySpeed ;
      this.noMoveCounter = this.noMoveTimeAfterEvent ;
      this.addLogLine ( "goToElement : " + elementNo + ", contentLeft = " + this.contentLeft );
    }

  this.goToElementRel = function ( relElementShift )
    {
      this.lastElement = this.lastElement + relElementShift ;     
      if ( this.lastElement > this.elementsCount - this.elementsInArea )
        {
          this.lastElement = this.elementsCount - this.elementsInArea ;
          this.jumpingShift = - this.jumpingShift ;
        }
      if ( this.lastElement < 0 )
        {
          this.lastElement = 0 ;
          this.jumpingShift = - this.jumpingShift ;
        }
      this.goToElement ( this.lastElement ) ;      
    } 
    
  this.calcSpeed = function ()
    {
      GetMousePos();
      mx = mousePosX - parseInt ( this.divArea.offsetLeft ) ;
      my = mousePosY - parseInt ( this.divArea.offsetTop ) ;
      this.mouseLastX = mx ;
      this.mouseLastY = my ;
      this.initDivsParams();
      this.rollSpeed = ( ( ( this.areaWidth / 2 ) + mx ) - this.areaWidth ) / (this.areaWidth / 2) * this.MaxSpeed * this.speedIsNegative;
    }

  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  //  Debug, log and tools
  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  this.initLog = function ( logId, debugId )
    {
      if ( this.useLog == true )
        {
          this.divLog = document.getElementById ( logId ) ;
          this.divDebug = document.getElementById ( debugId ) ;
          this.useLog = true ;
          this.addLogLine ( "Log and debug windows ready. \r\n" ) ;
        }
    }
    
  this.addLogLine = function ( logLine )
    {
      if ( this.useLog==true )
        {
          this.divLog.innerHTML = this.divLog.innerHTML + "<br>" + logLine ;
        }
        else
        {
          //alert ( 'addLogLine : divLog not initialised');
        }
    }

  this.showDebug = function ()
    {
      if ( this.useLog == true )
        {
          lan = 'loopCount :' + this.loopCount + '<br>' ;
          lan += 'rollSpeed : ' + this.rollSpeed + '<br>' ;
          lan += 'contentLeft : ' + this.contentLeft + '<br>' ;
          lan += 'areaWidth: ' + this.areaWidth + '<br>' ;
          lan += 'contentWidth : ' + this.contentWidth + '<br>' ;
          lan += 'loopMainBusy : ' + this.loopMainBusy + '<br>' ;
          lan += 'noMoveCounter : ' + this.noMoveCounter + '<br>' ;
          lan += 'slideTo : ' + this.slideTo + '<br>' ;
          lan += 'isSliding : ' + this.isSliding + '<br>' ;
          lan += 'lastElement : ' + this.lastElement + '<br>' ;
          lan += 'jumpingScroll : ' + this.jumpingScroll + '<br>' ;     
          this.divDebug.innerHTML = lan ;
        }
    }

}
