var LS = new Object();

LS.lineScroller = function(contId,topId,botId,barId,delay) {
  this.name = 'Universal line scroller';
  this.author = 'Nikita N. Zhukov';

  this.contId = contId;
  this.topId = topId;
  this.botId = botId;
  this.barId = barId;
  this.delay = delay;

  this.container = null;
  this.topButton = null;
  this.botButton = null;
  this.thumb = null;

  this.timer = null;
  this.thumbActive = false;
  this.thumbPos = 0;
  this.mousePos = 0;
}

LS.lineScroller.prototype = {
  setScroll: function() {
    var self = this;
    this.container = $$(this.contId);
    this.topButton = $$(this.topId);
    this.botButton = $$(this.botId);
    var scrollBar = $$(this.barId);
    if (scrollBar) {
      this.thumb = scrollBar.firstChild;
    }
    if (this.container && this.topButton && this.botButton && this.thumb) {

      if (this.container.scrollHeight > this.container.clientHeight) {
        // Обработка мышиного колеса
        if (typeof(this.container.addEventListener) != 'undefined') {
          this.container.addEventListener('DOMMouseScroll',function(event) {self.wheelScroll(event)},false);
        } else if (typeof(this.container.attachEvent) != 'undefined') {
          this.container.attachEvent('onmousewheel',function(event) {self.wheelScroll(event)});
        }
        this.container.onmousewheel = function(event) {self.wheelScroll(event)};

        // Обработка кнопок вверх/вниз
        this.topButton.onmousedown = function() {self.activateButton(this); self.startTopScroll()};
        this.botButton.onmousedown = function() {self.activateButton(this); self.startBotScroll()};
        this.topButton.onmouseup = function() {self.stopScroll(); self.disactivateButton(this)};
        this.botButton.onmouseup = function() {self.stopScroll(); self.disactivateButton(this)};

        // Обработка полосы
        document.onmousemove = function(event) {self.thumbScroll(event)};
        document.onmouseup = function() {self.stopThumbScroll(); self.disactivateButton(self.thumb)};
        this.thumb.onmousedown = function(event) {self.activateButton(this); self.startThumbScroll(event)};

        // Обработка клавиш вверх/вниз
        document.onkeypress = function(event) {self.keyScroll(event)};

        // Показ скроллбара
        $style(this.topButton,{display:'block'});
        $style(this.botButton,{display:'block'});
        $style(scrollBar,{display:'block'});

        // Вычисление дефолтного положения полосы
        self.changeThumbPos();
      } else {
        // If need:
        //$style(topButton,{display:'none'});
        //$style(botButton,{display:'none'});
        //$style(scrollBar,{display:'none'});
      }
    }
  },
  startTopScroll: function() {
    var self = this;
    this.timer = window.setInterval(function() {self.topScroll()},this.delay);
  },
  startBotScroll: function() {
    var self = this;
    this.timer = window.setInterval(function() {self.botScroll()},this.delay);
  },
  topScroll: function(k) {
    var self = this;
    if (!k) {
      k = 1;
    }
    if (this.container.scrollTop > 0) {
      this.container.scrollTop -= Math.floor(Math.abs(100/this.delay))*k;
      self.changeThumbPos();
    } else {
      self.stopScroll();
    }
  },
  botScroll: function(k) {
    var self = this;
    if (!k) {
      k = 1;
    }
    if (this.container.scrollTop < this.container.scrollHeight - this.container.clientHeight) {
      this.container.scrollTop += Math.floor(Math.abs(100/this.delay))*k;
      self.changeThumbPos();
    } else {
      self.stopScroll();
    }
  },
  stopScroll: function() {
    window.clearInterval(this.timer);
    this.timer = null;
  },
  startThumbScroll: function(event) {
    var self = this;
    this.thumbActive = true;
    if (!event) {
      event = window.event;
    }
    this.mousePos = event.clientY;
    this.thumbPos = this.thumb.offsetTop;

    // Запрет выделения мышью
    event.cancelBubble = true;
    if (event.preventDefault) {
      event.preventDefault();
    } else {
      event.returnValue = false;
    }
    document.onselectstart = function() {return false};
  },
  thumbScroll: function(event) {
    var self = this;
    if (this.thumbActive) {
      if (!event) {
        event = window.event;
      }
      var delta = event.clientY - this.mousePos;
      var thumbHeight = this.thumb.offsetHeight;
      var barHeight = this.thumb.parentNode.offsetHeight + this.topButton.offsetHeight + this.botButton.offsetHeight;

      if ((this.thumbPos + delta > 0) && (this.thumbPos + delta + thumbHeight < barHeight)) {
        this.container.scrollTop = Math.floor((this.thumbPos + delta)*(this.container.scrollHeight/barHeight));
      } else if (this.thumbPos + delta <= 0) {
        this.container.scrollTop = 0;
      } else if (this.thumbPos + delta + thumbHeight >= barHeight) {
        this.container.scrollTop = this.container.scrollHeight - this.container.clientHeight;
      }
      self.changeThumbPos();
    }
  },
  stopThumbScroll: function() {
    var self = this;
    this.thumbActive = false;
    document.onselectstart = null;
  },
  wheelScroll: function(event) {
    var self = this;
    var k = 0;
    if (!event) {
      event = window.event;
    }
    if (event.wheelDelta) {
      k = event.wheelDelta / 120;
      if (navigator.userAgent.search(/Opera/) >= 0) {
        k = -k;
      }
    } else if (event.detail) {
      k = -event.detail / 3;
    }
    if (k) {
      self.wheelHandle(k);
    }
    if (event.preventDefault) {
      event.preventDefault();
    }
    event.returnValue = false;
  },
  wheelHandle: function(k) {
    var self = this;
    if (k < 0) {
      self.botScroll(2);
    } else {
      self.topScroll(2);
    }
  },
  keyScroll: function(event) {
    var self = this;
    if (!event) {
      event = window.event;
    }
    var k = event.keyCode;
    if (k == 38) {
      self.topScroll(2);
    } else if (k == 40) {
      self.botScroll(2);
    }
  },
  changeThumbPos: function() {
    var self = this;
    var pPercent = this.container.scrollTop/(this.container.scrollHeight - this.container.clientHeight);
    var barHeight = this.thumb.parentNode.clientHeight;
    var thumbHeight = this.thumb.clientHeight;
    var thumbPos = Math.floor(pPercent*(barHeight - thumbHeight));
    $style(this.thumb,{top:thumbPos});
  },
  activateButton: function(el) {
    $addClass(el,'active');
  },
  disactivateButton: function(el) {
    $removeClass(el,'active');
  }
};

var cScroll = new LS.lineScroller('cPlace','cTopScroll','cBotScroll','cScrollBar',10);

function lineScrollSetup() {
  cScroll.setScroll();
}