window.addEvent('domready',function(){
var i = 0;

new Element('img',{'src':'artwork.png','alt':'Artwork'}).inject($('artwork'));

Playlist.player = $('player');

Playlist.controls = new Element('ul', {'id':'controls'}).inject(Playlist.player,'top');
Playlist.playEl = new Element('li',{'class':'control', 'id':'play'}).inject(Playlist.controls,'bottom');
Playlist.stopEl = new Element('li',{'class':'control', 'id':'stop'}).inject(Playlist.controls,'bottom');
Playlist.stopEl.addEvent('click', function(e) { 
	e.stop();
	Playlist.stopSounds();
	Playlist.playing.each( function(sound) { sound.playlistEntry.setStyle('font-weight','normal'); });
}.bind(this));
Playlist.playEl.addEvent('click', function(e) { 
	e.stop();
	var playingSound = Playlist.playRandom(); 
	playingSound.playlistEntry.setStyle('font-weight','bold');
}.bind(this));

Playlist.seekbar = new Element('li',{'class':'seekbar'}).inject(Playlist.controls);
Playlist.position = new Element('span',{'class':'position'}).inject(Playlist.seekbar);

Playlist.list = new Element('ol',{'id':'playlist'}).inject(Playlist.player);

var options = {
	'onRegister': function() {
		i++;
		this.playlistEntry = new Element('li',{'class':'playlistEntry'}).addEvent('click', function(e){
			e.stop();
			Playlist.stopSounds();
			Playlist.playing.each( function(sound) { sound.playlistEntry.setStyle('font-weight','normal'); });
			this.start(0);
			this.playlistEntry.setStyle('font-weight','bold');
		}.bind(this)).inject(Playlist.list);
		this.tracklength = new Element('span',{'id':'tracklength'}).inject(this.playlistEntry);
		this.number = new Element('span',{text:i+'.\t'}).inject(this.playlistEntry);
		this.artist = new Element('span',{'id':'artist'}).inject(this.playlistEntry);
		this.title = new Element('span',{'id':'title',text:this.url}).inject(this.playlistEntry);
		this.playlistEntry.set('morph',{duration: 'long', transition: Fx.Transitions.Sine.easeOut});

		this.playlistEntry.addEvent('mouseenter', function(e){
			e.stop();
			this.morph({'background-color':'#aea537'}); 
		});
		this.playlistEntry.addEvent('mouseleave', function(e){
			e.stop();
			this.morph({'background-color':'#000000'});
		});
	},
	'onLoad': function() {
		var temp = this.getDuration();
		var minutes = Math.floor(temp/1000/60);
		var seconds = Math.floor((temp-minutes*60000)/1000);
		if (seconds<10) {
		  this.tracklength.set('text',minutes+':0'+seconds); 
		} else {
		  this.tracklength.set('text',minutes+':'+seconds); 
		}
	},
	'onPause': function() { },
	'onPlay': function() {
		this.playlistEntry.addClass('playing');
	 	Playlist.seekbar.addEvent('click', function(e) {
			e.stop();
			var coords = Playlist.seekbar.getCoordinates();
			var ms = ((e.page.x - coords.left)/coords.width)*this.duration;
			this.jumpTo(ms);
		}.bind(this));
		Playlist.seekbar.set('tween', {duration:this.options.progressInterval, unit:'%', link: 'cancel'});
		Playlist.position.set('tween', {duration:this.options.positionInterval, unit:'%', link: 'cancel'});
	},
	'onStop': function() { this.playlistEntry.removeClass('playing'); },
	'onProgress': function(loaded, total) {
		var percent = (loaded / total*100).round(2);
		Playlist.seekbar.get('tween').start('width', percent*4.71);
	},
	'onPosition': function(position,duration) {
		var percent = (position/duration*100).round(2);
		Playlist.position.get('tween').start('left', percent);
	},
	'onID3': function(key, value) {
		if (key== "TPE1") { this.artist.set('text', value+' - '); }
		if (key == "TIT2") { this.title.set('text', value); }
		//if (key == "TLEN") {this.duration.set('text', value);}
	},
	'onComplete': function() {
		Playlist.playRandom.delay(100, Playlist);
	},
	'streaming': false
};

	Playlist.loadSounds(["01.mp3","02.mp3"],options);
});
