// news ticker based on
// WebTicker by Mioplanet http://www.mioplanet.com
// but rewritten, by Michael Hodgins, to use the Mootools library.

window.addEvent('domready', function() {
	var NewsTicker = new Class({
		initialize: function(name) {
			this.ticker = $(name);
			if (this.ticker != null) {
				this.ticker.style.display = '';
				this.content = this.ticker.innerHTML;
				this.righttoleft = false;
				this.speed = 2;
				this.style = "font-family:Arial; font-size:12px; color:#000";
				this.paused = false;
				this.ticker.addEvent('mouseover', this.pause.bind(this));
				this.ticker.addEvent('mouseout', this.unpause.bind(this));
				this.start();
			}
		},
		start: function() {
			var tickerSupported = false;
			this.width = this.ticker.getWidth();
			var img = '<img src="ticker_space.gif" width="'+this.width+'" height="0"/>';
		
			// Firefox
			if (navigator.userAgent.indexOf("Firefox")!=-1 || navigator.userAgent.indexOf("Safari")!=-1) {
				this.ticker.innerHTML = '<table cellspacing="0" cellpadding="0" width="100%"><tr><td nowrap="nowrap">'+img+'<span style="'+this.style+'" id="ticker_body" width="100%">&nbsp;</span>'+img+'</td></tr></table>';
				tickerSupported = true;
			}
			// IE
			if (navigator.userAgent.indexOf("MSIE")!=-1 && navigator.userAgent.indexOf("Opera")==-1) {
				this.ticker.innerHTML = '<div nowrap="nowrap" style="width:100%;">'+img+'<span style="'+this.style+'" id="ticker_body" width="100%"></span>'+img+'</div>';
				tickerSupported = true;
			}
			if(!tickerSupported) {
				this.ticker.outerHTML = ""; 
			} else {
				this.ticker.scrollLeft = this.righttoleft ? this.ticker.scrollWidth - this.ticker.offsetWidth : 0;
				$('ticker_body').innerHTML = this.content;
				this.ticker.style.display="block";			
			}
			
			this.tick.bind(this).periodical(30);
		},
		tick: function() {
			if(!this.paused) { 
				this.ticker.scrollLeft += (this.speed * (this.righttoleft ? -1 : 1));
			}
			if(this.righttoleft && this.ticker.scrollLeft <= 0) {
				this.ticker.scrollLeft = this.ticker.scrollWidth - this.ticker.offsetWidth;
			}
			if(!this.righttoleft && this.ticker.scrollLeft >= this.ticker.scrollWidth - this.ticker.offsetWidth) {
//				debugger;
				this.ticker.scrollLeft = 0;
			}
		},
		pause: function() {
			this.paused = true;
		},
		unpause: function() {
			this.paused = false;
		}
	});
	var ticker = new NewsTicker('news_ticker');
});