// ###############################
// ## NewsSlider for JavaScript ##
// ## (c) 2011 by R. Hecksteden ##
// ## Published under the GPLv2 ##
// ## Documentation:            ##
// ##  https://www.jurmatix.de/ ##
// ###############################

function NewsSlider(elm,seconds){
  this.IE = document.all&&!window.opera;
  this.DOM = document.getElementById&&!this.IE;

  this.element = elm;
  this.milliseconds = seconds * 1000;
  this.news = document.getElementById(elm).getElementsByTagName("li");
  this.currentElm = 0;
  this.currentTransparency = 1;
  var selfobject = this;
  window.setInterval (function(){selfobject.nextNews();}, this.milliseconds);

  this.nextNews = function() {
    this.fadeOutCurrentElement();
  }

  this.fadeOutCurrentElement = function () {
    var selfobject = this;
    this.currentTransparency = this.currentTransparency - 0.1;
    this.setTransparency(this.news[this.currentElm], this.currentTransparency);
    if (this.currentTransparency >= 0.1) {
       window.setTimeout (function(){selfobject.fadeOutCurrentElement();}, 60);
    } else {
       this.showNext();
    }
  }

  this.showNext = function() {
    this.currentElm = this.currentElm + 1;
    if (this.currentElm >= this.news.length) this.currentElm = 0;
    for(var i=0;i<this.news.length;i++) {
      this.news[i].style.display = "none";
      if (this.currentElm == i) {
        this.setTransparency(this.news[i],0);
        this.news[i].style.display = "block";
      }
    }
    this.fadeInCurrentElement();
  }

  this.fadeInCurrentElement = function () {
    var selfobject = this;
    this.currentTransparency = this.currentTransparency + 0.1;
    this.setTransparency(this.news[this.currentElm], this.currentTransparency);
    if (this.currentTransparency <= 0.9) {
       window.setTimeout (function(){selfobject.fadeInCurrentElement();}, 60);
    }
  }
  
  this.setTransparency = function(elm, intWert){
    if(this.IE){
      intWert *= 100;
      elm.style.filter = "alpha(opacity="+intWert+")";
    }
    if(this.DOM && !window.opera){
      elm.style.MozOpacity = intWert;
    }
  }
}
