var ScrollingElements_Collection = [];

function ScrollingElements(container_node, container_width, hop, spacing)
{
  this.elements = [];

  this.interval_speed = null;
  this.interval = null;
  this.indice = ScrollingElements_Collection.length;

  this.spacing = (spacing) ? spacing : 0;
  this.hop = (hop) ? hop : 1;

  this.container_node = container_node;
  this.container_node.style.overflow = "hidden";
  
  if (!container_width && container_width != 0)
    this.container_width = container_node.offsetHeight;
  else
    this.container_width = container_width;
    
  this.start = ScrollingElements_Start;
  this.stop = ScrollingElements_Stop;
  this.resume = ScrollingElements_Resume;
  this.scroll = ScrollingElements_Scroll;
  this.sortElements = ScrollingElements_SortElements;
  this.positionElements = ScrollingElements_PositionElements;
  this.addImageLink = ScrollingElements_AddImageLink;
  this.addText = ScrollingElements_AddTextLink;
  this.imageExists = ScrollingElements_ImageExists;
  
  ScrollingElements_Collection[this.indice] = this;
}

function ScrollingElements_Start(interval_speed)
{
  this.stop();

  this.positionElements();
  this.interval_speed = interval_speed;

  this.interval = setInterval(
    "ScrollingElements_Collection["+ this.indice +"].scroll()",
    this.interval_speed
  );
}

function ScrollingElements_Resume()
{
  this.interval = setInterval(
    "ScrollingElements_Collection["+ this.indice +"].scroll()",
    this.interval_speed
  );
}

function ScrollingElements_Stop()
{
  if (this.interval) {
    clearInterval(this.interval);
    this.interval = null;
  }
}

function ScrollingElements_Scroll()
{

  var len = this.elements.length, y, newY, endY, i;
  for (i = 0; i < len; i++) {
    //if (!this.elements[i].style.top) this.elements[i].style.top = 0;
    y = parseInt(this.elements[i].style.top);
    newY = y - this.hop;

    if (newY + this.elements[i].offsetHeight < 0) {
      endY = parseInt(this.elements[len - 1].style.top) + this.elements[len - 1].offsetHeight + this.spacing;
      //endY=0;

      if (endY < this.container_width)
        endY = this.container_width;

      this.elements[i].style.top = endY;
    }
    else
      this.elements[i].style.top = newY;
  }
  this.sortElements();
}

function ScrollingElements_SortElements()
{

  var sort_func = function(x, y) {
    x = parseInt(x.style.top);
    y = parseInt(y.style.top);
    if (x < y) return -1;
    else if (x === y) return 0;
    else return 1;
  };
  this.elements.sort(sort_func);
}

function ScrollingElements_PositionElements()
{

  var len = this.elements.length, i;
  for (i = 0; i < len; i++) {
    if (i > 0)
      this.elements[i].style.top = parseInt(this.elements[i - 1].style.top) + this.elements[i - 1].offsetHeight + this.spacing;
    else
      this.elements[i].style.top = 0;
  }
  this.sortElements();
}

function ScrollingElements_AddImageLink(image_src, href, className)
{
  if (!this.imageExists(image_src)) {
    var anchor, indice = this.elements.length;

    anchor = document.createElement("A");
    anchor.name = "scroll_element_"+ indice;
    anchor.href = href;
    anchor.target = "_blank";
    anchor.className = className;

    var img = document.createElement("IMG");
    img.src = image_src;
    img.onload = function()
    {
      var len = scroller.elements.length, i;

      for (i = 0; i < len; i++)
        if (i > 0)
          scroller.elements[i].style.left = parseInt(scroller.elements[i - 1].style.left) + scroller.elements[i - 1].offsetWidth + scroller.spacing;

      scroller.sortElements();
    }

    anchor.appendChild(img);

    this.elements[indice] = anchor;
    this.container_node.appendChild(this.elements[indice]);
  }
}

function ScrollingElements_AddTextLink(text, className)
{

  var anchor, indice = this.elements.length;
  if (document.getElementById(text)) {
  anchor = document.getElementById(text);
  //anchor = document.createElement("P");
  if(navigator.userAgent.indexOf("Firefox")!=-1) {
    anchor.className = className;
  } else {
    anchor.className = className;
  }
  //anchor.appendChild( document.createTextNode(text) );

  this.elements[indice] = anchor;
  this.container_node.appendChild(this.elements[indice]);

  this.positionElements();
  }
}

function ScrollingElements_ImageExists(image_src)
{
  var len = this.elements.length, i;
  for (i = 0; i < len; i++)
    if (this.elements[i].childNodes[0].src == image_src)
      return(true);

  return(false);
}