/***************************
	(C) 2006 by Charleshan www.Denninger.nl
	http://www.paranoid.demon.nl
	Free for all users, but leave in this header.
		Started:	30-05-2006
		Last change:	31-12-2006
	==========================
	Function:		What is does:
	PlaySound()		Plays a sound in either "Microsoft Internet Explorer" or "Mozilla" browsers, like Firefox and Netscape. See function for details...
	PlaySoundRandom()	Play files randomly from the given location using PlaySound() parsing it's options.
	==========================
	v1.0T	30-05-2006	Started as a .js scriptfile.
	v1.0B	31-05-2006	Still testing conflicts between IE and Mozilla...
	v1.0	03-06-2006	It seems to work when I force to use WMP.
	v1.01	05-07-2006	PlaySoundRandom() added.
	v1.02	18-09-2006	Bug solved: PlaySoundRandom() minimum value added to while loop.
	v1.1	31-12-2006	Bug solved: Mozilla based browsers use 1 and 0 instead of true and false. (See: https://bugzilla.mozilla.org/show_bug.cgi?id=365261).
	==========================
	Notes:
		Windows Media Player < v6.4	clsid:22D6F312-B0F6-11D0-94AB-0080C74C7E95
		Windows Media Player > v7.x	clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6
		<param name=\"FileName\" value=\""+str_File+"\">	//url = IE?
		<param name=\"ShowAudioControls\" value=\"true\">
		<param name=\"ShowGotoBar\" value=\"false\">
		<param name=\"uiMode\" value=\"invisible\">
		<param name=\"uiMode\" value=\"mini\">
		<param name=\"WindowlessVideo\" value=\"true\">
****************************/

onerror=handleErr
function handleErr(msg,url,l){
	//Reference:	http://www.w3schools.com/js/js_onerror.asp
	//alert("There was an error on this page.\n\nError:	"+msg+"\nURL:	"+url+"\nLine:	"+l+"\n\nClick OK to continue.\n\n");
	return true
}

function PlaySound(str_File,flg_AutoStart,flg_Hidden,int_Width,int_Height,flg_Loop,flg_ForceWMP){
	/****************************
	str_File	File to play.
	flg_AutoStart	OPTIONAL: If true, autostart the player.
	flg_Hidden	OPTIONAL: If true, hide the player's interface.
	int_Width	OPTIONAL: Players Width,	default=240.
	int_Height	OPTIONAL: Players Height,	default=45.
	flg_Loop	OPTIONAL: If true, Loop.
	flg_ForceWMP	OPTIONAL: If true, FORCE to use the Windows Media Player.
	Note:
		If all options are false / default, you only have to give te str_File to play.
	Reference:	http://support.microsoft.com/kb/279022 | http://msdn.microsoft.com/library/en-us/wmplay10/mmp_sdk/playersettings.asp | http://plugindoc.mozdev.org/winmime.html | http://www.codecguide.com
	****************************/
	//var int_Height=45,int_Width=240;

	/***	Detect Windows Media Player	***/
	var flg_WMP=false;
	//Based on code found @:	http://www.javascriptkit.com/script/script2/plugindetect.shtml | http://www.aspfree.com/c/a/Windows-Scripting/Detecting-Plugins-in-Internet-Explorer
	if(window.ActiveXObject&&navigator.userAgent.indexOf("Windows")!=-1){	//alert("IE for Windows object instantiation -- use of ActiveXObject");
		//flg_WMP=(Detect_ActivX("WMP","MediaPlayer.MediaPlayer.1")=="WMP");
		var obj_ActiveX;
		try{
			obj_ActiveX=new ActiveXObject("MediaPlayer.MediaPlayer.1");
			flg_WMP=true;
		}
		catch(obj_Error){
			flg_WMP=false;
		}
	//}else if(window.GeckoActiveXObject){	alert("Netsape 7.1 object instantiation --use of GeckoActiveXObject");
	}else if(navigator.mimeTypes){	//alert("Plugin architecture, such as in Netscape 4x - 7.02 and Opera browsers");
		var str_NavPlug="";
		for(i=0;i<navigator.mimeTypes.length;i++)	str_NavPlug+=navigator.mimeTypes[i].type.toLowerCase();
		if(str_NavPlug.indexOf("application/x-mplayer2")!=-1)if(navigator.mimeTypes["application/x-mplayer2"].enabledPlugin!=null) flg_WMP=true;
	}	//alert("Windows Media Player="+flg_WMP);
	if(flg_ForceWMP==null) flg_ForceWMP=flg_WMP;	//Force Windows Media Player anyway
	if(flg_ForceWMP)flg_ForceWMP=1; else flg_ForceWMP=0;
	/***	Detect Windows Media Player	***/

	if(flg_AutoStart==null)flg_AutoStart=false;
	if(flg_Hidden	==null)flg_Hidden=false;
	if(flg_Loop	==null)flg_Loop=false;
	if(flg_Hidden)flg_Hidden=1; else flg_Hidden=0;
	if(flg_AutoStart)flg_AutoStart=1; else flg_AutoStart=0;
	if(flg_Loop)flg_Loop=1; else flg_Loop=0;
	//if(flg_ShowDisplay) flg_ShowDisplay=1; else flg_ShowDisplay=0;
	//if(flg_ShowStatusBar) flg_ShowStatusBar=1; else flg_ShowStatusBar=0;
	if(flg_Hidden&&flg_AutoStart){	//Work Around for Mozilla browsers.
		int_Width=0;
		int_Height=0;
	}else{
		flg_Hidden=false;
		if(int_Width	==0||int_Width	==null)int_Width=240;
		if(int_Height	==0||int_Height	==null)int_Height=45;
	}
	//if(int_Hspace	==null)int_Hspace=0;
	//if(int_Vspace	==null)int_Vspace=0;
	/***
	if(navigator.appName=="Microsoft Internet Explorer"){
		if(flg_Hidden&&flg_AutoStart){
			if(flg_ForceWMP)
				document.write("<object\n"+
				"classid=\"clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6\"\n"+
				"codebase=\"http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,5,715\"\n"+
				"standby=\"Loading M$ Windows Media Player components...\"\n"+
				"type=\"application/x-oleobject\"\n"+
				"height=\""+int_Height+"\"\n"+
				"width=\""+int_Width+"\">\n"+
				"<param name=\"autostart\" value=\""+flg_AutoStart+"\">\n"+
				"<param name=\"url\" value=\""+str_File+"\">\n"+
				"Your browser does not support the ActiveX? Windows Media Player!, <a href=\""+str_File+"\" target=\"PlayMedia\">Try this...</a>.</object>");
			else
				document.write("<bgsound balance=0 src=\""+str_File+"\" volume=10>");	//</bgsound>
		}else
			document.write("<object\n"+
				"classid=\"clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6\"\n"+
				"codebase=\"http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,5,715\"\n"+
				"standby=\"Loading M$ Windows Media Player components...\"\n"+
				"type=\"application/x-oleobject\"\n"+
				"height=\""+int_Height+"\"\n"+
				"width=\""+int_Width+"\">\n"+
				"<param name=\"autostart\" value=\""+flg_AutoStart+"\">\n"+
				"<param name=\"url\" value=\""+str_File+"\">\n"+
				"Your browser does not support this Media format!, <a href=\""+str_File+"\" target=\"PlayMedia\">Try this...</a>.</object>");
	***/
	if(navigator.appName=="Microsoft Internet Explorer"&&flg_Hidden&&flg_AutoStart){//&&flg_ForceWMP==false){
		document.write("<bgsound balance=\"0\" src=\""+str_File+"\" volume\"=10\">");	//</bgsound>
	}else{
		document.write("<embed\n");
		if(flg_ForceWMP){
			document.write("name=\"MediaPlayer\"\n"+
				"pluginspage=\"http://www.microsoft.com/windows/windowsmedia/\"\n"+
				"type=\"application/x-mplayer2\"\n");
			if(flg_Hidden)
				document.write("ShowControls=\"0\"\n"+
					"ShowDisplay=\"0\"\n"+
					"ShowStatusBar=\"0\"\n");	// uiMode=\"invisible\"
			else
				document.write("ShowControls=\"1\"\n"+
					"ShowDisplay=\"1\"\n"+
					"ShowStatusBar=\"1\"\n");	// uiMode=\"invisible\"
		}//document.write("hspace=\""+int_Hspace+"\"\nvspace=\""+int_Vspace+"\"\n");
		if(navigator.appName=="Microsoft Internet Explorer")	//Mozilla won't play if hidden=true!
			document.write("hidden=\""+flg_Hidden+"\"\n");
		document.write("src=\""+str_File+"\"\n"+
				"width=\""+int_Width+"\"\n"+
				"height=\""+int_Height+"\"\n"+
				"autostart=\""+flg_AutoStart+"\"\n"+
				"loop=\""+flg_Loop+"\"\n"+
				">\n<noembed><a href=\""+str_File+"\" target=\"PlayMedia\">&raquo; Afspelen</a>.</noembed>\n</embed>");
	}
}
//PlaySound("./sounds/demo/xxx.mp3",true,true,0,0,false,false);	//This is the command, but you can place this line anywhere else in a document between <script> tags...

function PlaySoundRandom(str_Path,str_Ext,int_Max,flg_AutoStart,flg_Hidden,int_Width,int_Height,flg_Loop,flg_ForceWMP){
	/****************************
	PlaySoundRandom(	Play files randomly from the given location.
	str_Path	Location and (optional name without numbers) of the files to play.
	str_Ext		File extention.
	int_Max		Highest file number starting at 0.
	****************************/
	var int_Nr;
	do {
		int_Nr=Math.random()*10;
		int_Nr=Math.round(int_Nr);
	} while(int_Nr<0||int_Nr>int_Max);
	PlaySound(str_Path+int_Nr+str_Ext,flg_AutoStart,flg_Hidden,int_Width,int_Height,flg_Loop,flg_ForceWMP);	//document.write(str_Path+int_Nr+str_Ext);
}
//PlaySoundRandom("./sounds/demo/",".mp4",7,true,true);	//This is the command playing randomly files 0 till 7, but you can place this line anywhere else in a document between <script> tags...
