var scrollticker = Class.create();
scrollticker.prototype = {
  speed:      40,
  oContainer: null,
  oList:      null,
  
  oAnimation: null,
  
  initialize: function(sScrolltickerId)
  {
    this.oContainer = $(sScrolltickerId);
    if (this.oContainer == null) return;

    this.oList = $(this.oContainer).getElementsBySelector('UL')[0];
    if (this.oList == null) return;

    var aListItems = $A(this.oList.getElementsByTagName('LI'));
    if (aListItems.length < 1) return;

    // Breite aller List-Elemente errechnen
    var iListWidth = 0;
    aListItems.each(function(oListItem)
      {
        iListWidth += $(oListItem).getWidth();
      }
    );

    // Pruefen ob genug Elemente vorhanden sind, wenn nicht muessen
    // die Vorhandenen dupliziert werden.
    var iContainerWidth = $(this.oContainer).getWidth();
    if (iListWidth < (iContainerWidth * 2))
    {
      do
      {
        for (var i=0; i<aListItems.length; i++)
        {
          this.oList.appendChild(aListItems[i].cloneNode(true));
          iListWidth += $(aListItems[i]).getWidth();
        } // for
      } while (iListWidth < (iContainerWidth * 2));
    } // if

    // Start/Stop Events registrieren
    this.oContainer.onmouseover = this.stop.bind(this);
    this.oContainer.onmouseout = this.start.bind(this);

    this.scroll();
  }, // function initialize
  
  scroll: function()
  {
    var iWidth = this.oList.getElementsBySelector('LI')[0].getWidth();
    var iLeftOffset = parseInt($(this.oList).getStyle('left'));
    if (iLeftOffset < 0) iWidth += iLeftOffset;

    this.oAnimation = new Effect.Move (this.oList,
      {
        x: (iWidth * -1),
        transition: Effect.Transitions.linear,
        duration: (iWidth / this.speed),
        afterFinish: this.prepareNextStep.bind(this),
        fps: 15
      }
    );
  },
  
  prepareNextStep: function()
  {
    var oCurItem = this.oList.getElementsBySelector('LI')[0];
    var iCurItemWidth = oCurItem.getWidth();

    this.oList.appendChild(oCurItem.cloneNode(true));

    oCurItem.remove();
    $(this.oList).setStyle({left: '0px'});

    this.scroll();
  }, // function prepareNextScroll
  
  stop: function() { this.oAnimation.cancel(); },
  start: function() { this.scroll(); }
}
