var SCROLLER;
var SCROLLERS;

function Scroller(type) {
  SCROLLER = this;
  SCROLLER[type] = this;

  /* Attributes. */

  this.type = ( type ? type : 'news' );
  this.scrollRef = "SCROLLER_" + this.type;
  this.nbNews = 0;
  this.currentNewsid = 0;
  this.time = 30;
  this.waitTime = 7000;
  this.offset = 5;

  this.currentScrollPanel1;
  this.currentScrollHeight1;
  this.currentScrollPanel2;
  this.currentScrollHeight2;

  this.scrollerPanel1;
  this.scrollerHeight1;

  this.scrollerPanel2;
  this.scrollerHeight2;

  this.scrollerTimeoutArrive1;
  this.scrollerTimeoutEnd1;
  this.scrollerTimeoutArrive2;
  this.scrollerTimeoutEnd2;

  /* Methods. */

  this.init = function(panel, nbNews, width, height) {
    this.nbNews = nbNews;
    this.scrollerHeight = height;

    // Init container
    this.scrollerPanel = document.createElement("div");
    this.scrollerPanel.style.overflow = "hidden";
    this.scrollerPanel.style.position = "absolute";
    this.scrollerPanel.style.height   = height + "px";
    this.scrollerPanel.style.width    = width + "px";
    panel.appendChild(this.scrollerPanel);
    var scrollerPanel2 = this.scrollerPanel.cloneNode(true);
    panel.appendChild(scrollerPanel2);
    
    // Init dynamic scroller panel
    this.currentScrollPanel1  = document.createElement("div");
    this.currentScrollPanel1.style.position = "relative";
    this.currentScrollPanel1.style.top = "0px";
    this.currentScrollPanel1.style.left = "0px";
    this.scrollerPanel.appendChild(this.currentScrollPanel1);

    this.currentScrollPanel2 = document.createElement("div");
    this.currentScrollPanel2.style.position = "relative";
    this.currentScrollPanel2.style.top = "0px";
    this.currentScrollPanel2.style.left = "0px";
    scrollerPanel2.appendChild(this.currentScrollPanel2);

    var _this = this;
   
    window.onload = function() { 
      SCROLLER.scrollPanel1();
    };
  };

  /* ----------------------- */

  this.scrollPanel1 = function() {
    this.currentNewsid++;
    if (this.currentNewsid > this.nbNews)
      this.currentNewsid = 1;
    var panel = document.getElementById(this.type + "_" + this.currentNewsid);
    if (panel == null)
      return;
    this.currentScrollPanel1.style.top = this.scrollerHeight;
    this.currentScrollHeight1          = panel.offsetHeight;
    this.currentScrollPanel1.innerHTML = panel.innerHTML;
    this.scrollerTimeoutArrive1        = setInterval(this.scrollRef + ".scrollArrive1()", this.time);
  };

  this.scrollArrive1 = function() {
    var top = parseInt(this.currentScrollPanel1.style.top) - this.offset;
    if (top < 0) {
      clearTimeout(this.scrollerTimeoutArrive1);
      setTimeout(this.scrollRef + ".wait1()", this.waitTime);  
  } else {
      this.currentScrollPanel1.style.top = top;
    }
  };

  this.wait1 = function() {
    this.scrollerTimeoutEnd1 = setInterval(this.scrollRef + ".scrollEnd1()", this.time);    
    this.scrollPanel2();
  };

  this.scrollEnd1 = function() {
    var top = parseInt(this.currentScrollPanel1.style.top) - this.offset;
    if (-(top) >= (this.currentScrollHeight1 + this.offset)) {
      clearTimeout(this.scrollerTimeoutEnd1);
      this.currentScrollPanel1.innerHTML = "";
    } else {
      this.currentScrollPanel1.style.top = top;
    }
  };

  /* ----------------------- */

  this.scrollPanel2 = function() {
    this.currentNewsid++;
    if (this.currentNewsid > this.nbNews)
      this.currentNewsid = 1;
    var panel = document.getElementById(this.type + "_" + this.currentNewsid);
    this.currentScrollPanel2.style.top = this.scrollerHeight;
    this.currentScrollHeight2 = panel.offsetHeight;
    this.currentScrollPanel2.innerHTML = panel.innerHTML;
    this.scrollerTimeoutArrive2 = setInterval(this.scrollRef + ".scrollArrive2()", this.time);    
  };

  this.scrollArrive2 = function() {
    var top = parseInt(this.currentScrollPanel2.style.top) - this.offset;
    if (top < 0) {    
      clearTimeout(this.scrollerTimeoutArrive2); 
      setTimeout(this.scrollRef + ".wait2()", this.waitTime);
    } else {
      this.currentScrollPanel2.style.top = top;
    }
  };

  this.wait2 = function() {
    this.scrollerTimeoutEnd2 = setInterval(this.scrollRef + ".scrollEnd2()", this.time);    
    this.scrollPanel1();
  };

  this.scrollEnd2 = function() {
    var top = parseInt(this.currentScrollPanel2.style.top) - this.offset;
    if (-top >= (this.currentScrollHeight1 + this.offset)) {
      clearTimeout(this.scrollerTimeoutEnd2);
      this.currentScrollPanel2.innerHTML = "";
    } else {
      this.currentScrollPanel2.style.top = top;
    }
  };

}
