// Pop-Up Embedder Script by David Battino, www.batmosphere.com
// Version 2006-05-31 
// OK to use if this notice is included
   
function BatmoAudioPop(filedesc,filepath,WindowNumber) {
	// Get Operating System 
	var isWin = navigator.userAgent.toLowerCase().indexOf("windows") != -1
	if (isWin) {
	    // Use MIME type = "application/x-mplayer2";
	    visitorOS="Windows";
	    controllerHt=69; // Windows Media plug-in height
	} else {
	    // Use MIME type = "audio/mpeg"; // or audio/x-wav or audio/x-ms-wma, etc.
	    visitorOS="Other";
	    controllerHt=16; // QuickTime plug-in height
	}

	// Get the MIME type of the audio file from its extension (for non-Windows browsers)
	var mimeType = "audio/mpeg"; // assume MP3/M3U
	var objTypeTag = "application/x-mplayer2"; // The Windows MIME type to load the WMP plug-in in Firefox, etc.
/*  DOES NOT WORK!!!
	var objTypeTag = "application/x-ms-wmp"; // The Windows MIME type to load the WMP plug-in in Firefox, etc.
*/
	var theExtension = filepath.substr(filepath.lastIndexOf('.')+1, 3); // truncates .aiff to aif
	if (theExtension.toLowerCase() == "wav") { mimeType = "audio/x-wav";}
	if (theExtension.toLowerCase() == "aif") { mimeType = "audio/x-aiff";}
	if (theExtension.toLowerCase() == "wma") { mimeType = "audio/x-ms-wma";}
	if (theExtension.toLowerCase() == "mid") { mimeType = "audio/mid";}
	if (theExtension.toLowerCase() == "m4a") { mimeType = "audio/mp4";}
//	if (theExtension.toLowerCase() == "m4a") { mimeType = "audio/x-m4a";}
	// Add additional MIME types as desired

	if ( (visitorOS != "Windows") || (theExtension.toLowerCase() == "m4a") || (getQuicktimeVersion() > -1) ) { 
		objTypeTag = mimeType; // audio/mpeg, audio/x-wav, audio/x-ms-wma, etc.
	}
	
	// QuickTime specific (No idea why QT will not work with the <object> tag.)
	if ( (theExtension.toLowerCase() == "m4a") || (getQuicktimeVersion() > -1) ) {
		PlayerWin = window.open('',WindowNumber,'width=340,height=260,top=0,left=0,screenX=0,screenY=0,resizable=0,scrollbars=0,titlebar=0,toolbar=0,menubar=0,status=0,directories=0');
		PlayerWin.focus();
		PlayerWin.document.writeln("<html><head><title>" + filedesc + "</title> <style> a:link { color:#334433; } </style></head>");
		PlayerWin.document.writeln("<body bgcolor='#CCDDEE'; text='#660033'; onblur='javascript:setTimeout(\"window.focus()\",2000)';>"); // specify background image if desired
		PlayerWin.document.writeln("<div align='center'>");
		PlayerWin.document.writeln("<b style ='font-size:18px;font-family:Lucida,sans-serif;line-height:1.4'>" + filedesc + "</b><br />");
		PlayerWin.document.writeln("<embed src ='" + filepath + "' qtsrc ='" + filepath + "' type='" + objTypeTag + "' autoplay='true' autostart='true' width='280' height='"+controllerHt+"' controller='1' showstatusbar='1' bgcolor='#CCDDEE' kioskmode='true' pluginspace='http://www.apple.com/quicktime/download/'></embed>");
		PlayerWin.document.writeln("</div>");
		PlayerWin.document.writeln("<p style ='font-size:12px;font-family:Lucida,sans-serif;text-align:center;line-height:0.8;'>(Audio file may take a few moments to load. Please wait.)</p>");
		PlayerWin.document.writeln("<p style ='font-size:12px;font-family:Lucida,sans-serif;text-align:center;'>If file does not play, try <a href='" + filepath +"'>clicking here</a>.</p>");
		PlayerWin.document.writeln("<form><div align='center'><input type='button' value='Close window' onclick='javascript:window.close();'></div></form>");
		PlayerWin.document.writeln("</body></html>");
		PlayerWin.document.close(); // "Finalizes" new window
	} else {
		PlayerWin = window.open('',WindowNumber,'width=340,height=260,top=0,left=0,screenX=0,screenY=0,resizable=0,scrollbars=0,titlebar=0,toolbar=0,menubar=0,status=0,directories=0');
		PlayerWin.focus();
		PlayerWin.document.writeln("<html><head><title>" + filedesc + "</title> <style> a:link { color:#334433; } </style></head>");
		PlayerWin.document.writeln("<body bgcolor='#CCDDEE'; text='#660033'; onblur='javascript:setTimeout(\"window.focus()\",2000)';>"); // specify background image if desired
		PlayerWin.document.writeln("<div align='center'>");
		PlayerWin.document.writeln("<b style ='font-size:18px;font-family:Lucida,sans-serif;line-height:1.4'>" + filedesc + "</b><br />");
		PlayerWin.document.writeln("<object width='300' height='69' classid='clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6'>"); // ClassID forces IE to use Windows Media Player
		PlayerWin.document.writeln("<param name='src' value='" +  filepath + "'>");
		PlayerWin.document.writeln("<param name='qtsrc' value='" +  filepath + "'>");
		PlayerWin.document.writeln("<param name='filename' value='" +  filepath + "'>"); // alternative to src
		PlayerWin.document.writeln("<param name='data' value='" +  filepath + "'>"); // alternative to src
		PlayerWin.document.writeln("<param name='type' value='" + objTypeTag + "'>");
		PlayerWin.document.writeln("<param name='autostart' value='1'>");
		PlayerWin.document.writeln("<param name='autoplay' value='1'>");
		PlayerWin.document.writeln("<param name='showstatusbar' value='1'>");
		PlayerWin.document.writeln("<param name='showcontrols' value='1'>"); 
		PlayerWin.document.writeln("<embed src ='" + filepath + "' qtsrc ='" + filepath + "' type='" + objTypeTag + "' autoplay='true' autostart='true' width='280' height='"+controllerHt+"' controller='1' showstatusbar='1' bgcolor='#CCDDEE' kioskmode='true'></embed>");
		PlayerWin.document.writeln("</object>");
		PlayerWin.document.writeln("</div>");
		PlayerWin.document.writeln("<p style ='font-size:12px;font-family:Lucida,sans-serif;text-align:center;line-height:0.8;'>(Audio file may take a few moments to load. Please wait.)</p>");
		PlayerWin.document.writeln("<p style ='font-size:12px;font-family:Lucida,sans-serif;text-align:center;'>If file does not play, try <a href='" + filepath +"'>clicking here</a>.</p>");
		PlayerWin.document.writeln("<form><div align='center'><input type='button' value='Close window' onclick='javascript:window.close();'></div></form>");
		PlayerWin.document.writeln("</body></html>");
		PlayerWin.document.close(); // "Finalizes" new window
	}
}

function getQuicktimeVersion() {
	var quicktimeVersion = -1;
	var agent = navigator.userAgent.toLowerCase();

	if (navigator.plugins != null && navigator.plugins.length > 0) {
		for (i=0; i < navigator.plugins.length; i++ ) {
			var plugin =navigator.plugins[i];
			if (plugin.name.indexOf("QuickTime") > -1) {
				quicktimeVersion = parseFloat(plugin.name.substring(18));
			}
		}
        }

        return quicktimeVersion;
}
