﻿/*
Class: MooTicker
	Mootools implementation of the old ticker.js. Allows for mulitple tickers on a page and is styled using CSS
	
Authors
	Ryan Marsh <ryan@fsite.com>
*/
var MooTicker = new Class({
	Implements: [Options],

	content: null,
	cps: 2,
	aw: 0,
	el: null,
	container: null,
	slider: null,
	contentWrapper: null,

	options: {
		className: null,
		pauseOnMouseOver: true,
		speed: 2
	},

	initialize: function(el, options) {
		this.setOptions(options);
		this.el = $(el);

		if (!$defined(this.el)) return;

		this.content = this.el.get('html'); this.el.set('html', '');
		this.cps = this.options.speed;

		this.container = new Element('div', {
			"class": this.options.className,
			"styles": { "position": "relative", "overflow": "hidden" }
		});

		this.slider = new Element('div', {
			"class": "slider",
			"styles": {
				"position": "absolute",
				"left": "0px",
				"top": "0px",
				"white-space": "nowrap"
			}
		});

		this.container.inject(this.el);
		this.slider.inject(this.container);

		this.contentWrapper = new Element('span');
		this.contentWrapper.set('html', this.content);

		this.contentWrapper.inject(this.slider);

		if (this.options.pauseOnMouseOver) {
			this.container.addEvent('mouseover', this.container$mouseover.bind(this));
			this.container.addEvent('mouseout', this.container$mouseout.bind(this));
		}

		this.slider.style.left = (parseInt(this.container.getSize().x) + 10) + "px";
		this.aw = this.contentWrapper.offsetWidth;
		window.setInterval(this.scroll.bind(this), 50);
	},

	container$mouseover: function() {
		this.cps = 0;
	},

	container$mouseout: function() {
		this.cps = this.options.speed;
	},

	scroll: function() {
		this.slider.style.left = (parseInt(this.slider.style.left) > (-10 - this.aw))
			? parseInt(this.slider.style.left) - this.cps + "px"
			: parseInt(this.container.getSize().x) + 10 + "px";
	}
}); 