/**
 * class fScroll constructor
 * param   gridId     string        identificatore univoco della grid
 * param   container  string        id del div che conterrą la grid
 */
function fScroll(scrollId) {
  var _self = this
  // ID's setting
  this.scroll = scrollId; // identificatore univoco dello scroll
  this.scrollObj = document.getElementById(scrollId); // oggetto scroll
  // time-out handler
  this.timeOuts = new Object()
  this.timeOuts.tscrollTO = new Object() // time-out handler for scroll
  this.timeOuts.tscrollTO.handler = null // time-out handler
  this.timeOuts.tscrollTO.ms = 20 // time-out milliseconds
  
  this.scrollDir = 'UP' // can be 'DOWN'
  this.scrollStep = 1;
  
  this.scrollingOn = true
    
  if (this.scrollObj) {
    if (this.scrollObj.runtimeStyle) {
      this.scrollObj.height = parseInt(this.scrollObj.clientHeight)
    } else {
      this.scrollObj.height = parseInt(window.getComputedStyle(this.scrollObj, null).getPropertyValue('height'))
    }
    
    this.scrollObj.onmouseover = function() {_self.stopScroll()}
    this.scrollObj.onmouseout = function() {_self.startScroll()}
  }
}

/**
 * 
 */
fScroll.prototype.setStep = function(st) {
  this.scrollStep = st;
}
fScroll.prototype.setTo = function(to) {
  this.timeOuts.tscrollTO.ms = to
}

fScroll.prototype.scrollUpDwn = function() {
  var _self = this
  var toutms = this.timeOuts.tscrollTO.ms
  if (this.scrollDir == 'UP') {
    this.scrollObj.scrollTop -= this.scrollStep
    if (this.scrollObj.scrollTop <= 0) {
      this.scrollObj.scrollTop = 0
      this.scrollDir = 'DOWN'
      toutms += 1000
    }
  } else {
    if (this.scrollDir == 'DOWN') {
      this.scrollObj.scrollTop += this.scrollStep
      if (this.scrollObj.scrollTop >= (this.scrollObj.scrollHeight - this.scrollObj.height)) {
        this.scrollObj.scrollTop = (this.scrollObj.scrollHeight - this.scrollObj.height)
        this.scrollDir = 'UP'
        toutms += 1000
      }
    }
  }
  this.timeOuts.tscrollTO.handler = window.setTimeout(function() {_self.scrollUpDwn();}, toutms)
 
}

fScroll.prototype.startScroll = function() {
  var _self = this
  this.scrollingOn = true
  // faccio partire un tout al termine del quale scrollo in su o in giu di tot px
  this.timeOuts.tscrollTO.handler = window.setTimeout(function(){_self.scrollUpDwn();}, this.timeOuts.tscrollTO.ms)
}

fScroll.prototype.stopScroll = function() {
  this.scrollingOn = false
  window.clearTimeout(this.timeOuts.tscrollTO.handler)
}

fScroll.prototype.switchScroll = function() {
  if (this.scrollingOn) {
    this.stopScroll()
  } else {
    this.startScroll()
  }
}


