/** mooTicker - Newsticker class
 *  Original copyright 2006 Wolfgang Bartelme, Bartelme Design - http://bartelme.at
 *
 *  Ported and edited for mootools by Huug Helmink
 *  version 0.4
 *  date 2009-01-13
 *
 *  Usage:
 *  var myTicker = new Ticker('idOfDivElement');
 *      or (with options)
 *  var myTicker = new Ticker('idOfDivElement',{interval:####});
 *  with #### as an integer > 2000
  */
var mooTicker = new Class({
  Implements: Options,
  options: {
    groupBy: 1,
    interval: 10000
  },
  
  initialize: function(containerElement,options) {  
    // Set options
    this.setOptions(options);
    this.groupBy = this.options.groupBy;
    this.interval = this.options.interval;
    
    // Set container div
    this.container = containerElement;
     
    this.messages = $(this.container).getChildren();
    this.number_of_messages = this.messages.length - 1;
    
    this.group = new Array(this.groupBy);
    this.start_message = 0;
    this.end_message = this.groupBy;
    
    // Display first message
    this.hideAllMessages();
    this.showMessage();
    // Install timer
    this.timer = this.showMessage.periodical(this.interval,this);
  },
 
  showMessage: function() {
    for(var i = 0; i < this.groupBy; i++) {
      if(this.start_message > this.number_of_messages) {
        this.start_message = 0;
      }
      this.group[i] = this.messages[this.start_message];
      this.start_message += 1;
    }
    
    this.group.each(function(item) {
      item.setStyle('display','block');
      item.tween('opacity',1);
    }.bind(this));
      
    this.fadeMessage.delay(this.interval-2000,this);
  },
 
  fadeMessage: function() {
    this.group.each(function(item) {
      item.setStyle('display','none');
      item.tween('opacity',0);
    }.bind(this));
  },
 
  hideAllMessages: function() { 
    this.messages.setStyles({
      'display':'none',
      'opacity':0
    });
  }
});