var RESOSCO_scrollTimer = null;
var RESOSCO_scrollTimeout = 5;
var RESOSCO_scrollStep = 5;

var RESOSCO_scrollers = {};

var RESOSCO_scroller = new Class({
   initialize: function(id) {
      this.id = id;
      this.scrollPos = 0;
      this.scrollableElement = null;
   },
   
   resetPosition: function() {
      var scrollable = this._getScrollableElement();
      if (null == scrollable) {
         return false;
      }
      this.scrollPos = 0;
      scrollable.scrollTo(0);
   },

   getPosition: function() {
      var scrollable = this._getScrollableElement();
      if (null == scrollable) {
         return false;
      }
      return scrollable.scrollTop;
   },
   
   up: function() {
      this._startScroll('up');
   },
   
   down: function() {
      this._startScroll('down');
   },
   
   _startScroll: function(direction) {
      var scrollable = this._getScrollableElement();
      if (null == scrollable) {
         return false;
      }
      
      if (RESOSCO_scrollTimer == null) {
         RESOSCO_scrollTimer = setInterval(function(){
            if ('down' == direction) {
               scrollable.scrollTop += RESOSCO_scrollStep;
            } else if('up' == direction) {
               scrollable.scrollTop -= RESOSCO_scrollStep;
            }
            this.scrollPos = scrollable.scrollTop;
            if (this.scrollPos < 0) {
               this.scrollPos = 0;
            }
         }, RESOSCO_scrollTimeout);
      }
   },
   
   _getScrollableElement: function() {
      if (null != this.scrollableElement) {
         return this.scrollableElement;
      }
   },
   
   setScrollableElement: function(el) {
      this.scrollableElement = $(el);
//      this.resetPosition();
   }
});


RESOSCO_scroller.getInstance = function(id) {
   if (null != RESOSCO_scrollers[id]) {
      return RESOSCO_scrollers[id];
   }
   RESOSCO_scrollers[id] = new RESOSCO_scroller(id);
   return RESOSCO_scrollers[id];
};

RESOSCO_scroller.stop = function() {
   if (RESOSCO_scrollTimer != null) {
      clearInterval(RESOSCO_scrollTimer);
      RESOSCO_scrollTimer = null;
   }
};
