/**
 * @author adrian
 */
window.addEvent('domready', function() {

    var i = 0;
    var options = {
        'onRegister': function() {
            i++;
            this.el = new Element('div', {'class':'song'});
            //this.title        = new Element('h3', {'class':'title', text:this.url}).inject(this.el);
            this.controls     = new Element('li', {'class':'controls'}).inject(this.el);
            this.seekbar      = new Element('li', {'class': 'seekbar'}).inject(this.el);
            this.position     = new Element('span', {'class':'position'}).inject(this.seekbar);
            this.seekbar.set('tween', {duration:this.options.progressInterval, unit:'%', link: 'cancel'});
            this.position.set('tween', {duration:this.options.positionInterval, unit:'%', link: 'cancel'});
            this.playEl       = new Element('img', {'class':'play',  src:'img/play.png',id:'play'+i }).inject(this.controls);
            this.stopEl       = new Element('img', {'class':'stop',  src:'img/stop.png',id:'stop'+i }).inject(this.controls);
            this.pauseEl      = new Element('img', {'class':'pause', src:'img/pause.png',id:'pause'+i}).inject(this.controls);
            this.stopEl.addEvent('click', function() { this.stop(); }.bind(this));
            this.playEl.addEvent('click', function() { this.start(); }.bind(this));
            this.pauseEl.addEvent('click', function() { this.pause(); }.bind(this));
            this.seekbar.addEvent('click', function(e) {
                var coords = this.seekbar.getCoordinates();
                var ms = ((e.page.x - coords.left)/coords.width)*this.duration;
                this.jumpTo(ms);
            }.bind(this));
            this.el.inject($('playlist'));
			this.start();
        },
        'onLoad': function() {   },
        'onPause': function() { },
        'onPlay': function() { this.el.addClass('playing');    },
        'onStop': function() { this.el.removeClass('playing'); },
        'onProgress': function(loaded, total) {
            var percent = (loaded / total*100).round(2);
            this.seekbar.get('tween').start('width', percent * .65);
        },
        'onPosition': function(position,duration) {
            var percent = (position/duration*100).round(2);
            this.position.get('tween').start('left', percent);
        },
        'onID3': function(key, value) {
            //if (key == "TIT2") { this.title.set('text', value); }
        },
        'onComplete': function() {
            Playlist.playRandom.delay(0, Playlist);
        }
		
    };

    //Note: I have some funky file serving going on in my widgets app, which means that
    //the filesize isn't readily accessible.  So it bugs out a bit in the official demo.
    //You can try it at home for better luck.

    var songs = ["mp3s/drifting.mp3"]; //I only have one mp3. ;_; (But you can add more!)

    Playlist.loadSounds(songs, options);
    //$('play').addEvent('click', function(e) {
    //    e.stop();
    //    Playlist.playRandom(); //Hm, I wonder what it will end up being.
    //});

});


