

function Player(opts) {
	this.playing = false;
	
	this.file = opts.file;
	this.onplay = opts.onplay;
	this.onstop = opts.onstop;
}


Player.jPlayer = function(cb) {
	if (!this._jPlayer) {
		var el = jQuery('<div id="__jplayer"></div>').appendTo(document.body).jPlayer({
			swfPath: Player.path,
			ready: function() {
				Player._jPlayer = this;
				cb(this);
			}
		});
	}
	else {
		cb(this._jPlayer);
	}
}

Player.play = function(file, complete) {
	this.jPlayer(function(jPlayer) {
		jPlayer.setFile(file);
		jPlayer.element.jPlayer('onSoundComplete',  complete || function() {});
		jPlayer.play();
	});
}

Player.stop = function() {
	this.jPlayer(function(jPlayer) {
		jPlayer.stop();
	});
}


Player.prototype = {
	play: function() {
		this.playing = true;
		this.onplay();
		
		var that = this;
		Player.play(this.file, function() {
			that.stop();
		});
	},
	
	stop: function() {
		this.playing = false;
		this.onstop();
		Player.stop();
	},
	
	toggle: function() {
		if (this.playing) {
			this.stop();
		}
		else {
			this.play();
		}
	}
};

