/*
@author: Thomas Kunambi, <http://www.kunambi.com>
@description: Simple MooTicker
@license: MIT License
@copyright: Copyright (c) 2009 Thomas Kunambi
@arguments: Parameters - see Parameters below
@parameters:
	container: (string||object) reference to div dom element container | required
	autostart: (boolean) wether the scrolling of the ticker should autostart or not | default: true
	animation: (string) wether the ticker text should "scroll" or "fade" | default: "scroll"
	steps: (integer) how many pixels to the left should the ticker take each iteration when "animation: scroll" is chosen | default: 5 pixels
	duration: (integer) amount of miliseconds between each iteration | default 100 miliseconds
		NB! the duration should NOT be lower than 1000 when chosing "animation: fade"
@methods:
	animate(): perform the desired animation between each ticker message
	start(): starts animating the ticker
	stop(): halts animating the ticker
@requires: MooTools 1.2.3 core
*/

var SimpleMooTicker = new Class({
	Implements: Options,
	options: {
		autostart: true,
		animation: "scroll",
		step: 5,
		periodical: 100
	},
	initialize: function(options) {
		this.setOptions(options);
		this.container = document.id(this.options.container);
		if (this.container === null) {
			throw("DOM object not found");
		}
		this.timer = null;
		this.container.addEvents({
			"mouseover": function() { this.stop(); }.bind(this),
			"mouseout": function() { this.start(); }.bind(this)
		});
		if (this.options.animation == "fade") {
			this.container.addClass(this.options.animation);
			this.container.getElement("ul").setStyle("height", this.container.getElement("ul > li").getSize().y);
		}
		this.oLI = this.container.getElement("ul li:first-child");
		if (this.options.autostart) {
			this.start();
		}
	},
	animate: function() {
		switch(this.options.animation) {
			case "fade":
				new Fx.Tween(this.oLI, {
					onComplete: function() {
						this.oLI.getParent("ul").adopt(this.oLI.clone());
						this.oLI.dispose();
						this.oLI = this.container.getElement("ul li:first-child");
						new Fx.Tween(this.oLI).start("opacity", 1);
					}.bind(this)
				}).start("opacity", 0);
			break;
			default:
				if (this.oLI.getCoordinates().width <= Math.abs(this.oLI.getStyle("margin-left").toInt())) {
					this.oLI.getParent("ul").adopt(new Element("li", {"html": this.oLI.get("html")}));
					this.oLI.dispose();
					this.oLI = this.container.getElement("ul li:first-child");
				}
				this.oLI.setStyle("margin-left", this.oLI.getStyle("margin-left").toInt() - this.options.step);
			break;
		}
	},
	start: function() {
		this.timer = this.animate.periodical(this.options.periodical, this);
	},
	stop: function() {
		$clear(this.timer);
	}
});
