/** mooTicker - Newsticker class
 *  Original copyright 2006 Wolfgang Bartelme, Bartelme Design - http://bartelme.at
 *
 *  Ported and edited for mootools by Huug Helmink
 *  version 0.3
 *  date 2009-09-10
 *
 *  Edited to work with mootools 1.3.2 by Bart Baaten
 *  2011-12-23
 *
 *  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: {
    interval: 5000
  },

  initialize: function(containerId,options) {
    // Set options
    this.setOptions(options);
    // Set container div
    this.container = $(containerId);

    this.interval = this.options.interval;

    this.messages = $(this.container).getElements('li');
    this.number_of_messages = this.messages.length;

    this.current_message = 0;
    this.previous_message = null;

    // Display first message
    this.showMessage();
    // Install timer
    this.timer = this.showMessage.periodical(this.interval,this);
  },

  showMessage: function() {

    var cm = this.current_message;
    var msgs = this.messages;

  	var content_element = this.container.getElement('.newsticker_content');
    var showHide = new Fx.Morph(content_element, {
    	duration: 500,
    	transition: Fx.Transitions.Sine.easeOut,
    	onComplete: function() {
    		content_element.innerHTML = msgs[cm].innerHTML;
    		content_element.fade(1);
    	}
    });

		showHide.start({ 'opacity': [1,0] });

    if (this.current_message < this.number_of_messages-1) {
      this.previous_message = this.current_message++;
    } else {
      this.current_message  = 0;
      this.previous_message = this.number_of_messages - 1;
    }
  }
});
