// Pop-Up Audio/Photo Embedder Script by David Battino, www.batmosphere.com
// v 2005-10-04
// OK to use if this notice is included


function EnhAudioPop(popuptitle,imgpath,imgwidth,imgheight,caption,soundpath,UniqueID) { // Add error handling?

     var winWidth = Number(imgwidth) + 100;
     var rawHeight = Number(imgheight) + 168 + caption.length/7; // calculate window height based on caption length
     var winHeight = Math.round(rawHeight * Math.pow(10,0))/Math.pow(10,0); // round to integer

// Get Operating System 
var isWin = navigator.userAgent.toLowerCase().indexOf("windows") != -1
if (isWin) { // Use MIME type = "application/x-ms-wmp";
    visitorOS="Windows";
    winHeight = winHeight + 50;
    controllerHt=65; // 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-ms-wmp"; // The Windows MIME type to load the WMP plug-in in Firefox, etc.
var theExtension = soundpath.substr(soundpath.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/x-midi"};
if (theExtension.toLowerCase() == "ogg") { mimeType = "application/ogg"}; // try others
if (theExtension.toLowerCase() == "m3u") { mimeType = "audio/x-mpegurl"}; // may not be necessary
// Add additional MIME types as desired

     MediaWin = window.open('',UniqueID,'width=' + winWidth + ',height=' + winHeight + ',top=0,left=0,resizable=1,scrollbars=0,titlebar=0,toolbar=0,menubar=0,status=0,directories=0,personalbar=0');
     MediaWin.focus();
     var winContent = "<html><head><title>" + popuptitle + "</title></head>";
     winContent += "<body bgcolor='#9E9E9E' background='graphics/metal_tile.jpg'>"; // check image path
    
     winContent += "<div align='center'>";
     winContent += "<br /><br />"; // could use padding
     winContent += "<img src='" + imgpath + "' id='image1' border='2' alt='" + popuptitle + "' width='" + imgwidth + "' height='" + imgheight + " 'title='" + popuptitle + "' />";
     winContent += "<br />";
     winContent += "<object width='" + imgwidth + "' height='42' >"; // add 4 to width to align controller with img border?
     winContent += "<param name='src' value='" + soundpath + "'>";
     winContent += "<param name='autoplay' value='true'>";
     winContent += "<param name='controller' value='true'>";
     winContent += "<param name='bgcolor' value='#9e9e9e'>";
     winContent += "<embed src ='" + soundpath + "' autostart='true' loop='false' width='" + imgwidth + "' height='42' controller='true' bgcolor='#9e9e9e'>";
     winContent += "</embed></object>";

     winContent += "<div style='width: " + imgwidth + "px; margin: 0px; padding: 0px; text-align:left;'>"; // restrict caption width to image width
     winContent += "<p style='font-size:12px;font-family:Verdana,sans-serif'>" + caption + "</p>";
     winContent += "</div>";
     winContent += "<p style='font-size:12px;font-family:Verdana,sans-serif'><a href='" + soundpath +"'>Download audio file</a> <span style='font-size:10px'>(right-click or Option-click)</span>";
     winContent += " &#8226; <a href='#' onClick='javascript:window.close();'>Close this window</a></p>";

     winContent += "</div>";
     winContent += "</body></html>";
     MediaWin.document.write(winContent); 
     MediaWin.document.close(); // "Finalizes" new window
}