var NoizeStats = new Class({
	Extends: Options,
	
	options: {
		// Default Options
		listen_count: null,
		broadcast_status: null,
		now_playing: null,
		playing_caption: null,
		refresh_ms: 5000
	},
	
	initialize: function(options) {
		this.setOptions(options);
		
		// if ($type(this.options.listen_count)!=='element') {
		// 	throw new Error('listener_count element now found.');
		// }
		// 
		// if ($type(this.options.broadcast_status)!=='element') {
		// 	throw new Error('broadcast_status element now found.');
		// }
		
		if ($type(this.options.url)!=='string') {
			throw new Error('url not defined.');
		}
		
		var that = this;
		this.requestor = new Request({
			url:this.options.url,
			onSuccess: that.updateStats.bind(that),
			onFailure: that.errorStats.bind(that)
		});
		
		if ( this.options.listen_count ){
			this.options.listen_count.set('tween', {duration:'long'});
		}
		
		(function(){
			that.startTimer(that);
		}).periodical(this.options.refresh_ms);
		
		that.startTimer(that);
	},
	
	updateStats: function(resp) {
		var new_stats = JSON.decode(resp);
		
		if ( this.options.listen_count ){
			if ( this.options.listen_count.get('html') != new_stats.listeners ){
				this.options.listen_count.tween('color', ['#fff','#C6822A']);
				this.options.listen_count.set('html',new_stats.listeners);
			}
		}
		if ( this.options.broadcast_status ){
			this.options.broadcast_status.set('html',(new_stats.is_live)?'Live Now!':'Archive');
		}
		this.options.playing_caption.set('html',(new_stats.is_live)?'&#187;&#187 NOW PLAYING LIVE &#171;&#171;':'NOW PLAYING...');
		
		var track = new_stats.track.replace(/\*\*\*LIVE NOW\*\*\*/,"").trim();
		if ( this.options.now_playing.get('html') != track ){
			this.options.now_playing.set('html', track);
		}
	},
	
	errorStats: function(resp) {
		console.log('Error!');
	},
	
	startTimer: function(that) {
		that.requestor.send();
	}
});