// JavaScript Document
// Some of this source code based on David Battino's scripts, www.batmosphere.com
Audio = function(mov, width, height)
{
    this.mov = mov || "";
    this.width = width || "100%";
    this.height = height || "100%";
    this.id = "";
	this.type = "";
	this.params = new Object();
};

Audio.resizeTo = function(id, w, h) { 
    var movie = document.getElementById(id); 
	movie.width = w; 
	movie.height = h;
};

Audio.prototype.getMov = function() { return this.mov; };
Audio.prototype.setMov = function(mov) { this.mov = mov; };
Audio.prototype.getWidth = function() { return this.width; };
Audio.prototype.setWidth = function(w) { this.width = w; };
Audio.prototype.getHeight = function() { return this.height; };
Audio.prototype.setHeight = function(h) { this.height = h; };
Audio.prototype.getId = function() { return this.id; };
Audio.prototype.setId = function(id) { this.id = id; };
Audio.prototype.getType = function() { return this.type; };
Audio.prototype.setType = function(type) { this.type = type; };
Audio.prototype.getParam = function(name) { return this.params[name]; };
Audio.prototype.getParams = function() { return this.params; };
Audio.prototype.setParam = function(name, value) { this.params[name] = value; };

Audio.prototype.getParamTags = function()
{
    var paramTags = "";
    for (var param in this.getParams()) {
        paramTags += '<param name="' + param + '" value="' + this.getParam(param) + '" />';
    }
    if (paramTags == "") {
        paramTags = null;
    }
    return paramTags;
};
Audio.prototype.getHTML = function()
{
    var AudioHTML = "";
    if (window.ActiveXObject && navigator.userAgent.indexOf('Mac') == -1) { // PC IE
		AudioHTML += "<object id='" + this.getId() + "' width='" + this.getWidth() + "' height='" + this.getHeight() + "' type='" + this.getType() + "'>";
		AudioHTML += "<param name='src' value='" + this.getMov() + "' />";
		AudioHTML += "<param name='autostart' value='0' />";
		if (this.getParamTags() != null)
		{
			AudioHTML += this.getParamTags();
		}
        AudioHTML += "<" + "/object>";
    }
    else { // Everyone else
		AudioHTML += "<embed id='" + this.getId() + "' src='" + this.getMov() + "' width='" + this.getWidth() + "' height='" + this.getHeight() + "' type='" + this.getType() + "' autoplay='0' autostart='0'";
		for (var param in this.getParams()) {
            AudioHTML += " " + param + "='" + this.getParam(param) + "'";
        }
        AudioHTML += "><" + "/embed>";
    }

    return AudioHTML;
};
Audio.prototype.render = function(elementId)
{
    //if(Audio.hasVersion(this.getRequiredVersion())) {
        if (elementId) {
            document.getElementById(elementId).innerHTML = this.getHTML();
        }
        else {
            document.write(this.getHTML());
        }
    //}
	//else
	//{
	//	document.write('<div class="no_Audio"><p>This portion of the site requires the latest version of the Audio Player Browser Plug-in. You may download this plug-in via Macromedia\'s website. Audio Player lets your web browser play video and animations made with Audio.</p>');
	//	document.write('<p align="center"><a href="http://www.macromedia.com/go/getAudioplayer/" target="blank"><img src="http://www.macromedia.com/images/shared/download_buttons/get_Audio_player.gif" border="0" /></a></p>');
	//	document.write('<p>&nbsp;</p><p align="center">After you have installed the latest Audio Player, you may <a href="javascript:document.location.reload(true);">reload this page</a>.</p></div>');
	//}
};


function RenderAudioPlayer(id, audioURL, width, height, showControls, showStatusBar) { 
	if (showControls == null)
	{
		showControls = 1;
	}
	
	if (showStatusBar == null)
	{
		showStatusBar = 1;
	}

	// Get Operating System 
	var isWin = navigator.userAgent.toLowerCase().indexOf("windows") != -1

	if (isWin) 
	{
		// Use MIME type = "application/x-mplayer2";
		visitorOS="Windows";
	} 
	else 
	{
		// Use MIME type = "audio/mpeg"; // or audio/x-wav or audio/x-ms-wma, etc.
		visitorOS="Other";
	}
	
	// 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.
	
	var theExtension = audioURL.substr(audioURL.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"};
	// Add additional MIME types as desired
	
	if (visitorOS != "Windows") { 
		objTypeTag = mimeType; // audio/mpeg, audio/x-wav, audio/x-ms-wma, etc.
	};

	/*
	document.writeln("<div>");
	document.writeln("<object width='" + width + "' height='" + height + "' type='" + objTypeTag + "'>"); // Width is the WMP minimum. Height = 45(WMP controls) + 24 (WMP status bar) 
	//document.writeln("<param name='type' value='" + objTypeTag + "'>");
	document.writeln("<param name='src' value='" + audioURL + "'>");
	document.writeln("<param name='autostart' value='0'>");
	document.writeln("<param name='showcontrols' value='" + showControls + "'>");
	document.writeln("<param name='controller' value='" + showControls + "'>");
	document.writeln("<param name='showstatusbar' value='" + showStatusBar + "'>");
	//document.writeln("<embed src ='" + audioURL + "' type='" + objTypeTag + "' autoplay='false' autostart='0' width='" + width + "' height='" + height + "' controller='" + showControls + "' showstatusbar='" + showStatusBar + "'></embed>"); // Firefox and Opera Win require both autostart and autoplay
	document.writeln("</object>");
	document.writeln("</div>");
	*/
	
	var html = '<div id="faudiocontainer_' + id + '"><' + '/div><script language="javascript" type="text/javascript">var audioform = new Audio("' + audioURL + '", "' + width + '", "' + height + '");';
	html += 'audioform.setParam("showcontrols", "' + showControls + '");';
	html += 'audioform.setParam("controller", "' + showControls + '");';
	html += 'audioform.setParam("showstatusbar", "' + showStatusBar + '");';
	html += 'audioform.setId("' + id + '");';
	html += 'audioform.setType("' + objTypeTag + '");';
	html += 'audioform.render("faudiocontainer_' + id + '");</' + 'script>';
	
	document.write('<div id="faudiocontainer_' + id + '"><' + '/div><script language="javascript" type="text/javascript">var audioform = new Audio("' + audioURL + '", "' + width + '", "' + height + '");');
	document.write('audioform.setParam("showcontrols", "' + showControls + '");');
	document.write('audioform.setParam("controller", "' + showControls + '");');
	document.write('audioform.setParam("showstatusbar", "' + showStatusBar + '");');
	document.write('audioform.setId("' + id + '");');
	document.write('audioform.setType("' + objTypeTag + '");');
	document.write('audioform.render("faudiocontainer_' + id + '");</' + 'script>');	
}
