/*
Script: NewsTicker.js
	Contains <NewsTicker>

Author:
	Nimrod A. Abing <http://abing.gotdns.com/>

License:
	MIT-style license.
*/
/*
Class: NewsTicker
	Creates a scrolling news ticker or scrolling marquee text (remember the Netscape MARQUEE tag?).

Arguments:
	el - the $(element) containing the text to be scrolled. Note that this element will be used to contain the actual scrolling div. It will be resized according to the width and height options and its overflow CSS style will be set to 'hidden'.
	options - options for the NewsTicker.

Options:
	width - the width in px that $(element) will be resized to.
	height - the height in px that $(element) will be resized to.
	charWidth - approximate width of 1em in pixels for the current font-size.
	paddingMultiplier - multiplies the length of the text inside the $(element) so that we create enough padding to scroll.

Example:
	>var ticker = new NewsTicker('news_ticker_element', { width: 600, height: 12, charWidth: 10, paddingMultiplier: 8 });
	>ticker.start.delay(1000, ticker); // Start after 1 second.
*/
NewsTicker = new Class({
	getOptions: function() {
		return {
			width: 600,
			height: 12,
			charWidth: 11,
			paddingMultiplier: 8
		};
	},

	initialize: function(el, options) {
		this.setOptions(this.getOptions(), options);

		container = $(el);
		theText = container.innerHTML;
		container.innerHTML = '';
		this.element = new Element('div').injectInside(el);
		this.element.innerHTML = theText;
		container.setStyle('width', this.options.width+'px');
		container.setStyle('height', this.options.height+'px');
		container.setStyle('overflow', 'hidden');

		this.scrollSpeed = -1 * this.options.charWidth;

		if (0 == this.element.innerHTML.length) {
			this.element.innerHTML = 'No news items available . . . ';
			this.element.innerHTML += 'No news items available . . . ';
			this.element.innerHTML += 'No news items available . . . ';
			this.element.innerHTML += 'No news items available . . . ';
		}
		this.tickerSize = this.element.innerHTML.length * this.options.paddingMultiplier;

		this.startOffset = this.element.getSize().size.x;
		this.currentOffset = this.startOffset;
		this.element.setStyle('width', this.tickerSize + 'em');
		this.paused = false;
		this.timer = null;
	},

	onMouseOver: function(event) {
		this.paused = true;
	},

	onMouseOut: function(event) {
		this.paused = false;
	},

	scroll: function() {
		if (!this.paused) {
			this.currentOffset = this.currentOffset + this.scrollSpeed;

			if (this.currentOffset <= -this.tickerSize) {
				this.currentOffset = this.startOffset;
			}
			this.element.setStyle('margin-left', this.currentOffset + 'px');
		}
	},

	start: function() {
		if (!this.timer) {
			this.timer = this.scroll.periodical(250, this);
			this.element.onmouseover = this.onMouseOver.bindAsEventListener(this);
			this.element.onmouseout = this.onMouseOut.bindAsEventListener(this);
		}
	},

	stop: function() {
		if (this.timer) {
			$clear(this.timer);
			this.timer = null;
			this.element.onmouseover = Class.empty();
			this.element.onmouseout = Class.empty();
		}
	}
});

NewsTicker.implement(new Options());
