var Ticker = Class.create();
Ticker.prototype = {
	ticker: null,
	lines: null,
	
	lineOn: null,
	lineIndex: null,
	totalLines: null,
	
	fadingLine: null,
	appearingLine: null,
	
	PE: null,
	animatePE: null,
	fadeOutPE: null,
	fadeInPE: null,
	
    initialized : false,
    
    initialize : function(config) {
        if (!this.initialized) {
	        this.ticker = $$(config.tickerSelector)[0];
			this.lines = this.ticker.childElements();
			this.lines.invoke('hide');
          
		  	this.totalLines = this.ticker.childElements().length - 1;
			this.lineIndex = Math.round(Math.random() * this.totalLines);
			this.PE = new PeriodicalExecuter(this.tick.bind(this), config.interval);
			this.tick();
            this.initialized = true;
        }
    },
	
	tick : function() {
		if (this.lineOn) {
			this.startFadeOut(this.lineOn);
		}
		this.lineOn = this.lines[this.lineIndex];

		this.startFadeIn(this.lineOn);
		this.lineIndex = this.lineIndex < this.totalLines ? this.lineIndex += 1 : 0;
	},
	
	startFadeOut : function(line) {
		this.fadingLine = line;
		new Effect.Fade(this.fadingLine, {from: 1.0, to: 0, duration: 0.5});
	},

	startFadeIn : function(line) {
		this.ticker.setStyle({visibility: 'visible'});
		
		this.appearingLine = line;
		this.appearingLine.setStyle({left: 500 + 'px', opacity: 0});

		new Effect.Parallel([
			new Effect.Move(this.appearingLine, {sync: true, x: -500, mode: 'relative'}),
			new Effect.Fade(this.appearingLine, {sync: true, from: 0, to: 1}) 
		], { 
			duration: 1.5,
			delay: 0
		});
		
		this.appearingLine.show();
	}	
};

Event.observe(window, 'load', function() {
	var ticker = new Ticker({tickerSelector: '.whats_new ul', interval: 5});
});