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 : 5;
  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.offsetWidth;
  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.addTextLink = 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, x, newX, endX, i;
  for (i = 0; i < len; i++) {
    x = parseInt(this.elements[i].style.left);
    newX = x - this.hop;

    if (newX + this.elements[i].offsetWidth < 0) {
      endX = parseInt(this.elements[len - 1].style.left) + this.elements[len - 1].offsetWidth + this.spacing;

      if (endX < this.container_width)
        endX = this.container_width;

      this.elements[i].style.left = endX;
    }
    else
      this.elements[i].style.left = newX;
  }
  this.sortElements();
}

function ScrollingElements_SortElements()
{
  var sort_func = function(x, y) {
    x = parseInt(x.style.left);
    y = parseInt(y.style.left);
    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.left = parseInt(this.elements[i - 1].style.left) + this.elements[i - 1].offsetWidth + this.spacing;
    else
      this.elements[i].style.left = 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, href, className)
{
  var anchor, indice = this.elements.length;

  anchor = document.createElement("A");
  anchor.href = href;
  anchor.target = "_blank";
  anchor.className = className;
  anchor.name = "scroll_element_"+ indice;
  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);
}