/*
	eclispe-creative.com 
	20090329 tomc
	
	DISPLAY NEWS TICKER
*/

var ticker = new Class({

	Implements: [Options],
	
	options:{
		element:null,
		newsData:null
	},
	uInt:null,
	uChar:0,
	uTick:0,
	cursors:[ '\\', '|' , '/', '-' ],
	uCur:0,
	tmp:'',
	
	
	initialize: function(options){
		this.setOptions(options);
		this.options.element.ticker = this;
		this.startAni();
	},
	
	
	startAni:function(){
		this.uChar = 0;
		this.tmp = '';
			
		if( this.uTick >= this.options.newsData.length)
			this.uTick = 0;
		
		this.options.element.set('tween', {onComplete:function(){}});
		this.options.element.tween('opacity',1);
		
		this.uInt = this.update.periodical(50, this);
	},
	
	endAni:function(){
		this.options.element.set('tween',{  onComplete:function(e){
				e.setProperty('html','');
				e.ticker.startAni();
			}});
		this.options.element.tween('opacity',0);
		
		++this.uTick;	
	},
	
	update:function(){
		
		var output = '';
		var data = this.options.newsData[ this.uTick ].ticker;
		
		if( this.uCur >= this.cursors.length  )
			this.uCur = 0;
		
		if( this.uChar >= data.length ){
			$clear(this.uInt);
			this.endAni.delay( 1000, this );
			output = this.tmp;
		}
		else
		{
			this.tmp += data.charAt( this.uChar );
			output = this.tmp + this.cursors[this.uCur];
		}
		
		this.options.element.setProperty( 'html', output );
		
		++this.uCur;
		++this.uChar;
	}
	
	
});


/*
	ENTRY
*/
window.addEvent('domready', function(){
	/*
		REQUEST NEWS TICKER DATA
		and start the animation
	*/
	new Request.JSON({
		url:'../js/ticker.json',
		onComplete: function(data,txt){
			if( data && data.length ){
				new ticker({
					element:$('newsticker'),
					newsData:data
				});
			}
		}
	}).send();
});