/*
	module name: lu-audio-player.js
	date: Friday, March 27, 2009
	version: 1.0 of wrapper, 1.2.3 of Flash
	author: Jay Dansand (jay.dansand@gmail.com) for Lawrence University ITS
	revisions:
		04/07/2009 by JJD - Added width and height optional parameters
	purpose:
		Provide a JavaScript wrapper to create instances of the Audio Player Flash application.
	configuration:
		None.
	usage:
		Instantiate the player with:
			new AudioPlayer(mp3, [loop, [width, [height, [container]]]]])
				mp3 - required. String with URL (relative or absolute) to an MP3 audio file.
				loop - optional, default false.  Boolean whether or not to loop at end of playback.
				width - optional. Integer pixel width for display area.
				height - optional. Integer pixel height for display area.
				container - optional.  String ID, Number Index, or Element reference to the destination for the Flash applet.
					NOTE: The element referenced, if pre-existing, will be replaced by the proper OBJECT/EMBED element.
						If it does not exist, it will be created.
		
		Object provides (read-only functions/variables):
			.getSelf() - Object self reference.
			.getWidth() - Number width of applet.
			.getHeight() - Number height of applet.
			.getPath() - String path to base of JavaScript/Flash files.
			.getExpressInstaller() - String path to Adobe ExpressInstall applet.
			.getContainer() - Object reference to Flash object/embed.
			.getContainerID() - String ID of Flash object.
			.getLoop() - Boolean whether or not playback will restart after end.
			.Version - String version of this Flash/wrapper.
			
			Flash controls (return True if no error, False on error):
			Play() - Start playback.
			Stop() - Stop playback.
		
		During execution, the global array ap_instances[] is populated with references to
		registered AudioPlayer instances.
*/

var ap_instances = new Array();

function AudioPlayer(mp3, loop, width, height, container)
{
	var __path = __scripts['audio-player']['path'];
	var __expressInstaller = __scripts['swfobject']['path'] + "/expressInstall.swf";
	var __loop = (true == loop) ? "yes" : "no";
	var __width = (!isNaN(parseInt(width))) ? parseInt(width) : 247;
	var __height = (!isNaN(parseInt(height))) ? parseInt(height) : 24;
	var __self = this;
	this.playing = function () { return this.getContainer().GetVariable("closePlayer") == 0; };
	this.getSelf = function () { return __self; };
	this.getWidth = function () { return __width; };
	this.getHeight = function () { return __height; };
	this.getPath = function () { return __path; };
	this.getExpressInstaller = function () { return __expressInstaller; };
	this.getLoop = function () { return __loop; };
	var __container = createContainer(container, "AudioPlayer").id;
	this.getContainer = function () { return document.getElementById(__container); };
	this.getContainerID = function () { return __container; };
	this.create(mp3); //Instantiate the player
}

AudioPlayer.prototype = {
	Version: '1.2.3',
	create: function (mp3)
	{
		swfobject.embedSWF(this.getPath() + "/player.swf", this.getContainerID(),
			this.getWidth(), this.getHeight(), "8.0.0", this.getExpressInstaller(),
			{ soundFile: (mp3) ? mp3 : "", playerID: "'" + this.getContainerID() + "'", loop: this.getLoop(), autostart: "no" },
			{ quality: "high", menu: "false", wmode: "transparent" });
		ap_instances[this.getContainerID()] = this.getSelf();
	},
	Stop: function () { try { this.getContainer().SetVariable("closePlayer", 1); } catch(e) { return false; } return true; },
	Play: function () {
		try
		{
			for (var i in ap_instances) if (i != this.getContainerID()) ap_instances[i].Stop();
			this.getContainer().SetVariable("closePlayer", 0);
			this.getContainer().Play();
		} catch(e) { return false; } return true;
	}
};

//This function is called automatically by the Flash object on Play, so it must be left in for compatibility reasons
function ap_stopAll(playerID) { ap_instances[playerID].Play(); }
