// Programmed by joep@joep-i.nl
// Feel free to use and modify this.

function Player(sound) {
    this.playing = false;
	this.sm = sound;
    this.sound = null;
    this.frequency = 200;
    this.song = '';
    this.id = '';
    this.timeleft = 5;
    this.position = 0;
    this.registerCallback();      
    this.time = 1;
}

Player.prototype.registerCallback = function() {
   setInterval(this.onTimerEvent.bind(this), this.frequency);
}

Player.prototype.play = function(song,id) {
	var start = 1;
    if (this.playing) {
        if (this.id == id) {
        	// stop the song if you click a second time
			start = 0;
        }
		this.onSoundComplete();
    }
	if (start) {
		// start to play a song
        this.id = id;
        this.song = song;
		if (this.song) {
			this.sm.createSound({id: this.id, url: this.song, autoPlay : true});
			this.sound = this.sm.getSoundById(this.id);

			this.playing = true;
			jQuery('#'+this.id).addClass('playing'); 
			if (this.time) { 
				jQuery('#'+this.id).append("<span id='time'></span>");
			}
		}

    }

    return false;
}

Player.prototype.onTimerEvent = function() {

    if (this.playing) {

        this.position = document.player.sound.position;
        this.duration = document.player.sound.duration;
		if (document.player.sound.isBuffering) {
			this.duration = document.player.sound.durationEstimate;
		}

        var progress = this.position/this.duration;
        this.timeleft = this.duration - this.position;

        var rest = (this.timeleft % 60000);
        var seconds = Math.round(rest/1000);
        var minutes = Math.round((this.timeleft - rest) / 60000);
        if (seconds == 60) {
            seconds = 0;
            minutes = minutes + 1;
        }
        if (!(isNaN(minutes)) && !(isNaN(seconds))) {
			if ((seconds < 10) && (seconds >= 0)) {
				seconds = "0" + seconds;
			}
			if (this.time) {
				jQuery('#time').html(minutes + ":" + seconds);
			}
            if (!(this.timeleft > 0) && (this.duration > 0)) {
                this.onSoundComplete();
            }
        }
        if (isNaN(progress)) {
            progress = 0
        }
        try {
            jQuery('#slider').slider('option', 'max',(this.duration * 1.05));
            jQuery('#slider').slider('value',this.position);
        } catch (e) { }
    }
}

Player.prototype.onSoundComplete = function() {
	if (this.time) {
		jQuery('#time').remove();
	}

    this.playing = false;
    this.sm.destroySound(this.id);
    jQuery('#' + this.id).removeClass('playing'); 
    this.sound = '';
    this.song = '';
    this.id = '';
}


jQuery.noConflict();
jQuery().ready(function(){
	soundManager.url = 'swf/soundmanager2.swf';
	soundManager.debugMode = false;
	soundManager.onload = function() {
		jQuery("a.autoplay").click();
	};
	this.player = new Player(soundManager);

	var count = 0;
	var mp3match = /mp3$/;
	jQuery('a').each(function() {
		if (this.href.match(mp3match)) {
			count += 1;
			var id = "mp3inline_" + count;
			jQuery(this).attr('id',id);
			jQuery(this).addClass("player");
			jQuery(this).click(function() {
				jQuery(this).blur();
				document.player.play(this.href,id);
				return false;
			});
		}
	});
});

