// News ticker
(function() {

//get the ticker
var ticker = document.getElementById("ticker");

window.Newsticker = {
	anim : null,
	paused : false,

	Presets : {
		TICKER_CONTENT : ticker.innerHTML, // get content
		TICKER_RIGHTTOLEFT : false,
		TICKER_SPEED : 80,
		TICKER_PAUSED : false,
		TICKER_WIDTH : 770
	},

	start: function () {
		var img = '<img src="/img/ticker_spacer.gif" width="' + this.Presets.TICKER_WIDTH + '" height="0">';
		ticker.innerHTML = '<div style="white-space: nowrap;">'+img+'<span id="ticker-body">'+this.Presets.TICKER_CONTENT+'</span>'+img+'</div>';
		ticker.scrollLeft = this.Presets.TICKER_RIGHTTOLEFT ? ticker.scrollWidth - ticker.offsetWidth : 0;
		
		YAHOO.util.Event.addListener('ticker-body', 'mouseover', this.pause, this, true);
		YAHOO.util.Event.addListener('ticker-body', 'mouseout', this.play, this, true);

		this.restart();
	},

	pause : function () {
		this.Presets.TICKER_PAUSED = true;

		this.animate(this.getScrollPosition() + 15, this.getScrollPosition(), 0.3, YAHOO.util.Easing.easeOut);
	},

	play : function () {
		this.animate(this.getScrollEnd(), this.getScrollPosition(), (this.getScrollEnd() - this.getScrollPosition()) / this.Presets.TICKER_SPEED, YAHOO.util.Easing.easeNone);

		this.Presets.TICKER_PAUSED = false;
	},

	restart : function () {
		if (this.Presets.TICKER_PAUSED)
		{
			return;
		}

		this.animate(this.getScrollEnd(), 0, this.getScrollEnd() / this.Presets.TICKER_SPEED, YAHOO.util.Easing.easeNone);
	},

	getScrollPosition : function () {
		return ticker.scrollLeft;
	},

	getScrollEnd : function () {
		return this.Presets.TICKER_WIDTH + this.getTickerBodyWidth();
	},

	getTickerBodyWidth : function () {
		var tickerBody = document.getElementById('ticker-body');

		return tickerBody.offsetWidth;
	},

	animate : function (to, from, time, ease) {
		if (null === this.anim) {
			this.anim = new YAHOO.util.Scroll('ticker');
			this.anim.onComplete.subscribe(this.onAnimationComplete);
		} else {
			this.anim.stop(false);
		}

		this.anim.attributes = {
			scroll: {
				to: [to, 0],
				from: [from, 0]
			}
		};
		
		this.anim.duration = time;
		this.anim.method = ease;

		this.anim.animate();
	},

	onAnimationComplete : function () {
		window.Newsticker.restart();
	}
};

window.Newsticker.start();

})();
