/*
 * minNewsticker.js 0.1 2009-03-29
 *
 * Javascript newsticker prototype
 *
 * Copyright (c) 2009 ipdienste.net, Duesseldorf. All Rights Reserved.
 * Author: Lutz Eymers
 * Contact: lutz ___AT___ ipdienste ___DOT___ net
 * Download: http://www.ipdienste.net/data/minNewsticker.tgz
 *
 * 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.
 *
 */

minNewsticker = function(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() }');
  }
}

minNewsticker.prototype.init = function() {
  this.instance = this.cfg['instance'];
  this.clipEl = document.getElementById(this.cfg['id']);
  this.ms = typeof this.cfg['ms'] != 'undefined' ? parseInt(this.cfg['ms']) : 25;
  this.clipWidth = typeof this.cfg['clipWidth'] != 'undefined' ? parseInt(this.cfg['clipWidth']) : parseInt(this.clipEl.style.width);
  this.sh = -1;
  this.isRunning = 0;
  this.pos = 0;
  this.clipEl.innerHTML = '<div style="position:absolute;white-space:nowrap;padding-left:' + this.clipWidth + 'px">' + this.clipEl.innerHTML +  '</div>';
  this.scrollEl = this.clipEl.firstChild;
  this.scrollWidth = this.scrollEl.offsetWidth;  
}

minNewsticker.prototype.shift = function() {
  if (-this.pos > this.scrollWidth) {
    this.pos = 0;
  } else {
    this.pos += this.sh;
  }
  this.scrollEl.style.left = this.pos + 'px';
}

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

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

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

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

