/* ************************************************************************************** *
 * Das Script kann frei verwendet werden, dieser Kommentar, der URL sowie die Nennung des
 * Nicks müssen jedoch erhalten bleiben.
 *
 *                                                                  Quelle: www.quaese.de
 *                                                                       (c) Quaese, 2008
 * ************************************************************************************** */
function qpAnimMove(){
  var intLen = qpAnimMove.arguments.length;  // Länge der Argumentenliste

  // Debugging
	this.blnDebug = false;  // Debugging ein-/ausschalten (true/false)

  // Debug-Bereich erstellen
  if(this.blnDebug){
    this.DebugArea = document.createElement("div");
    this.DebugArea.style.fontFamily = "courier";
    this.DebugArea.style.fontSize = "9pt";
    this.DebugArea.style.position = "absolute";
    this.DebugArea.style.zIndex = 999;
    this.DebugArea.style.top = "0";
    this.DebugArea.style.right = "0";
    this.DebugArea.style.backgroundColor = "#fff";
    this.DebugArea.style.border = "1px solid #ccc";
    this.DebugArea.style.width = "400px";
    this.DebugArea.style.height = "400px";
    this.DebugArea.style.padding = "9px";
    this.DebugArea.style.overflow = "auto";
    document.getElementsByTagName("body")[0].appendChild(this.DebugArea);
  }


  // Optionsparameter auswerten
  this.ID       = (intLen && typeof qpAnimMove.arguments[0].id != "undefined")? qpAnimMove.arguments[0].id : new Date().getTime();
  this.Width    = (intLen && typeof qpAnimMove.arguments[0].width != "undefined")? qpAnimMove.arguments[0].width : 200;
  this.Height   = (intLen && typeof qpAnimMove.arguments[0].height != "undefined")? qpAnimMove.arguments[0].height : 100;
  this.First    = (intLen && typeof qpAnimMove.arguments[0].first != "undefined")? qpAnimMove.arguments[0].first : 1;
	this.Duration = (intLen && typeof qpAnimMove.arguments[0].duration != "undefined")? qpAnimMove.arguments[0].duration : 50;
  this.Type     = (intLen && typeof qpAnimMove.arguments[0].type != "undefined")? qpAnimMove.arguments[0].type : "quadratic";
  this.Wait     = (intLen && typeof qpAnimMove.arguments[0].wait != "undefined")? (qpAnimMove.arguments[0].wait*1000) : 3000;
  this.News     = (intLen && typeof qpAnimMove.arguments[0].news != "undefined")? (qpAnimMove.arguments[0].news) : [];

  // Falls keine News übergeben wurden -> kein Ticker
  if(this.News.length == 0) return;

  // Bewegungsparameter
  this.Distance = this.Height + 1;
  this.Time     = 0;
  this.Signum   = -1;
  this.DirStyle = "top";
  this.Start    = (this.First == 1)? 0 : this.Height;

  // Tickerkonstrukt erstellen und enthaltene Elemente auswerten
  this.CreateTicker();
  this.Tickers   = this.MoveObject.getElementsByTagName("div");  // Ticker-DIVs
  this.TickCount = 0;                                            // Tickerzähler

  // Bewegungstyp analysieren
  switch(this.Type.toLowerCase()){
  	case 'quadratic': this.fnMove = this.animQuadratic;
                      break;
  	case 'sine'			: this.fnMove = this.animSine;
    									break;
  	case 'linear'		: this.fnMove = this.animLinear;
    									break;
  	default					: this.fnMove = this.animQuadratic;
  }


  // Timerhandles für Bewegung und Ticker
  this.hTimer = null;
  this.hTimerTicker = null;
}


qpAnimMove.prototype.CreateTicker = function(){
  //document.write("<div id=\"tickerHolder\"></div>");
  document.write("<div id=\""+this.ID+"\"></div>");

  this.TickerHolder = document.getElementById(this.ID);
  this.TickerHolder.style.width = this.Width + "px";
  this.TickerHolder.style.height = (this.Height-2) + "px";
  this.TickerHolder.style.position = "relative";
  this.TickerHolder.style.overflow = "hidden";

  this.MoveObject = document.createElement("div");
  this.MoveObject.style.position = "absolute";
  this.MoveObject.style.width = this.Width + "px";
  this.MoveObject.style.left = "0px";
  this.MoveObject.style.top = this.Start + "px";
  this.MoveObject.style.color = "#000";

  for(var i=0; i<this.News.length; i++){
  	this.CreateSingleTicker(i);
  }
  // Falls nur eine News angezeigt werden soll
  if(this.News.length == 1)
  	this.CreateSingleTicker(0);

  this.TickerHolder.appendChild(this.MoveObject);
}

qpAnimMove.prototype.CreateSingleTicker = function(intIndex){
    var objNews = document.createElement("div");
    objNews.style.width = this.Width + "px";
    objNews.style.height = this.Height + "px";
    //objNews.style.backgroundColor = "#f00";
    objNews.style.overflow = "hidden";
    objNews.innerHTML = this.News[intIndex].html;
    objNews.className = this.News[intIndex].css;
    this.MoveObject.appendChild(objNews);
}


qpAnimMove.prototype.Move = function(){
  var _this = this;
  this.MoveObject.style[this.DirStyle] = (this.Start + this.Signum * (this.fnMove() - this.Start)) + "px";
  this.Time++;

  if(this.Time < this.Duration){
    this.hTimer = window.setTimeout(function(){_this.Move()}, 30);
  }else{
    this.hTimer = null;
  	this.Start = parseInt(this.MoveObject.style[this.DirStyle]);
    this.Time = 0;
    // Falls das Ende der Ticker-Nachrichten erreicht ist
    if(this.TickCount++ == (this.Tickers.length-(1+this.First))){
    	var objClone = this.MoveObject.cloneNode(true);
      var objSingle = objClone.getElementsByTagName("div")[this.Tickers.length-1].cloneNode(true);
      objClone.removeChild(objClone.getElementsByTagName("div")[this.Tickers.length-1]);
      objClone.insertBefore(objSingle, objClone.getElementsByTagName("div")[0]);
      objClone.style[this.DirStyle] = "0px";
      this.MoveObject.parentNode.replaceChild(objClone, this.MoveObject);
      this.MoveObject = objClone;
  		this.Tickers   = this.MoveObject.getElementsByTagName("div");
      this.TickCount = 1-this.First;
      this.Start = 0;
    }
    this.hTimerTicker = window.setTimeout(function(){_this.Move();}, _this.Wait);

    this.Debug("this.MoveObject.style["+this.DirStyle+"] = " + this.MoveObject.style[this.DirStyle], "Move");
  	this.Debug("this.TickCount = " + this.TickCount, "Move");
  }
}

qpAnimMove.prototype.Stop = function(intTimer){
	var _this = this;
  var timerHandle = (intTimer == 0)? this.hTimer : this.hTimerTicker;
	if(this.hTimer != null){
  	window.clearTimeout(this.hTimer);
    this.hTimer = null;
  }
  if(this.hTimerTicker != null){
  	window.clearTimeout(this.hTimerTicker);
    this.hTimerTicker = null;
  }
}

qpAnimMove.prototype.animQuadratic = function(){
  return (-6*this.Distance)/(this.Duration*this.Duration)*((this.Time*this.Time*this.Time)/(3*this.Duration)-(this.Time*this.Time)/2) + this.Start;
}

qpAnimMove.prototype.animLinear = function(){
	return this.Distance*this.Time/this.Duration + this.Start;
}

qpAnimMove.prototype.animSine = function(){
	return -this.Distance/2 * (Math.cos(Math.PI*this.Time/this.Duration) - 1) + this.Start;
}

qpAnimMove.prototype.Debug = function(strMsg, strFn){
	if(this.blnDebug){
  	this.DebugArea.innerHTML += "<p>" + new Date() + ":<br>" + strFn + ": " + strMsg + "</p>";
  }
}

qpAnimMove.prototype.StartTicker = function(){
	if((this.hTimer == null) && (this.hTimerTicker == null))
	  this.Move();
}
