var ScrollerClass = Class.create({
  
  /* Constructor method    
   */
  initialize: function(containerDivId, options) {
    options = new Hash(options || {});
    var defaultOptions = new Hash({type : 'vertical', startOnDocumentLoad : true});
    options = defaultOptions.merge(options);
    if(options.get('type') != 'vertical' && options.get('type') != 'horizontal') throw "Illegal scrolling type. Must be either: vertical or horizontal";    
    this.options = options;
    this.container = $(containerDivId);
    this.container.scroller = this;    
    this.scrolling = false;
    this.timer = null;    
    
    var self = this;
    document.observe("dom:loaded", function() {        
      self.initContainer();
    });
    if(this.options.get('startOnDocumentLoad') == true){      
      document.observe("dom:loaded", function() {        
        setTimeout(function(){ self.start(); }, 1000);
      });
    }
  },
  
  initContainer : function(){
    //this.container.style.height.replace('px','');
    //this.container.style.width.replace('px','');

    var scrollableContent = this.container.innerHTML;
    var spacer;
    
    if(this.isVertical()){
      spacer = '<div style="height:'+this.container.getHeight()+'px">&nbsp;</div>';
    }else {
      spacer = '<img style="width:'+this.container.getWidth()+'px;height:1px;" src="/images/spacer.gif" />';
    }
    this.container.update(spacer);
    this.container.insert(scrollableContent);
    this.container.insert(spacer);
    this.container.scrollTop = 0;
    
    this.repeatHeight = this.container.scrollHeight; //get the current height so we know when to wrap
    this.repeatWidth = this.container.scrollWidth; //get the current height so we know when to wrap
    if(this.isRTL() && !this.isFirefox3()){
      this.container.scrollLeft = this.repeatWidth - this.container.getWidth();
    }else {
      this.container.scrollLeft = 0;
    }       

  },
  
  isVertical : function(){
    return this.options.get('type') == 'vertical';
  },
  
  isRTL : function(){
    return this.options.get('direction') == 'rtl';
  },
  
  isFirefox3 : function(){
    return BrowserDetect.browser == "Firefox" && BrowserDetect.version >= 3;
  },
  
  scroll : function(){
    if(this.timer) clearTimeout(this.timer);
    
    if(!this.scrolling){
      return;
    }
    
    if(this.isVertical()){
      this.scrollVertically();
    }
    else {
      if(this.isRTL()){
        this.scrollHorizontallyRight();
      }
      else {
        this.scrollHorizontallyLeft();
      }      
    }
    
  },
  
  scrollHorizontallyLeft : function(){    
    this.container.scrollLeft = this.container.scrollLeft+1;
    var self = this;
    if(this.container.scrollLeft <= this.repeatWidth - this.container.getWidth()) {
      // keep on scrolin' 
      this.timer = setTimeout(function(){ self.scroll(); }, 40);
    }
    else { //we have hit the wrap point
      this.container.scrollLeft = 0;
      this.timer = setTimeout(function(){ self.scroll(); }, 40);
    }
  },
  
  scrollHorizontallyRight : function(){    
    this.container.scrollLeft = this.container.scrollLeft-1;
    var self = this;
    if(this.isFirefox3()){
      //firefox3 has different handlign of scrollLeft property with rtl direction
      //the scrollLeft must start from 0, and go to the negative
      if(this.container.scrollLeft > -(this.repeatWidth - this.container.getWidth())) {
        // keep on scrolin' 
        this.timer = setTimeout(function(){ self.scroll(); }, 40);
      }
      else { //we have hit the wrap point
        this.container.scrollLeft = 0;
        this.timer = setTimeout(function(){ self.scroll(); }, 40);
      }
    }
    else {
      if(this.container.scrollLeft > 0) {
        // keep on scrolin' 
        this.timer = setTimeout(function(){ self.scroll(); }, 40);
      }
      else { //we have hit the wrap point
        this.container.scrollLeft = this.repeatWidth - this.container.getWidth();
        this.timer = setTimeout(function(){ self.scroll(); }, 40);
      }
    }
    
  },
  
  scrollVertically : function(){
    this.container.scrollTop = this.container.scrollTop+1;
    var self = this;
    if(this.container.scrollTop <= this.repeatHeight - this.container.getHeight()) {
      // keep on scrolin' 
      this.timer = setTimeout(function(){ self.scroll(); }, 40);
    }
    else { //we have hit the wrap point
      this.container.scrollTop = 0;
      this.timer = setTimeout(function(){ self.scroll(); }, 40);
    }
  },
  
  start : function(){
    this.scrolling = true;
    this.scroll();
  },
  
  stop : function(){
    this.scrolling = false;
  },


  debug : function(message){
    if($('debugger')){
      $('debugger').insert(message +" <br/>");
    }
  }
});
