

/*
*	Børge Warvik - bowarv at gmail dot com
*	Created: 2007-10-08
*
*	Handles sponsor ticker.
*
*/

var Ticker = Class.create();

Ticker.prototype = {
	initialize: function(el, options)
	{
		this.el = $(el);
		this.elQuote = this.el.getElementsByClassName("ticker-quote")[0];
		this.elSign = this.el.getElementsByClassName("ticker-sign")[0];
		this.setOptions(options);
		this.currentQuote = "";
		this.currentSign = "";
		
		var url = "ajax.asp?section=ticker";
		var opt = { method: "POST", onSuccess: (this.success).bind(this), onFailure: (this.failure).bind(this) };
		var request = new Ajax.Request( url, opt );
	},
	
	setOptions: function(options) 
	{
		this.options = {
			fadeDuration: 0.5,
			showtime: 5000,
			tickerIndex: 0,
			useFadeOut: false,
			writespeed: 10
		};
		Object.extend( this.options, options || {} );
	},
	
	success: function(transport)
	{
		this.tickers = eval(transport.responseText);
		this.printTicker();
	},
	
	failure: function(o)
	{
		console.log("Ticker error:");
		console.log(o);
	},
	
	fadeOut: function()
	{
		if( this.options.useFadeOut && !this.isFadedOut)
		{
			this.isFadedOut = true;
			new Effect.Fade( this.el, { duration: this.options.fadeDuration, from: this.visibleFadeFrom, to: 0, afterFinish: (function() { this.printTicker(); } ).bind(this) } );
		}
		else
			this.printTicker();
	},
	
	printTicker: function()
	{
		var quote = this.tickers[this.options.tickerIndex].quote;
		var sign = this.tickers[this.options.tickerIndex].sign;
		
		if( this.currentQuote.length === quote.length && this.currentSign.length === sign.length)
		{
			this.currentQuote = "";
			this.currentSign = "";
			this.options.tickerIndex++;
			
			if( this.options.tickerIndex == this.tickers.length-1 )
				this.options.tickerIndex = 0;
				
			setTimeout( ( function() { this.fadeOut(); } ).bind(this), this.options.showtime);
		}
		else if( this.currentQuote.length < quote.length && this.currentSign.length === 0 )
		{
			this.elSign.innerHTML = "";
			var n = this.currentQuote.length === 0 ? 1 : this.currentQuote.length + 1;
			this.currentQuote = this.tickers[this.options.tickerIndex].quote.substr(0, n);
			this.elQuote.innerHTML = this.currentQuote;
			setTimeout( ( function() { this.printTicker(); } ).bind(this), this.options.writespeed);
		}
		else
		{
			var n = this.currentSign.length === 0 ? 1 : this.currentSign.length + 1;
			this.currentSign = this.tickers[this.options.tickerIndex].sign.substr(0, n);
			this.elSign.innerHTML = this.currentSign;
			setTimeout( ( function() { this.printTicker(); } ).bind(this), this.options.writespeed);
		}
	}
};

if(typeof TUIL=="undefined"){var TUIL={};}

TUIL.sponsorticker = function()
{
	return {
		init: function()
		{
			if( $("ticker") )
				new Ticker("ticker");
			
			if( $("tickersub") )
				new Ticker("tickersub");
		}
	}
}();

// Run script when window has loaded
Event.observe( window, 'load', TUIL.sponsorticker.init );


