/*
 * Javascript scroller
 *
 * minScroller allows multiple instances of vertical or horizontal scroller.
 * The content may be valid html. In autoscroll mode, padding is being added
 * automatically to enable circular scolling.
 *
 * minScroller.js 0.2 2009-04-17
 * - add autoscroll
 * - add horizontal scroll (e.g. newsticker)
 *
 * minScroller.js 0.1 2006-03-12
 *
 *
 * Copyright (c) 2009 ipdienste.net, Duesseldorf. All Rights Reserved.
 * Author: Lutz Eymers
 * Contact: lutz ___AT___ ipdienste ___DOT___ net
 * Download: http://www.ipdienste.net/data/minScroller/minScroller.tgz
 * Demo: http://www.ipdienste.net/data/minScroller/index.html
 *
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for any purposes and without fee
 * is hereby granted provided that this copyright notice
 * appears in all copies.
 *
 * Of course, this software is provided "as is" without express or implied
 * warranty of any kind.
 *
 */

function minScroller(cfg) {
  this.cfg = cfg;

  var _onload = window.onload;
  if (typeof _onload == 'function') {
    window.onload = function() {
      _onload();
      eval(cfg['instance'] + '.onload()');
    }
  } else {
    eval('window.onload = function () { ' + cfg['instance'] + '.onload() }');
  }
}

minScroller.prototype.init = function() {
  this.instance   = this.cfg['instance'];
  this.ms         = typeof this.cfg['ms'] != 'undefined' ? parseInt(this.cfg['ms']) : 25;
  this.sh         = typeof this.cfg['sh'] != 'undefined' ? Math.abs(parseInt(this.cfg['sh'])) : 1;
  this.auto       = typeof this.cfg['auto'] == 'string' ? this.cfg['auto'] : '';
  this.skipFst    = typeof this.cfg['skipFst'] == 'number' ? Math.abs(parseInt(this.cfg['skipFst'])) : 0;
  this.skipLst    = typeof this.cfg['skipLst'] == 'number' ? Math.abs(parseInt(this.cfg['skipLst'])) : 0;
  this.startAt    = typeof this.cfg['startAt'] == 'number' ? Math.abs(parseInt(this.cfg['startAt'])) : 0;
  this.mode       = typeof this.cfg['mode'] == 'string' && this.cfg['mode'] == 'horizontal' ? 'horizontal' : 'vertical';
  this.clipEl     = document.getElementById(this.cfg['id']);
  this.upEl       = document.getElementById(this.cfg['id'] + '_up');
  this.downEl     = document.getElementById(this.cfg['id'] + '_down');
  this.clipHeight = this.clipEl.offsetHeight;
  this.clipWidth  = this.clipEl.offsetWidth;
  this.isRunning  = 0;
  this.pos        = 0;
  if (this.ms < 10) this.ms = 10;

  var addPadding = '';
  if (this.auto == 'n') {
    addPadding = 'padding-top:' + this.clipHeight + 'px;'
    this.sh *= -1;
    this.mode = 'vertical';
  } else if (this.auto == 's') {
    addPadding = 'padding-bottom:' + this.clipHeight + 'px;'
    this.mode = 'vertical';
  } else if (this.auto == 'w') {
    addPadding = 'padding-left:' + this.clipWidth + 'px;'
    this.sh *= -1;    
    this.mode = 'horizontal';
  } else if (this.auto == 'e') {
    addPadding = 'padding-right:' + this.clipWidth + 'px;'
    this.mode = 'horizontal';
  } else {
    this.auto = '';
  }

  if (this.mode == 'vertical') {
    this.clipEl.innerHTML = '<div style="position:absolute;width:' + this.clipEl.offsetWidth + 'px;' + addPadding + '">' + this.clipEl.innerHTML +  '</div>';
  } else {
    this.clipEl.innerHTML = '<div style="position:absolute;white-space:nowrap;' + addPadding + '">' + this.clipEl.innerHTML +  '</div>';
  }

  this.scrollEl         = this.clipEl.firstChild;
  this.scrollHeight     = this.scrollEl.offsetHeight;
  this.scrollWidth      = this.scrollEl.offsetWidth;  
  
  if (this.mode == 'vertical' && (this.scrollHeight > this.clipHeight) || this.mode == 'horizontal' && (this.scrollWidth > this.clipWidth)) {
    if (this.upEl) {
      this.upEl.style.visibility = 'visible';
      eval('this.upEl.onmousedown = function () { ' + this.instance + '.up() }');
      eval('this.upEl.onmouseup = function () { ' + this.instance + '.stop() }');
    }
    if (this.downEl) {
      this.downEl.style.visibility = 'visible';
      eval('this.downEl.onmousedown = function () { ' + this.instance + '.down() }');
      eval('this.downEl.onmouseup = function () { ' + this.instance + '.stop() }');
    }
  }
  if (this.skipFst > 0 || this.startAt > 0) {
    this.pos = -(this.skipFst > this.startAt ? this.skipFst : this.startAt);
  }
  this.setPos();
  if (this.auto == 's') this.pos = -this.scrollHeight + this.clipHeight;
  if (this.auto == 'e') this.pos = -this.scrollWidth + this.clipWidth;
  if (this.auto != '') this.go();
}

minScroller.prototype.down = function(init) 
{
  this.sh = -1;
  this.go();
}

minScroller.prototype.up = function(init) 
{
  this.sh = 1;
  this.go();
}

minScroller.prototype.check = function() {
  if (this.auto == 's') {
    if (this.pos > this.clipHeight) {
      this.pos = -this.scrollHeight + this.clipHeight;
    }
  } else if (this.auto == 'n') {
    if (this.pos < -this.scrollHeight) {
      this.pos = 0;
    }
  } else if (this.auto == 'w') {
    if (-this.pos > this.scrollWidth) {
      this.pos = 0;
    }
  } else if (this.auto == 'e') {
    if (this.pos > this.clipWidth) {
      this.pos = -this.scrollWidth + this.clipWidth;
    }
  } else {
    if (this.pos > -this.skipFst) {
      this.pos = -this.skipFst
    }
    if (this.mode == 'vertical') {
      if (this.pos < -this.scrollHeight + this.clipHeight + this.skipLst) {
        this.pos = -this.scrollHeight + this.clipHeight + this.skipLst
      }
    } else {
      if (this.pos < -this.scrollWidth + this.clipWidth + this.skipLst) {
        this.pos = -this.scrollWidth + this.clipWidth + this.skipLst
      }
    }  
  }
}

minScroller.prototype.setPos = function(pos) {
  if (typeof pos == 'number') {
    this.pos = parseInt(pos);
  }
  this.check();
  if (this.mode == 'vertical') {
    this.scrollEl.style.top = this.pos + 'px';
  } else {
    this.scrollEl.style.left = this.pos + 'px';
  }
}

minScroller.prototype.shift = function() {
  this.pos += this.sh;
  this.setPos();
}

minScroller.prototype.scroll = function() {
  if (this.isRunning) {
    this.shift();
    setTimeout(this.instance + ".scroll()", this.ms);  
  }
}

minScroller.prototype.stop = function() {
  this.isRunning = 0;
}

minScroller.prototype.go = function() {
  if (!this.isRunning) {
    this.isRunning = 1;
    this.scroll();
  }
}

minScroller.prototype.onload = function() {
  this.init();
}

