var Bandolier = new Class({
	initialize: function(ul,options) {
		Cutter_exlog = function(msg) { console.info(msg); };
		Cutter_loadStarted = function(song_key) { this.Cutter_loadStarted(song_key); }.bind(this);
		Cutter_loadFinished = function(song_key,true_length) { this.Cutter_loadFinished(song_key,true_length); }.bind(this);
		Cutter_loadProgress = function(song_key,bytes_loaded,bytes_total) { this.Cutter_loadProgress(song_key,bytes_loaded,bytes_total); }.bind(this);
		Cutter_finishedPlaying = function(song_key) { this.Cutter_finishedPlaying(song_key); }.bind(this);
		Cutter_errorLoading = function(requested_key) { this.Cutter_errorLoading(requested_key); }.bind(this);

		if($('cutter') && $(ul)) {
			this.cutter = $('cutter');
			this.loadStripes(ul);
			this.original_title = document.title;
			this.clockUpdate.periodical(100,this);

			document.addEvent('keydown',function(event) {
				if(event.key == 'enter') {
					this.stripeToggle(this.now_active ? this.now_active : this.all_keys[0]);
				}
				if(event.key == 'left') {
					this.stripeToggle(this.previousKey());
				}
				if(event.key == 'right') {
					this.stripeToggle(this.nextKey());
				}
			}.bind(this));
			
			this.true_time = {};
			this.became_active = {};
			
			var anchor_key = location.href.split('#')[1];
			if(anchor_key) {
				this.stripeToggle(anchor_key);
			}
		}
	},
	loadStripes: function(ul) {
		this.stripes = {};
		this.all_keys = [];
		$(ul).getElements('li').each(function(li) {
			if(!li.hasClass('strike')) {
				var song_key = li.getProperty('id').substring(6);
				this.all_keys.push(song_key);
				this.stripes[song_key] = new Stripe(song_key,this);
			}
		}.bind(this));
	},
	stripeToggle: function(song_key) {
		if(song_key) {
			if(this.now_active == song_key) {
				if(this.cutter.hasRequestCompleted()) {				
					if(this.cutter.playingNow()) {
						this.pause();
					} else {
						this.play();
					}
				}
			} else {
				$clear(this.retry_timer);
				this.retries = 0;
				if(this.cutter.playingNow()) {
					this.pause();
				}
				if(this.now_active) {
					this.stripes[this.now_active].shutdown();
				}
				this.cutter.loadFromKey(song_key);
				this.nowplaying = false;
				this.setNowActive(song_key);
				this.stripes[this.now_active].setActivated(true);
				this.stripes[this.now_active].setLoading(true);			
			}
		}
	},
	reconnect: function(key) {
		var s = this.stripes[key];
		document.title = s.getArtist() + ' - ' + s.getTitle() + ' / ' + this.original_title;
		this.nowplaying = true;
		s.setActivated(true);
		this.now_active = key;
	},
	setNowActive: function(song_key) {
		this.now_active = song_key;
		if(song_key) {
			var now = new Date();
			this.became_active[song_key] = Math.floor(now.getTime()/1000);
		} else {
			this.now_active = false;
		}
	},
	clockUpdate: function() {
		if(this.now_active) {
			if(this.now_active === this.cutter.getCurrentKey()) {
				var seconds = Math.floor(this.cutter.getTimeElapsed() / 1000);
				this.stripes[this.now_active].setClock(seconds);
				this.stripes[this.now_active].setPlaying(this.cutter.playingNow());		
			}
		}
	},
	play: function() {
		this.cutter.soundStart();
		var s = this.stripes[this.now_active];
		document.title = s.getArtist() + ' - ' + s.getTitle() + ' / ' + this.original_title;
	},
	pause: function() {
		this.cutter.soundStop();
		document.title = this.original_title;
	},
	nextKey: function() {
		var index = this.all_keys.indexOf(this.now_active);
		return (index < this.all_keys .length - 1 ? this.all_keys[index+1] : false);
	},
	previousKey: function() {
		var index = this.all_keys.indexOf(this.now_active);
		return (index > 0 ? this.all_keys[index-1] : false);		
	},
	Cutter_loadStarted: function(song_key) {
		this.play();
	},
	Cutter_loadFinished: function(song_key,true_length) {
		if(this.now_active == song_key) {
			this.true_time[song_key] = Math.floor(true_length / 1000);
		}
	},
	Cutter_loadProgress: function(song_key,bytes_loaded,bytes_total) {
		if(this.now_active == song_key) {
			$clear(this.retry_timer);
			var percent = Math.ceil(bytes_loaded/bytes_total*100);
			this.stripes[song_key].setPercentage(percent);
		}
	},
	Cutter_finishedPlaying: function(song_key) {
		this.pause();
		this.stripes[this.now_active].shutdown();
		var next_up = this.nextKey();
		if(next_up) {
			this.stripeToggle(next_up);
		} else {
			this.setNowActive(false);
		}
	},
	Cutter_errorLoading: function(requested_key) {
		if(this.now_active == requested_key) {
			this.stripes[this.now_active].setPercentage('retrying '+(this.retries+1)+'&hellip;');
			this.retry_timer = function() {
				if(this.retries < 2) {
					this.retries++;
					this.cutter.loadFromKey(requested_key);					
				} else {
					this.stripes[this.now_active].setPercentage('ERROR: could not acquire stream :(');
				}
			}.delay(3000,this);
		}
	}
});