
var undefined;

function NewsTicker(id) {
  this.id = undefined;
  this.text = '';
  this.parentElement = undefined;
  this.time = 350;                          
  this.timeOut = undefined;
  
  this._setID(id);
}

NewsTicker.prototype._setID = function (str) {
  if (arguments.length != 1) {
    focus();
    throw new Error("NewsTicker->_setID: Falsche Anzahl von Argumenten!");
  }
  if (typeof str != "string") {
    focus();
    throw new Error("NewsTicker->_setID: Argument str ist nicht vom Typ String!");
  }
  this.id = str;
}

NewsTicker.prototype.setText = function (str) {
  if (arguments.length != 1) {
    focus();
    throw new Error("NewsTicker->setText: Falsche Anzahl von Argumenten!");
  }
  if (typeof str != "string") {
    focus();
    throw new Error("NewsTicker->setText: Argument str ist nicht vom Typ String!");
  }
  this.text = str;
}

NewsTicker.prototype.setParentElement = function (idx) {
  if (arguments.length != 1) {
    focus();
    throw new Error("NewsTicker->setParentElement: Falsche Anzahl von Argumenten!");
  }
  if (typeof idx != "string") {
    focus();
    throw new Error("NewsTicker->setParentElement: Argument idx ist nicht vom Typ String!");
  }
  var elem = document.getElementById(idx);
  if (! elem) {
    focus();
    throw new Error("NewsTicker->setParentElement: Es existiert kein HTML-Element mit id = "+ idx +" !");
  }
  this.parentElement = elem;
}

NewsTicker.prototype.moveText = function () {
  if (this.timeOut) {
    window.clearTimeout(this.timeOut);
  }
  if (! this.text.length) {
    return;
  }
  if (! this.parentElement) {
    return;
  }
  var str = this.text;
  this.text = str.substring(1, str.length); 
  this.text += str.substring(0, 1); 
  this.parentElement.innerHTML = this.text;
  var id = this.id;
  var func = function() {
    NewsTicker.getInstance(id).moveText();
  }
  this.timeOut = window.setTimeout(func, this.time);
}

NewsTicker._increment = [];
NewsTicker._registerInstance = {};
NewsTicker._registerInstanceLength = [];

NewsTicker.getInstance = function(id) {
  if (arguments.length!=1) {
    throw new Error("Falsche Anzahl von Argumenten!");
  }
  if (! (NewsTicker._registerInstance[id])){
    focus();
    throw new Error("Es ist keine NewsTicker.Instance mit id=" + id + " registriert!");
  }
  return NewsTicker._registerInstance[id];
}

NewsTicker.createInstance = function(id) {
  if (!arguments.length) {
    id = 'NewsTicker' + NewsTicker._increment.length;
    NewsTicker._increment.push(1);
  }
  if (! (NewsTicker._registerInstance[id])){
    NewsTicker._registerInstance[id] = new NewsTicker(id);
    NewsTicker._registerInstanceLength.push(id);
  } else {
    focus();
    throw new Error("NewsTicker.createInstance: id schon vorhanden!");
  }
  return NewsTicker.getInstance(id);
}

