// Newsticker class
// Original copyright 2006 Wolfgang Bartelme, Bartelme Design - [url=http://bartelme.at]http://bartelme.at[/url]
//
// Ported and edited for mootools by Huug Helmink, Ace Group bv - [url=http://www.acegroup.nl]http://www.acegroup.nl[/url]
// Modified by Matt Walker, Dotted Eyes Ltd - [url=http://www.dottedeyes.com]http://www.dottedeyes.com[/url]
//
// version 1.0
// date 2007-07-03
//
// Usage:
// var myTicker = new Ticker('idOfDivElement');
//     or (with options)
// var myTicker = new Ticker('idOfDivElement',{toggleButton:'idOfToggleDiv',interval:####});
// with #### as an integer > 2000

var Ticker = new Class({
  // Define options
  options: {
    toggleButton: false,
    interval: 5000
  },
 
  initialize: function(containerId, options) {
    // Declare variables
    var Appear,myFade,myBlinder;
 
    // Set container div
    this.container = $(containerId);
    // Set options
    this.setOptions(options);
    this.interval = this.options.interval;
    this.toggleButton = $(this.options.toggleButton);

    this.messages = this.container.getElements('li');
    this.number_of_messages = this.messages.length;
    if (this.number_of_messages == 0) {
      this.showError();
      return false;
    }
    this.current_message = 0;
    this.previous_message = null;
 
    // Create toggle button when ID is supplied with options
    if (this.toggleButton) {
      this.toggle_button = new Element('a').setProperties({
        'href': '#',
        'id': 'togglenewsticker'
      }).setHTML('Toggle').addEvent('click',this.toggleTicker.bind(this));
 
      this.toggleButton.adopt(this.toggle_button);
    }
		
		// Set the height of the ticker to the max height required to
		// display the tallest message
		var containerHeight = 0;
		this.messages.each(function(message) {
			var messageHeight = 0;
			var messageSize = message.getSize();
			messageHeight += messageSize.size.y;
			messageHeight += message.getStyle("margin-top").toInt();
			messageHeight += message.getStyle("margin-top").toInt();
      if (messageHeight > containerHeight) {
      	containerHeight = messageHeight;
      }
    })
    this.container.setStyle("height", containerHeight+'px');

    // Display first message
    this.hideMessages();
    this.showMessage();
    // Install timer
    this.timer = this.showMessage.periodical(this.interval,this);
  },
 
  showMessage: function() {
    Appear = new Fx.Style(this.messages[this.current_message],'opacity',{onStart:function(item) {
      item.setStyle('display','block');
    }}).start(0,1);
    this.fadeMessage.delay(this.interval-2000,this);
    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;
    }
  },
 
  fadeMessage: function() {
    myFade = new Fx.Style(this.messages[this.previous_message],'opacity',{onComplete:function(item) {
      item.setStyle('display','none');
    }}).start(1,0);
  },
 
  hideMessages: function() { 
    this.messages.each(function(message) { 
      message.setStyles({
        'display': 'none',
        'opacity': 0
      });
    })
  },
 
  toggleTicker: function() {
    myBlinder = new Fx.Slide(this.container,{duration:1000});
  },
 
  // Display error message when there is no list, or the list is empty
  showError: function() {
    if ($ES('ul',this.container).length == 0) {
      this.list = new Element('ul');
      this.container.adopt(this.list);
    } else {
      this.list = $E('ul',this.container);
    }
    this.errorMessage = new Element('li',{
      'class': 'error'
    }).setHTML('Could not retrieve data');
    this.list.adopt(this.errorMessage);
  }
});
Ticker.implement(new Options);