/***************************************
musicObject - Version 2.8.1
written by Chris van Rensburg

© Copyright 1996-1998 Headspace, Inc.
All  Rights Reserved
***************************************/

/*** customizable properties ***/

mo_enabled = true;

mo_requiredMinVersion = '';
mo_silentIfInadequate = false;
mo_showCompatibilityPrompt = true;
mo_ignoreJavaDisabled = false;

mo_debugToJavaConsole = true;
mo_debugToAlert = false;
mo_debugToStatus = true;
mo_ignoreNotReady = true;

/*** global properties & functions ***/

mo_hasBeatnik = false;
mo_BeatnikType = '';
mo_BeatnikVersion = '';
mo_hasCallbackIssue = false;
mo_windowOnloadStr = '';
mo_bodyEventsAdded = false;
mo_globalNoteNo = 0;
mo_minFadeInterval = 100;
mo_storedHandlers = 0;
mo_instances = new Array ();

function mo_debug (errorMessage,errorURL,errorLineNo) {
	var errorStr = '\n**** JAVASCRIPT ERROR ****\n    TYPE: ' + errorMessage + '\n    LINE# ' + errorLineNo + '\n    FILE: ' + errorURL + '\n';
	if (mo_debugToJavaConsole && typeof (java) != 'undefined') java.lang.System.out.println (errorStr);
	if (mo_debugToAlert) alert (errorStr);
	if (mo_debugToStatus) window.defaultStatus = errorStr;
	return true;
}

function mo_enableDebug (windowHandle) {
	windowHandle.onerror = mo_debug;
	for (var frameNo = 0; frameNo < windowHandle.frames.length; frameNo++)
		mo_enableDebug (windowHandle.frames [frameNo])
	;
}

mo_enableDebug (top);

function mo_addEventHandler (windowHandle,objectID,eventType,handler) {
	var storedHandlerStr = '';
	if (typeof (objectID [eventType]) == 'function') {
		if (typeof (windowHandle.mo_storedHandlers) == 'undefined') windowHandle.mo_storedHandlers = 0;
		windowHandle.mo_storedHandlers++;
		var storedHandler = 'mo_storedEventHandler' + mo_storedHandlers;
		windowHandle [storedHandler] = objectID [eventType];
		storedHandlerStr = '; return ' + storedHandler + ' (event)';
	}
	objectID [eventType] = windowHandle.eval ('new Function (\'event\',\'' + handler + storedHandlerStr + '\')');
}

function mo_retrieveVersion (appName) {
	var
		versionStr = '',
		numerals = '0123456789',
		charNo = appName.length - 1,
		currentChar,
		inVersion = false,
		parensLevel = 0
	;
	while (charNo >= 0) {
		currentChar = appName.charAt (charNo);
		if (currentChar == ')') {
			parensLevel++;
		} else if (currentChar == '(') {
			parensLevel--;
		} else if (parensLevel == 0) {
			if (inVersion || numerals.indexOf (currentChar) != -1) {
				inVersion = true;
				if (currentChar == ' ') charNo = 0;
					else if (currentChar == '.' || numerals.indexOf (currentChar) != -1) versionStr = currentChar + versionStr;
			}
		}
		charNo--;
	}
	return versionStr;
}

function mo_meetsMinVersion (versionToTest,minRequiredVersion) {
	var
		versionA = mo_retrieveVersion (versionToTest),
		versionB = mo_retrieveVersion (minRequiredVersion)
	;
	if (versionA.length < versionB.length) versionA += '.0.0.0.0.0.0.0.0.0.0.0.0'.substring (0,versionB.length - versionA.length);
	return versionA >= versionB;
}

function mo_hasMinVersion (minRequiredVersion) {
	return mo_meetsMinVersion (mo_BeatnikVersion,minRequiredVersion);
}

function mo_SupportedClient (_name,_platform,_minVersion,_upgradable) {
	this.name = _name;
	this.platform = _platform;
	this.minVersion = _minVersion;
	this.upgradable = _upgradable;
}

mo_supportedClients = new Array (
	new mo_SupportedClient ('Netscape','Win32','3.01',true),
	new mo_SupportedClient ('Netscape','MacPPC','3.01',true),
	new mo_SupportedClient ('WebTV Plus Receiver','WebTV','3.0',false),
	new mo_SupportedClient ('WebTV Satellite Receiver','WebTV','3.0',false),
	new mo_SupportedClient ('Microsoft Internet Explorer','Win32','4.0',false)
);

function mo_stringHasAny (sourceStr) {
	for (var argNo = 1; argNo < mo_stringHasAny.arguments.length; argNo++) {
		if (sourceStr.indexOf (mo_stringHasAny.arguments [argNo]) != -1) return true;
	}
	return false;
}

with (navigator) {
	if (mo_stringHasAny (userAgent,'Win95','Windows 95','WinNT','Windows NT','Win98','Windows 98')) {
		mo_platform = 'Win32';
	} else if (userAgent.indexOf ('PPC') != -1) {
		mo_platform = 'MacPPC';
	} else if (userAgent.indexOf ('WebTV') != -1) {
		mo_platform = 'WebTV';
	}
	mo_clientVersion = mo_retrieveVersion (navigator.appVersion);
	mo_browserSupported = false;
	for (mo_clientNo = 0; mo_clientNo < mo_supportedClients.length; mo_clientNo++) {
		mo_client = mo_supportedClients [mo_clientNo];
		if (mo_client.name == appName && mo_client.platform == mo_platform && mo_meetsMinVersion (mo_clientVersion,mo_client.minVersion)) {
			mo_browserSupported = true;
			break;
		}
	}
	if (mo_browserSupported) {
		if (appName == 'Netscape' || appName == 'WebTV Internet Terminal') {
			mo_BeatnikType = 'Plug-in';
			for (mo_pluginNo = 0; mo_pluginNo < plugins.length; mo_pluginNo++) {
				if (plugins [mo_pluginNo].name.indexOf ('Beatnik') != -1 && plugins [mo_pluginNo].name.indexOf ('Stub') == -1) {
					mo_BeatnikVersion = mo_retrieveVersion (plugins [mo_pluginNo].name);
					if (mo_BeatnikVersion == '') {
						if (mo_platform == 'Mac') {
							mo_BeatnikVersion = '1.1.7';
						} else if (mo_platform == 'WebTV') {
							mo_BeatnikVersion = '0.9.0';
						}
					}
					mo_hasBeatnik = true;
					break;
				}
			}
			mo_hasCallbackIssue = appName == 'Netscape' && mo_hasMinVersion ('1.3') && navigator.javaEnabled ();
		} else if (appName == 'Microsoft Internet Explorer') {
			mo_BeatnikType = 'ActiveX';
			document.writeln (
				'<OBJECT ID="mo_testInstance" CLASSID="CLSID:B384F118-18EE-11D1-95C8-00A024330339" WIDTH=1 HEIGHT=1 ALIGN=LEFT>' +
				'<PARAM NAME="WIDTH" VALUE="1">' + 
				'<PARAM NAME="HEIGHT" VALUE="1">' + 
				'<PARAM NAME="AUTOSTART" VALUE="FALSE">' +
				'</OBJECT>'
			);
			mo_hasBeatnik = typeof (document.mo_testInstance.getVersion) != 'undefined';
			if (mo_hasBeatnik) mo_BeatnikVersion = mo_retrieveVersion (document.mo_testInstance.getVersion ());
			mo_hasCallbackIssue = true;
		}
	}
}

mo_attrNames = new Array ('SRC','TYPE','WIDTH','HEIGHT','NAME','AUTOSTART','LOOP','VOLUME','ALIGN','HSPACE','VSPACE','BGCOLOR','HIDDEN','DISPLAY','MODE','GROOVOID','ONREADY','ONPLAY','ONPAUSE','ONSTOP','ONMETAEVENT','CALLBACKS','METAEVENTS');

mo_attribs = new Array ();

function mo_promptClose () {
	if (typeof (mo_promptWindow) == 'object') mo_promptWindow.close ();
	window.focus ();
}

function mo_promptUser (heading,message,okText,okAction,cancelText,cancelAction,showStatus) {
	mo_promptWindow = window.open ('','mo_promptWindow','toolbar=no,location=no,directories=no,status=' + ((showStatus || navigator.appName == 'Microsoft Internet Explorer') ? 'yes' : 'no') + ',menubar=no,scrollbars=no,resizable=yes,width=400,height=300');
	if (typeof (okAction) != 'string' || okAction == '') okAction = 'mo_promptClose ()';
	if (typeof (cancelAction) != 'string' || cancelAction == '') cancelAction = 'mo_promptClose ()';
	if (typeof (heading) != 'string') heading = '';
	with (mo_promptWindow.document) {
		open ('text/html');
		writeln (
			'<HTML><HEAD><TITLE>' + heading + '</TITLE></HEAD><BODY BGCOLOR=002244 TEXT=CCCCCC>' +
			'<FORM><TABLE WIDTH=100% HEIGHT=100% BORDER=1 CELLSPACING=10 CELLPADDING=6 BGCOLOR=000000>'
		);
		if (heading != '') writeln ('<TR><TD ALIGN=CENTER BGCOLOR=0088AA><FONT FACE="ARIAL,HELVETICA,VERDANA" COLOR=000000 SIZE=+2>' + heading + '</FONT></U><BR></TD></TR>');
		writeln (
			'<TR><TD VALIGN=TOP><FONT FACE="ARIAL,HELVETICA,VERDANA">' + message + '<P></FONT>' +
			'<TABLE WIDTH=100% BORDER=0 CELLSPACING=0 CELLPADDING=0><TR><TD ALIGN=LEFT>'
		);
		if (typeof (cancelText) == 'string' && cancelText != '')
			writeln ('<INPUT TYPE=button VALUE="' + cancelText + '" ONCLICK="with (window.opener) {' + cancelAction + '}">')
		;
		writeln ('</TD><TD ALIGN=RIGHT>');
		if (typeof (okText) == 'string' && okText != '')
			writeln ('<INPUT TYPE=button VALUE="' + okText + '" ONCLICK="with (window.opener) {' + okAction + '}">')
		;
		writeln ('</TD></TR></TABLE></TD></TR></TABLE></FORM></BODY></HTML>');
		close ();
		if (typeof (Event) != 'undefined') {
			mo_promptWindow.captureEvents (Event.KEYUP);
			mo_promptWindow.onKeyUp = new Function ('event','if (event.which == 13) {' + okAction + '} else if (event.which == 27) {' + cancelAction + '}; return false');
			mo_promptWindow.focus ();
		}
	}
}

function mo_installBeatnik () {
	mo_promptWindow = window.open ('http://www.headspace.com/to/install-player.html','mo_promptWindow','toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=no,resizable=no,width=400,height=300');
}

function mo_indexOf (sourceStr,searchStr,startPos) {
	var result = sourceStr.indexOf (searchStr,startPos);
	return (result != -1) ? result : sourceStr.length;
}

function mo_Voice_free () {
	with (this) {
		musicObjectID.playerID.noteOff (channelNo,noteNo,127);
		clearTimeout (noteOffTimeout);
		channelNo = 0;
	}
}

function mo_Voice (musicObjectID,channelNo,noteNo) {
	/*** Constructor Variables ***/
	this.musicObjectID = musicObjectID;
	this.channelNo = channelNo;
	this.noteNo = noteNo;

	/*** Instance State Variables ***/
	this.timeStamp = 0;
	this.noteOffTimeout = setTimeout ('',0);

	/*** Object's Exposed Methods ***/
	this.free = mo_Voice_free;
}

mo_polyphony = 32;
mo_voices = new Array ();
mo_newVoice = new mo_Voice (null,0,0);

for (mo_voiceNo = 0; mo_voiceNo < mo_polyphony; mo_voiceNo++)
	mo_voices [mo_voiceNo] = new mo_Voice (null,0,0)
;

function mo_isPlayerCompatible (minVersion,silentIfInadequate,showCompatibilityPrompt) {
	if (mo_browserSupported) {
		if (typeof (silentIfInadequate) == 'boolean') mo_silentIfInadequate = silentIfInadequate;
		if (typeof (showCompatibilityPrompt) == 'boolean') mo_showCompatibilityPrompt = showCompatibilityPrompt;
		if (typeof (minVersion) == 'string') mo_requiredMinVersion = minVersion;
		if (mo_hasBeatnik) {
			if (mo_hasMinVersion (mo_requiredMinVersion)) {
				return true;
			} else {
				if (mo_client.upgradable && mo_showCompatibilityPrompt) mo_promptUser ('Please Upgrade Beatnik','This page has been optimized for the features of version <FONT COLOR=FFFFFF><B>' + mo_requiredMinVersion + ' (or higher)</B></FONT> of the Beatnik Player. The currently installed version in your browser is ' + mo_BeatnikVersion + '.<P>If you choose to IGNORE this message, the page will continue to load normally, but may not function properly as designed by the author.','UPGRADE BEATNIK >>>','mo_installBeatnik ()','IGNORE','mo_promptClose ()');
				return false;
			}
		} else {
			if (mo_client.upgradable && mo_showCompatibilityPrompt) mo_promptUser ('Beatnik Enhanced Content !!','This page has been optimized for the audio features of the <FONT COLOR=FFFFFF><B>Beatnik Player</B></FONT>. It appears you do not have the Beatnik Player installed.<P>If you choose to IGNORE this message, the page will continue to load normally, except there will be no Beatnik audio.','INSTALL BEATNIK >>>','mo_installBeatnik ()','IGNORE','mo_promptClose ()');
			return false;
		}
	} else return false;
}

function mo_requireJava () {
	mo_ignoreJavaDisabled = true;
	if (mo_browserSupported && mo_client.name == 'Netscape' && !navigator.javaEnabled ()) {
		mo_promptUser ('Please Enable Java','This page makes use of the interactive audio features of the Beatnik Player. Java is currently not enabled in your browser. In order for the page to function correctly with Beatnik, <FONT COLOR=FFFFFF><B>you must have Java enabled</B></FONT>.<P>This page will continue to load normally, but some interactive audio functionality may be absent.','    OK    ','mo_promptClose ()');
	}
}

/*** musicObject class definition ***/

function mo_null () {
	if (this.ready)
		mo_debug ('Attempted to use a method not supported in this version of the Beatnik Player.',window.location.href,'unknown');
		else if (!mo_ignoreNotReady) mo_debug ('musicObject instance "' + this.objectName + '" is not yet ready for scripting control.',window.location.href,'unknown');
}

function mo_execCallback (callbackHandler) {
	if (typeof (callbackHandler) == 'string') eval (callbackHandler);
		else if (typeof (callbackHandler) == 'function') callbackHandler ();
}

function mo_magicEmbed (embedTag) {
	if (typeof (mo_playerCompatible) != 'boolean') mo_playerCompatible = mo_isPlayerCompatible (mo_requiredMinVersion,null,mo_showCompatibilityPrompt);
	var
		subChar,
		isMethod = typeof (this.playerID) != 'undefined'
	;
	if (!isMethod || typeof (this.playerName) != 'string') {
		for (var attrNo = 0; attrNo < mo_attrNames.length; attrNo++) mo_attribs [mo_attrNames [attrNo]] = null;
		with (this) {
			if (isMethod) {
				this.playerName = objectName + 'Plugin';
				mo_attribs ['NAME'] = playerName;
				var prefix = objectName + '.execOn';
				mo_attribs ['ONREADY'] = prefix + 'Ready()';
				mo_attribs ['ONPLAY'] = prefix + 'Play()';
				mo_attribs ['ONPAUSE'] = prefix + 'Pause()';
				mo_attribs ['ONSTOP'] = prefix + 'Stop()';
				mo_attribs ['ONMETAEVENT'] = prefix + 'MetaEvent()';
				mo_attribs ['METAEVENTS'] = 'FALSE';
			}

			mo_attribs ['AUTOSTART'] = 'TRUE';
			mo_attribs ['WIDTH'] = '144';
			mo_attribs ['HEIGHT'] = '60';
			mo_attribs ['HSPACE'] = '0';
			mo_attribs ['VSPACE'] = '0';
			mo_attribs ['BGCOLOR'] = document.bgColor;
			mo_attribs ['VOLUME'] = '100';

			var
				tagEndFound = false,
				attrStartPos = 0
			;
			while (!tagEndFound && attrStartPos < embedTag.length) {
				var attrFound = false;
				while (!tagEndFound && !attrFound && attrStartPos < embedTag.length) {
					subChar = embedTag.charAt (attrStartPos);
					if (subChar != ' ') {
						if (subChar == '>') tagEndFound = true;
							else attrFound = true;
					} else attrStartPos++;
				}
				if (attrFound) {
					var
						equalPos = mo_indexOf (embedTag,'=',attrStartPos),
						spacePos = mo_indexOf (embedTag,' ',attrStartPos),
						closePos = mo_indexOf (embedTag,'>',attrStartPos),
						attrNameEndPos = equalPos,
						hasValue = true
					;
					if (spacePos < attrNameEndPos) {
						attrNameEndPos = spacePos;
						hasValue = false;
					}
					if (closePos < attrNameEndPos) {
						attrNameEndPos = closePos;
						hasValue = false;
						tagEndFound = true;
					}
					var
						attrName = embedTag.substring (attrStartPos,attrNameEndPos).toUpperCase (),
						attrValuePos = attrNameEndPos + 1,
						attrValue = ''
					;
					if (hasValue) {
						var
							charCount = 0,
							attrValueEndFound = false,
							quoteChar = '',
							inEscape = false
						;
						while (!attrValueEndFound && attrValuePos < embedTag.length && !tagEndFound) {
							subChar = embedTag.charAt (attrValuePos);
							if (subChar == '"' || subChar == "'") {
								if (charCount == 0) {
									quoteChar = subChar;
									subChar = '';
								} else if (subChar == quoteChar && !inEscape) {
									attrValueEndFound = true;
									subChar = '';
								}
							} else if (subChar == ' ') {
								if (quoteChar == '') {
									attrValueEndFound = true;
									subChar = '';
								}
							} else if (subChar == '>') {
								if (quoteChar == '') {
									attrValueEndFound = true;
									tagEndFound = true;
									subChar = '';
								}
							} else if (subChar == '\\') {
								if (!inEscape) {
									inEscape = true;
									subChar = '';
								} else inEscape = false;
							}
							if (subChar != '\\') inEscape = false;
							attrValue += subChar;
							attrValuePos++;
							charCount++;
						}
					}
					attrStartPos = attrValuePos;
					mo_attribs [attrName] = attrValue;
				}
			}

			mo_attribs ['TYPE'] = 'audio/rmf';
			if (mo_attribs ['SRC'] == '') mo_attribs ['SRC'] = null;
			if (mo_attribs ['HIDDEN'] == '') mo_attribs ['HIDDEN'] = 'TRUE';
			if (isMethod) {
				if (mo_attribs ['NAME'] == '') mo_attribs ['NAME'] = playerName;
				playerName = mo_attribs ['NAME'];
			}

			if (isMethod) {
				if (mo_hasCallbackIssue) {
					mo_attribs ['CALLBACKS'] = 'FALSE';
					delayAutostart = mo_attribs ['AUTOSTART'].toUpperCase () == 'TRUE';
					mo_attribs ['AUTOSTART'] = 'FALSE';
					if (!mo_bodyEventsAdded) {
						mo_bodyEventsAdded = true;
						if (mo_BeatnikType == 'ActiveX') mo_addEventHandler (window,window,'onunload','mo_destroy ()');
						mo_addEventHandler (window,window,'onload','eval (mo_windowOnloadStr)');
					}
					mo_windowOnloadStr += objectName + '.enable ();';
				}
				this.volume = mo_attribs ['VOLUME'] - 0;
			}

			with (document) {
				if (mo_enabled && mo_browserSupported && mo_hasBeatnik && (mo_playerCompatible || !mo_silentIfInadequate)) {
					if (mo_BeatnikType == 'Plug-in') {
						var newEmbedTag = '<EMBED';
						for (var attrNo = 0; attrNo < mo_attrNames.length; attrNo++) {
							if (mo_attribs [mo_attrNames [attrNo]] != null) {
								newEmbedTag += ' ' + mo_attrNames [attrNo];
								if (mo_attribs [mo_attrNames [attrNo]] != '')
									newEmbedTag += '="' + mo_attribs [mo_attrNames [attrNo]] + '"'
								;
							}
						}
						newEmbedTag += '>';
						writeln (newEmbedTag);
						if (isMethod && !mo_ignoreJavaDisabled) mo_requireJava ();
					} else if (mo_BeatnikType == 'ActiveX') {
						if (isMethod) {
							var callbacks = new Array ('OnReady','OnPlay','OnPause','OnStop','OnMetaEvent');
							for (var callbackNo = 0; callbackNo < callbacks.length; callbackNo++) {
								var
									callbackParams = '(' + (callbacks [callbackNo] == 'OnMetaEvent' ? 'eventType,eventValue' : '') + ')',
									callbackHandler = mo_attribs [callbacks [callbackNo].toUpperCase ()]
								;
								writeln ('<SCRIPT LANGUAGE=JavaScript FOR="' + playerName + '" EVENT="' + callbacks [callbackNo] + callbackParams + '">' + callbackHandler.substring (0,callbackHandler.indexOf ('(')) + callbackParams + '</SCRIPT>');
							}
						}
						writeln ('<OBJECT ID="' + mo_attribs ['NAME'] + '" WIDTH=' + mo_attribs ['WIDTH'] + ' HEIGHT=' + mo_attribs ['HEIGHT'] + ' CLASSID="CLSID:B384F118-18EE-11D1-95C8-00A024330339">');
						for (var attrNo = 0; attrNo < mo_attrNames.length; attrNo++) {
							if (mo_attribs [mo_attrNames [attrNo]] != null)
								writeln ('<PARAM NAME="' + mo_attrNames [attrNo] + '" VALUE="' + mo_attribs [mo_attrNames [attrNo]] + '">')
							;
						}
						writeln ('</OBJECT>');
					}
				} else {
					var tableDims = ' WIDTH=' + mo_attribs ['WIDTH'] + ' HEIGHT=' + mo_attribs ['HEIGHT'] + ' HSPACE=' + mo_attribs ['HSPACE'] + ' VSPACE=' + mo_attribs ['VSPACE'] + ((mo_attribs ['ALIGN'] != null) ? (' ALIGN=' + mo_attribs ['ALIGN']) : 'LEFT');
					if (mo_enabled && mo_browserSupported && mo_client.upgradable && (mo_attribs ['HIDDEN'] == null || mo_attribs ['HIDDEN'].toUpperCase () != 'TRUE')) {
						writeln ('<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=0 BGCOLOR=FFFFFF' + tableDims + '><TR ALIGN=CENTER VALIGN=CENTER><TD><A HREF="javascript://" ONCLICK="mo_installBeatnik (); return false"><FONT FACE="ARIAL,HELVETICA,VERDANA" COLOR=000000>' + (mo_hasBeatnik ? 'Upgrade' : 'Get') + ' Beatnik!</FONT></A></TD></TR></TABLE>');
					} else {
						writeln ('<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0' + tableDims + '><TR><TD></TD></TR></TABLE>');
					}
				}
			}
		}
	}
}

function mo_stubEmbed (stubURL) {
	this.magicEmbed (((typeof (stubURL) != 'string') ? '' : ('SRC="' + stubURL + '" ')) + 'WIDTH=2 HEIGHT=2 HIDDEN AUTOSTART=TRUE LOOP=TRUE');
}

function mo_preloadEmbed (fileURL,extraAttr) {
	if (typeof (extraAttr) != 'string') extraAttr = '';
	this.magicEmbed ('SRC="' + fileURL + '" WIDTH=2 HEIGHT=2 HIDDEN AUTOSTART=FALSE LOOP=FALSE VOLUME=100 ' + extraAttr);
}

function mo_play (p1,p2) {
	with (this) {
		endVolumeFade ();
		if (typeof (p2) == 'string') {
			if (p2.indexOf ('.') == -1 && p2.indexOf ('groovoid://') != 0) p2 = 'groovoid://' + p2;
			if (typeof (p1) == 'string') p1 = (p1 == 'auto') ? (p2.indexOf ('groovoid://Background-') == 0) : (p1 == 'yes');
			playerID.play (p1,p2);
		} else if (typeof (p1) == 'boolean' || typeof (p1) == 'number') {
			playerID.play (p1);
		} else if (typeof (p1) == 'string') {
			play ('auto',p1);
		} else {
			if (!playerID.isPlaying ()) playerID.play ();
		}
	}
}

function mo_stop (fade) {
	with (this) {
		if (typeof (fade) == 'undefined') {
			endVolumeFade ();
			playerID.stop ();
		} else {
			if (isPaused ()) {
				endVolumeFade ();
				playerID.stop ();
			} else {
				fadeVolume (null,0,fade,objectName + '.stop ()');
			}
		}
	}
}

function mo_stopAll () {
	with (this) {
		endVolumeFade ();
		playerID.stopAll ();
	}
}

function mo_pause (fade) {
	with (this) {
		endVolumeFade ();
		if (isPaused ()) {
			playerID.pause ();
			if (typeof (fade) == 'number') fadeVolume (0,100,fade);
		} else {
			if (typeof (fade) != 'number') playerID.pause ();
				else fadeVolume (null,0,fade,objectName + '.pause ()');
		}
	}
}

function mo_endVolumeFade () {
	with (this) {
		if (volFade_inProgress) {
			clearTimeout (volFade_timeout);
			volFade_inProgress = false;
			mo_execCallback (volFade_endCallback);
			if (volFade_restoreVolume) setVolume (volFade_fromVolume);
		}
	}
}

function mo_execVolumeFade () {
	with (this) {
		volFade_volume += volFade_step;
		setVolume (Math.round (volFade_volume));
		volFade_timeElapsed += volFade_interval;
		mo_execCallback (volFade_advanceCallback);
		if (volFade_timeElapsed >= volFade_time) {
			setVolume (volFade_toVolume);
			endVolumeFade ();
		} else {
			volFade_timeout = setTimeout (objectName + '.execVolumeFade ()',volFade_interval);
		}
	}
}

function mo_fadeVolume (fromVolume,toVolume,fadeTime,fadeEndCallback,fadeAdvanceCallback) {
	with (this) {
		if (typeof (fromVolume) != 'number') fromVolume = getVolume ();
		if (typeof (toVolume) != 'number') toVolume = 0;
		if (typeof (fadeTime) == 'boolean') fadeTime = fadeTime ? 1000:0;
		if (typeof (fadeTime) != 'number') fadeTime = 1000;
		if (
			volFade_inProgress &&
			toVolume == volFade_toVolume &&
			fadeTime == volFade_time
		) {
			volFade_endCallback = fadeEndCallback;
			volFade_advanceCallback = fadeAdvanceCallback;
		} else {
			volFade_restoreVolume = typeof (fadeEndCallback) == 'string' && (fadeEndCallback.indexOf ('.stop ()') != -1 || fadeEndCallback.indexOf ('.pause ()') != -1);
			endVolumeFade ();
			if (fadeTime != 0 && fadeTime < mo_minFadeInterval)
				fadeTime = Math.round (fadeTime / mo_minFadeInterval) * mo_minFadeInterval
			;
			if (fadeTime == 0 || toVolume == fromVolume) {
				if (!volFade_restoreVolume)	setVolume (toVolume);
				mo_execCallback (fadeEndCallback);
			} else {
				volFade_fromVolume = fromVolume;
				volFade_toVolume = toVolume;
				volFade_time = fadeTime;
				volFade_endCallback = fadeEndCallback;
				volFade_advanceCallback = fadeAdvanceCallback;
				volFade_timeElapsed = 0;
				volFade_volume = fromVolume;
				volFade_inProgress = true;
				volFade_interval = Math.max (Math.ceil (volFade_time / Math.abs (toVolume - fromVolume)),mo_minFadeInterval);
				volFade_step = (toVolume - fromVolume) / (volFade_time / volFade_interval);
				setVolume (fromVolume);
				volFade_timeout = setTimeout (objectName + '.execVolumeFade ()',volFade_interval);
			}
		}
	}
}

function mo_fadeTo (toValue,fadeTime,fadeEndCallback,fadeAdvanceCallback) {
	this.fadeVolume (null,toValue,fadeTime,fadeEndCallback,fadeAdvanceCallback);
}

function mo_enable () {
	with (this) {
		if (typeof (window [playerName]) == 'object') {
			playerID = window [playerName];
		} else if (typeof (document [playerName]) == 'object') {
			playerID = document [playerName];
		} else {
			for (var formNo = 0; formNo < document.forms.length; formNo++) {
				formHandle = document.forms [formNo];
				if (typeof (formHandle [playerName]) == 'object') {
					playerID = formHandle [playerName];
					break;
				}
			}
		}
		if (playerID != null) {
			defineMethodsMO ('getAutostart','getChannelMute','getChannelSolo','getController','getInfo','getLoop','getPanelDisplay','getPanelMode','getProgram','getReverbType','getTempo','getTranspose','getTrackMute','getTrackSolo','getVolume','isPaused','isPlaying','setAutostart','setChannelSolo','setController','setGlobalMute','setLoop','setPanelDisplay','setPanelMode','setReverbType','setTempo','setTrackMute','setTrackSolo','setTranspose','setVolume','fadeTo','noteOff','noteOn','pause','play','playGroovoid','setChannelMute','setProgram','stop','fadeVolume');
			fadeFromTo = fadeVolume;
			playGroovoid = play;
			playNote = noteOn;
			if (mo_BeatnikVersion == '') mo_BeatnikVersion = mo_retrieveVersion (playerID.getVersion () + '');
			if (mo_hasMinVersion ('1.3'))
				defineMethodsMO ('doMenuItem','engageAudio','getFileSize','getPlayLength','getPosition','getTransposable','setEndTime','setPosition','setStartTime','setTransposable','stopAll')
			;
			if (typeof (onMetaEventHandler) == 'function') playerID.enableMetaEvents (true);
			if (mo_hasCallbackIssue) {
				playerID.enableCallbacks (true);
			} else {
				execOnReady ();
			}
		} else {
			setTimeout (objectName + '.enable ()',500);
		}
	}
}

function mo_execOnReady () {
	with (this) {
		if (ready) {
			if (mo_BeatnikType == 'ActiveX') playerID.receivedReady (true);
		} else {
			ready = playerID != null;
			if (ready) {
				if (mo_BeatnikType == 'ActiveX') playerID.receivedReady (true);
				if (delayAutostart) {
					setAutostart (true);
					play ();
				}
				mo_execCallback (onReadyHandler);
			} else {
				enable ();
			}
		}
		if (ready) mo_execCallback (onLoadHandler);
	}
}

function mo_setMonophonic (channelNo,state) {
	with (this) {
		if (channelNo == 0)
			for (var channelCount = 0; channelCount < 16; channelCount++) monophonic [channelCount] = state;
			else monophonic [channelNo - 1] = state;
	}
}

function mo_getMonophonic (channelNo) {return this.monophonic [channelNo - 1]}

function mo_noteOn (_channelNo,p2,p3,p4,p5,p6) {
	var mo_voiceNo;
	with (this) {
		if (mo_noteOn.arguments.length >= 5) {
			if (monophonic [_channelNo - 1]) noteOff (_channelNo);
			if (typeof (p4) == 'string') p4 = getNoteNumber (p4);
			mo_globalNoteNo++;
			with (mo_newVoice) {
				musicObjectID = this;
				timeStamp = mo_globalNoteNo;
				channelNo = _channelNo;
				noteNo = p4;
			}
			var assignToVoiceNo = -1;
			for (mo_voiceNo = 0; mo_voiceNo < mo_polyphony; mo_voiceNo++) {
				with (mo_voices [mo_voiceNo]) {
					if (channelNo == 0) {
						assignToVoiceNo = mo_voiceNo;
						break;
					}
				}
			}
			if (assignToVoiceNo == -1) {
				assignToVoiceNo = 0;
				for (mo_voiceNo = 0; mo_voiceNo < mo_polyphony; mo_voiceNo++) {
					if (mo_voices [mo_voiceNo].timeStamp < mo_voices [assignToVoiceNo].timeStamp)
						assignToVoiceNo = mo_voiceNo
					;
				}
			}
			with (mo_voices [assignToVoiceNo]) {
				musicObjectID = mo_newVoice.musicObjectID;
				timeStamp = mo_newVoice.timeStamp;
				channelNo = mo_newVoice.channelNo;
				noteNo = mo_newVoice.noteNo;
				if (p2 >= 0 && p3 >= 0) playerID.noteOn (channelNo,p2,p3,p4,p5);
					else playerID.noteOn (channelNo,p4,p5);
				if (typeof (p6) == 'number') noteOffTimeout = setTimeout ('mo_voices [' + assignToVoiceNo + '].free ()',p6);
			}
		} else {
			noteOn (_channelNo,-1,-1,p2,p3,p4);
		}
	}
}

function mo_noteOff (_channelNo,_noteNo) {
	with (this) {
		if (typeof (_noteNo) == 'undefined') {
			for (var mo_voiceNo = 0; mo_voiceNo < mo_polyphony; mo_voiceNo++) {
				with (mo_voices [mo_voiceNo])
					if (musicObjectID == this && channelNo == _channelNo) free ()
				;
			}
		} else {
			if (typeof (_noteNo) == 'string') _noteNo = getNoteNumber (_noteNo);
			for (var mo_voiceNo = 0; mo_voiceNo < mo_polyphony; mo_voiceNo++) {
				with (mo_voices [mo_voiceNo]) {
					if (musicObjectID == this && channelNo == _channelNo && noteNo == _noteNo) {
						free ();
						break;
					}
				}
			}
		}
	}
}

function mo_setProgram (channelNo,p2,p3) {
	with (this) {
		if (typeof (p3) == 'number') playerID.setProgram (channelNo,p2,p3);
			else playerID.setProgram (channelNo,p2);
	}
}

function mo_setChannelMute (channelNo,state) {
	with (this) {
		if (channelNo == 0) {
			for (var channelCount = 1; channelCount <= 16; channelCount++)
				playerID.setChannelMute (channelCount,state)
			;
		} else {
			playerID.setChannelMute (channelNo,state)
		}
	}
}

function mo_isReady () {return this.ready}

function mo_getNoteNumber (noteName) {
	var
		noteOffset = 'c-d-ef-g-a-b'.indexOf (noteName.charAt (0).toLowerCase ()),
		result = 0,
		sharpFlatOffset = 0
	;
	if (noteOffset != -1) {
		var sharpFlatPos = noteName.indexOf ('b',1);
		if (sharpFlatPos == -1) {
			sharpFlatPos = noteName.indexOf ('#',1);
			if (sharpFlatPos == -1) sharpFlatPos = 0; else sharpFlatOffset = 1;
		} else {
			sharpFlatOffset = -1;
		}
		var octaveNo = noteName.substring (sharpFlatPos + 1) - 0;
		result =  12 + octaveNo * 12 + noteOffset + sharpFlatOffset;
	}
	return Math.floor (result);
}

function mo_getNoteName (noteNumber) {
	var noteNames = new Array ('C','C#','D','D#','E','F','F#','G','G#','A','A#','B');
	return noteNames [noteNumber % 12] + (Math.floor (noteNumber / 12) - 1) + '';
}

function mo_onLoad (onLoadHandler) {this.onLoadHandler = onLoadHandler}
function mo_onPause (onPauseHandler) {this.onPauseHandler = onPauseHandler}
function mo_onPlay (onPlayHandler) {this.onPlayHandler = onPlayHandler}
function mo_onReady (onReadyHandler) {this.onReadyHandler = onReadyHandler}
function mo_onStop (onStopHandler) {this.onStopHandler = onStopHandler}

function mo_onMetaEvent (_onMetaEventHandler) {
	with (this) {
		onMetaEventHandler = _onMetaEventHandler;
		if (ready) playerID.enableMetaEvents (typeof (onMetaEventHandler) == 'function');
	}
}

function mo_execOnPause () {
	with (this) {
		endVolumeFade ();
		mo_execCallback (this.onPauseHandler);
	}
}

function mo_execOnPlay () {
	with (this) {
		setVolume (volume);
		mo_execCallback (onPlayHandler);
	}
}

function mo_execOnStop () {
	with (this) {
		endVolumeFade ();
		mo_execCallback (onStopHandler);
	}
}

function mo_execOnMetaEvent (eventType,eventValue) {
	with (this)
		if (onMetaEventHandler != null) onMetaEventHandler (eventType,eventValue,this)
	;
}

function mo_setVolume (_volume) {
	with (this) {
		volume = _volume;
		playerID.setVolume (_volume);
	}
}

function mo_getVersion () {return mo_BeatnikVersion}
function mo_getVolume () {return this.volume}

function mo_isPaused () {return this.playerID.IsPaused ()}
function mo_isPlaying () {return this.playerID.IsPlaying ()}

/*** Functions mapped directly to the player ***/

function mo_doMenuItem (menuItemName) {this.playerID.doMenuItem (menuItemName)}
function mo_engageAudio (audioState) {this.playerID.engageAudio (audioState)}
function mo_getAutostart () {return this.playerID.getAutostart ()}
function mo_getChannelMute (channelNo) {return this.playerID.getChannelMute (channelNo)}
function mo_getChannelSolo (channelNo) {return this.playerID.getChannelSolo (channelNo)}
function mo_getController (channelNo,controllerNo) {return this.playerID.getController (channelNo,controllerNo)}
function mo_getFileSize () {return this.playerID.getFileSize ()}
function mo_getInfo (infoField) {return this.playerID.getInfo (infoField)}
function mo_getLoop () {return this.playerID.getLoop ()}
function mo_getPanelDisplay () {return this.playerID.getPanelDisplay ()}
function mo_getPanelMode () {return this.playerID.getPanelMode ()}
function mo_getPlayLength () {return this.playerID.getPlayLength ()}
function mo_getPosition () {return this.playerID.getPosition ()}
function mo_getProgram (channelNo) {return this.playerID.getProgram (channelNo)}
function mo_getReverbType () {return this.playerID.getReverbType ()}
function mo_getTempo () {return this.playerID.getTempo ()}
function mo_getTrackMute (trackNo) {return this.playerID.getTrackMute (trackNo)}
function mo_getTrackSolo (trackNo) {return this.playerID.getTrackSolo (trackNo)}
function mo_getTranspose () {return this.playerID.getTranspose ()}
function mo_getTransposable (channelNo) {return this.playerID.getTransposable (channelNo)}
function mo_setAutostart (state) {this.playerID.setAutostart (state)}
function mo_setChannelSolo (channelNo,state) {this.playerID.setChannelSolo (channelNo,state)}
function mo_setController (channelNo,controllerNo,controllerValue) {this.playerID.setController (channelNo,controllerNo,controllerValue)}
function mo_setEndTime (endTime) {this.playerID.setEndTime (endTime)}
function mo_setGlobalMute (muteState) {this.playerID.setGlobalMute (muteState)}
function mo_setLoop (state) {this.playerID.setLoop (state)}
function mo_setPanelDisplay (displayType) {this.playerID.setPanelDisplay (displayType)}
function mo_setPanelMode (panelMode) {this.playerID.setPanelMode (panelMode)}
function mo_setPosition (position) {this.playerID.setPosition (position)}
function mo_setReverbType (reverbType) {this.playerID.setReverbType (reverbType)}
function mo_setStartTime (startTime) {this.playerID.setStartTime (startTime)}
function mo_setTempo (tempo) {this.playerID.setTempo (tempo)}
function mo_setTrackMute (trackNo,state) {this.playerID.setTrackMute (trackNo,state)}
function mo_setTrackSolo (trackNo,state) {this.playerID.setTrackSolo (trackNo,state)}
function mo_setTranspose (transpose) {this.playerID.setTranspose (transpose)}
function mo_setTransposable (channelNo,state) {this.playerID.setTransposable (channelNo,state)}

function mo_defineMethodsNull () {
	for (var argNo = 0; argNo < mo_defineMethodsNull.arguments.length; argNo++)
		this [mo_defineMethodsNull.arguments [argNo]] = mo_null
	;
}

function mo_defineMethodsMO () {
	for (var argNo = 0; argNo < mo_defineMethodsMO.arguments.length; argNo++)
		this [mo_defineMethodsMO.arguments [argNo]] = window ['mo_' + mo_defineMethodsMO.arguments [argNo]]
	;
}

function musicObject (objectName) {
	this.objectName = objectName;

	/*** Instance State Variables ***/
	this.delayAutostart = false;
	this.volFade_timeout = setTimeout ('',0);
	this.volFade_step = 0;
	this.volFade_time = 0;
	this.volFade_timeElapsed = 0;
	this.volFade_interval = 0;
	this.volFade_fromVolume = 0;
	this.volFade_volume = 0;
	this.volFade_toVolume = 0;
	this.volFade_endCallback = null;
	this.volFade_inProgress = false;
	this.volFade_restoreVolume = false;
	this.monophonic = new Array (false,false,false,false,false,false,false,false,false,false,false,false,false,false,false);
	this.ready = false;
	this.playerID = null;
	this.onLoadHandler = null;
	this.onMetaEventHandler = null;
	this.onPauseHandler = null;
	this.onPlayHandler = null;
	this.onStopHandler = null;
	this.onReadyHandler = null;

	/*** Method Configuration Methods ***/
	this.defineMethodsNull = mo_defineMethodsNull;
	this.defineMethodsMO = mo_defineMethodsMO;

	/*** Initialisation ***/
	this.defineMethodsNull ('doMenuItem','engageAudio','fadeTo','fadeFromTo','fadeVolume','getAutostart','getChannelMute','getChannelSolo','getController','getFileSize','getInfo','getLoop','getPanelDisplay','getPanelMode','getPlayLength','getPosition','getProgram','getReverbType','getTempo','getTrackMute','getTrackSolo','getTranspose','getTransposable','getVolume','isPaused','isPlaying','noteOff','noteOn','pause','play','playGroovoid','playNote','setAutostart','setChannelMute','setChannelSolo','setController','setEndTime','setGlobalMute','setLoop','setPanelDisplay','setPanelMode','setPosition','setProgram','setReverbType','setStartTime','setTempo','setTrackMute','setTrackSolo','setTranspose','setTransposable','setVolume','stop','stopAll');

	this.defineMethodsMO ('enable','endVolumeFade','execOnMetaEvent','execOnPause','execOnPlay','execOnReady','execOnStop','execVolumeFade','getMonophonic','getNoteName','getNoteNumber','getVersion','isReady','magicEmbed','onLoad','onMetaEvent','onPause','onPlay','onStop','preloadEmbed','onReady','setMonophonic','stubEmbed');

	window [objectName] = this;
	mo_instances [mo_instances.length] = this;
}

function mo_destroy () {
	for (var mo_instanceNo = mo_instances.length - 1; mo_instanceNo >= 0; mo_instanceNo--)
		mo_instances [mo_instanceNo].playerID = null
	;
}
