/*
	module name: lu-jw_player.js
	date: Friday, March 27, 2009
	version: 1.0 of wrapper, 4.3 of Flash
	author: Jay Dansand (jay.dansand@gmail.com) for Lawrence University ITS
	purpose:
		Provide a JavaScript wrapper to create instances of the JW Player Flash application.
	configuration:
		None.
	usage:
		Instantiate the player with:
			new JWPlayer(file, [width, [height, [options, [container]]]])
				file - required. String with URL (relative or absolute) to a playlist or media file.
				width - optional. Integer pixel width for display area.
				height - optional. Integer pixel height for display area.
				options - optional.  Key:value array of param values to pass to the Flash applet (see Flash's documentation)
				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):
			.ready() - Boolean readystate of the Flash applet.
			.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.
			.getOptions() - Array of options passed to Flash applet.
			.Version - String version of this Flash/wrapper.
			
			Flash controls (return True if no error, False on error):
			Play() - Start playback.
			Stop() - Stop playback.
			Next() - Go to next file in the playlist.
			Prev() - Go to previous file in the playlist.
			setTrack(#) - Go to track # in the playlist.
			setPosition(s) - Go to second s in the playlist.
			setVolume(%) - Set playback volume to % (0 to 100).
			setFullscreen(bool) - Set Fullscreen mode true/false.
			setMute(bool) - Set Mute true/false.
			toggleMute() - Toggle the Mute state of the playback audio.
		
		During execution, the global array jw_instances[] is populated with references to
		registered JWPlayer instances.
*/

var jw_instances = new Array();

function JWPlayer(file, width, height, options, container)
{
	var __path = __scripts['jw player']['path'];
	var __expressInstaller = __scripts['swfobject']['path'] + "/expressInstall.swf";
	var __width = (!isNaN(parseInt(width))) ? parseInt(width) : 400;
	var __height = (!isNaN(parseInt(height))) ? parseInt(height) : 280;
	var __ready = false;
	var __self = this;
	this.ready = function (ready) { if (ready) __ready = ready; return __ready; };
	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; };
	var __container = createContainer(container, "JWPlayer").id;
	var __options = (options) ? options : new Array();
	__options.file = (file) ? file : "";
	//Force some options:
	__options.config = ""; //don't allow a circumvention of these defaults
	__options.autostart = "false";
	__options.id = __container;
	//
	this.getOptions = function () { return __options; };
	this.getContainer = function () { return document.getElementById(__container); };
	this.getContainerID = function () { return __container; };
	this.create(); //Instantiate the player
}

JWPlayer.prototype = {
	Version: '4.3',
	create: function ()
	{
		swfobject.embedSWF(this.getPath() + "/player.swf", this.getContainerID(),
			this.getWidth(), this.getHeight(), "8.0.0", this.getExpressInstaller(), this.getOptions(), { allowfullscreen : true } );
		jw_instances[this.getContainerID()] = this.getSelf();
	},
	Play: function () { try { this.getContainer().sendEvent("PLAY", "true"); } catch(e) { return false; } return true; },
	Stop: function () { try { this.getContainer().sendEvent("STOP", "true"); } catch(e) { return false; } return true; },
	Next: function () { try { this.getContainer().sendEvent("NEXT"); } catch(e) { return false; } return true; },
	Prev: function () { try { this.getContainer().sendEvent("PREV"); } catch(e) { return false; } return true; },
	setTrack: function (id) { try { this.getContainer().sendEvent("ITEM", id);} catch(e) { return false; } return true; },
	setPosition: function (pos) { try { this.getContainer().sendEvent("SEEK", pos);} catch(e) { return false; } return true; },
	setVolume: function (vol) { try { this.getContainer().sendEvent("VOLUME", (vol < 1) ? vol * 100 : vol); } catch(e) { return false; } return true; },
	setFullscreen: function (fullscreen) { try { this.getContainer().sendEvent("FULLSCREEN", (fullscreen) ? "true" : "false"); } catch(e) { return false; } return true; },
	setMute: function (mute) { try { this.getContainer().sendEvent("MUTE", (mute) ? "true" : "false"); } catch(e) { return false; } return true; },
	toggleMute: function () { try { this.getContainer().sendEvent("MUTE", (this.getContainer().getConfig()['mute']) ? "false" : "true"); } catch(e) { return false; } return true; }
};

//Called by the Flash application on ready:
function playerReady(obj) { jw_instances[obj['id']].ready(true); }