
var Player = Base.extend({
	id: "",
	flashProxy: null,
	pan: 0.0,
	volume: 50,
	duration: 0,
	position: 0,
	isPlaying: false,
	isPaused: false,

	constructor: function() {
		this.id         = new Date().getTime();
		this.flashProxy = new FlashProxy(this.id, 'Flash/JavaScriptFlashGateway.swf');
	
		Player.playerList[this.id] = this;
	},

	render: function(width, height) {
		if (width == undefined) {
			width = 0;
		}

		if (height == undefined) {
			height = 0;
		}

	    var tag = new FlashTag('Flash/FlashStreamingPlayer7.swf', width, height);
	    tag.setFlashvars('lcId=' + this.id);
	    tag.write(document);
	},

	open: function(url) {
		this.flashProxy.call('open', url);
		this.isPlaying = true;

		// Force the volume to update shortly after playing.. this is a hack.
		var self = this;
		setTimeout(function() { self.setVolume(self.volume); }, 500);
	},

	play: function() {
		this.flashProxy.call('play');
		this.isPlaying = true;
		this.isPaused = false;
	},

	pause: function() {
		this.flashProxy.call('pause');
		this.isPlaying = false;
		this.isPaused = true;
	},

	stop: function() {
		this.flashProxy.call('stop');
		this.isPlaying = false;
		this.isPaused = false;
	},

	getVolume: function() {
		return this.volume;
	},

	setVolume: function(volume) {
		this.volume = volume;

		if (this.volume < 0) {
			this.volume = 0;
		}

		if (this.volume > Player.MaxVolume) {
			this.volume = Player.MaxVolume;
		}

		this.flashProxy.call('setVolume', this.volume);
	},

	getPan: function() {
		return this.pan;
	},

	setPan: function(pan) {
		this.pan = pan;
		this.flashProxy.call('setPan', this.pan);
	},

	updateDuration: function() {
		this.flashProxy.call('asyncGetDuration');
	},

	receiveDuration: function(duration) {
		this.duration = duration;
	},

	getDuration: function() {
		return this.duration;
	},

	updatePosition: function() {
		this.flashProxy.call('asyncGetPosition');
	},

	receivePosition: function(position) {
		this.position = position;
	},

	getPosition: function() {
		return this.position;
	},

	onPlay: function() { },

	onPause: function() { },

	onStop: function() { },

	onSoundComplete: function() { }
},
{
	playerList: new Array(),
	MaxVolume: 200
});

function playerHandler(id, func, args) {
	var player = Player.playerList[id];
	eval('player.' + func + '(args)');
}

