﻿/*
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: 550,
			height: 30,
			charWidth: 12,
			paddingMultiplier: 0.6
		};
	},

	initialize: function(el, options) {
		this.setOptions(this.getOptions(), options);

		container = $(el); // news_ticker_element
		holder = $(el);
		
		theText = holder.cloneNode(true);
		so_clearInnerHTML(holder);
		
		function so_clearInnerHTML(obj) {
	        // so long as obj has children, remove them
	        while(obj.firstChild) obj.removeChild(obj.firstChild);
        }
		
		this.element = new Element('div').injectInside(el);
		this.element.appendChild(theText);
		this.element.setAttribute("id","news_ticker_element");
		this.element.firstChild.setAttribute("id","news_ticker_element_copy");
		
		container.setStyle('width', this.options.width+'px');
		container.setStyle('height', this.options.height+'px');
		container.setStyle('overflow', 'hidden');

		this.scrollSpeed = -1 * this.options.charWidth;

		this.tickerSize = this.element.innerHTML.length * this.options.paddingMultiplier;
		this.startOffset = 0; // controls the start position, this.element.getSize().size.x
		this.restartOffset = 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.restartOffset; //this.restartOffset;
			}
			this.element.setStyle('margin-left', this.currentOffset + 'px');
		}
	},

	start: function() {
		if (!this.timer) {
			this.timer = this.scroll.periodical(250, this); // controls the speed
			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());

