/*
 *	Author : Michael Kinkaid
 *	Ticker class created using MooTools v1.11
 */
 
	var Ticker = new Class({
			options:{
				content:"",
				startBtn:"",
				stopBtn:"",
				timer:0,
				url:""
			},
		
			initialize:function(options){
				this.setOptions(options),
				this.totalNewsItems = 0,
				this.newsItemId = 0,
				this.timer = this.options.timer,
				this.url = this.options.url,
				this.dummy = "",
				this.periodical = "",
				this.tkr_ajax = "",
				this.setupTicker()
			},
			
			setupTicker:function(){				
				if(this.options.startBtn != ""){
					this.tkr_start = $(this.options.startBtn);
					this.tkr_start.addEvent('click', this.startFeed.bind(this));
				}				
				
				if(this.options.stopBtn != ""){
					this.tkr_stop = $(this.options.stopBtn);
					this.tkr_stop.addEvent('click', this.stopFeed.bind(this));
				}
				
				if(this.options.content != ""){
					this.tkr = $(this.options.content);
					
					this.tkr_ajax = new Ajax(this.url, { 
						method: 'get',
						onComplete: this.addNewsItem.bind(this),
						onCancel: this.cancelFromAjax.bind(this)
					});					
					
					this.getTotalNewsItems();
				}				
			},
			
			addNewsItem:function(response){
				this.newsItemId++;
				if(this.newsItemId > this.totalNewsItems){
					this.newsItemId = 1;
				}
				//this.tkr.removeClass('ajax-loading'); 
				this.tkr.setHTML(response);
				var opacityChange = new Fx.Style(this.tkr, 'opacity', {duration:500});
				opacityChange.start(0, 1);				
			},
			
			cancelFromAjax:function(response){
				this.tkr.removeClass('ajax-loading');
			},
			
			getTotalNewsItems:function(){
				var initAjax = new Ajax(this.url, { 
					data: 'request=init',
					method: 'get',
					onComplete: this.setTotalNewsItems.bind(this)
				}).request();					
			},
			
			setTotalNewsItems:function(response){
				this.totalNewsItems = response;
				if(this.totalNewsItems >= 1){
					
					this.newsItemId = 1;
					this.periodical = this.refresh.periodical(this.timer * 1000, this);
					this.tkr_ajax.request('request=1');					
				}
				else{
					this.tkr.setHTML("<strong>No News</strong>");
				}				
			},
				
			startFeed:function(){
				this.stopFeed(); 
				$clear(this.periodical); 
				//this.tkr.empty().addClass('ajax-loading'); 
				this.periodical = this.refresh.periodical(this.timer * 1000, this); 
				this.tkr_ajax.request('request=' + this.newsItemId + '&' + $time()); 				
				
			},
			
			stopFeed:function(){
				$clear(this.periodical); 
				this.tkr_ajax.cancel(); 
			},
			
			refresh:function(){
				this.dummy = 'request=' + this.newsItemId + '&' + $time() + $random(0, 100);
				//this.tkr.empty().addClass('ajax-loading');
				
				
				var opacityChange = new Fx.Style(this.tkr, 'opacity', {duration:500,onComplete:this.callBeta.bind(this)});
				opacityChange.start(1, 0);				
				
				//this.tkr_ajax.request(this.dummy); 				
			},
			
			callBeta:function(){
				this.tkr_ajax.request(this.dummy); 	
			}
	});
		
	Ticker.implement(new Options, new Events);	