/*
Script: Ticker.js
	Contains <Ticker>

Author:
	Mahendra KS Patel <http://www.star-soft.co.uk/>

License:
	MIT-style license.

Class: Ticker
	Creates a scrolling ticker or scrolling marquee text

Arguments:
	el - the $(element) containing the text or any to be scrolled
	options - options for the NewsTicker.

Options:
	rightToLeft - Scroll from right to left
	speed - how first to scroll

Example:
	var ticker = new Ticker('ticker', { rightToLeft: false, speed: 1 });
	ticker.start.delay(1000, ticker); // Start after 1 second.
*/

var Ticker = new Class({
	getOptions: function() {
		return {
			rightToLeft: false,
			speed: 1
		};
	},

	initialize: function(el, options) {
		this.setOptions(this.getOptions(), options);

		this.paused = false;
		this.timer = null;
	

		this.container = $(el);
		this.container.css('display', 'block');

		var width = this.container.size().x;
		var content = this.container.innerHTML;

		var img = "<img src='/images/blank.gif' width='" + width + "' height='0' />";
this.container.innerHTML = "<div id='" + this.container.get('id') + "Body' width='100%'>" + img +content + img + "</div>"		
//		this.container.innerHTML = "<table cellspacing='0' cellpadding='0' width='100%'><tr><td nowrap='nowrap'>" + img + "<span id='" + this.container.get('id') + "Body' width='100%'>&nbsp;</span>" + img + "</td></tr></table>";		

		this.container.scrollLeft = 0;
	
//		$(this.container.get('id') + 'Body').innerHTML = content;
	},

	onMouseOver: function(event) {
		this.paused = true;
	},

	onMouseOut: function(event) {
		this.paused = false;
	},

	scroll: function() {
		if (!this.paused) this.container.scrollLeft += this.options.speed * (this.options.rightToLeft ? -1 : 1);
		if ((this.options.rightToLeft) && (this.container.scrollLeft <= 0)) this.container.scrollLeft = this.container.scrollWidth - this.container.offsetWidth;
		if ((!this.options.rightToLeft) && (this.container.scrollLeft >= this.container.scrollWidth - this.container.offsetWidth)) this.container.scrollLeft = 0;
	},

	start: function() {
		if (!this.timer) {
			this.timer = this.scroll.periodical(10, this);
			
			this.container.onmouseover = this.onMouseOver.bind(this);
			this.container.onmouseout = this.onMouseOut.bind(this);
		}
	},

	stop: function() {
		if (this.timer) {
			$clear(this.timer);
			this.timer = null;
			this.container.onmouseover = Class.empty();
			this.container.onmouseout = Class.empty();
		}
	}
});

Ticker.implement(new Options(), new Events);
