// MP3 Embedder Script - Modified by Doug Jones www.cjboco.com
// Based on the original script Pop-Up Embedder Script 
// by David Battino, www.batmosphere.com

window.addEventListener?window.addEventListener("load",initMP3Embed,false):window.attachEvent("onload",initMP3Embed);

var mp3File = '_etc/demos/mlp-kl_4th_demo_1_rtrn_v2.mp3';
var mp3Link = '';
var mp3IFrame = '';
var isWin = navigator.userAgent.toLowerCase().indexOf("windows") != -1;

function initMP3Embed() {
	// check cookie settings
	if(readCookie("MLPMP3Embed")==null) {
		createCookie("MLPMP3Embed",true,7);
	}
	mp3Link=document.getElementById('MP3EmbedLink');
	if(mp3Link) {
		MP3CreateHolder();
		var op = 1.0;
		if(!isWin) {
			mp3Link.style.opacity = op;
			mp3Link.style.MozOpacity = op;
			mp3Link.style.filter = "Alpha(opacity="+parseInt(op*100)+")";
			if(readCookie("MLPMP3Embed")==true) {
				mp3Link.style.background="url(_etc/images/icn_audio_normal.png) no-repeat center top";
			} else {
				mp3Link.style.background="url(_etc/images/icn_audio_off.png) no-repeat center top";
			}
		} else {
			mp3Link.style.background="NONE";
			if(readCookie("MLPMP3Embed")==true) {
				mp3Link.style.filter='progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,sizingMethod=crop,src="_etc/images/icn_audio_normal.png")';
			} else {
				mp3Link.style.filter='progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,sizingMethod=crop,src="_etc/images/icn_audio_off.png")';
			}
		}
		mp3Link.onclick=function() {
			if(readCookie("MLPMP3Embed")==true) {
				mp3IFrame.innerHTML='';
				if(!isWin) {
					this.style.background="url(_etc/images/icn_audio_off.png) no-repeat center top";
				} else {
					this.style.background="NONE"
					this.style.filter='progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,sizingMethod=crop,src="_etc/images/icn_audio_off.png")';
				}
				eraseCookie("MLPMP3Embed");
				createCookie("MLPMP3Embed","0",7);
			} else {
				MP3Start();
				if(!isWin) {
					this.style.background="url(_etc/images/icn_audio_normal.png) no-repeat center top";
				} else {
					this.style.background="NONE"
					this.style.filter='progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,sizingMethod=crop,src="_etc/images/icn_audio_normal.png")';
				}
				eraseCookie("MLPMP3Embed");
				createCookie("MLPMP3Embed","1",7);
			}
		};
		if(!isWin) {
			mp3Link.onmouseover=function() {
				if(readCookie("MLPMP3Embed")==true) {
					this.style.background="url(_etc/images/icn_audio_normal_f2.png) no-repeat center top";
				} else {
					this.style.background="url(_etc/images/icn_audio_off_f2.png) no-repeat center top";
				}
			};
			mp3Link.onmouseout=function() {
				if(readCookie("MLPMP3Embed")==true) {
					this.style.background="url(_etc/images/icn_audio_normal.png) no-repeat center top";
				} else {
					this.style.background="url(_etc/images/icn_audio_off.png) no-repeat center top";
				}
			};
		} else {
			mp3Link.onmouseover=function() {
				if(readCookie("MLPMP3Embed")==true) {
					this.style.filter='progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,sizingMethod=crop,src="_etc/images/icn_audio_normal_f2.png")';
				} else {
					this.style.filter='progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,sizingMethod=crop,src="_etc/images/icn_audio_off.png")';
				}
			};
			mp3Link.onmouseout=function() {
				if(readCookie("MLPMP3Embed")==true) {
					this.style.filter='progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,sizingMethod=crop,src="_etc/images/icn_audio_normal.png")';
				} else {
					this.style.filter='progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,sizingMethod=crop,src="_etc/images/icn_audio_off_f2.png")';
				}
			};
		}
		if(readCookie("MLPMP3Embed")==true) {
			var pc=MP3Embed(mp3File,10);
			window.setTimeout(MP3Start,3000);
		}
	}
}

function MP3Start() {
	var pc=MP3Embed(mp3File,10);
	mp3IFrame.innerHTML=pc;
}

function MP3CreateHolder() {
	var bodyRef = document.getElementsByTagName("body").item(0);
	mp3IFrame = document.createElement('DIV');
	mp3IFrame.setAttribute('id','MP3IFrame');
	mp3IFrame.style.position="ABSOLUTE";
	mp3IFrame.style.top="-990px";
	mp3IFrame.style.left="-9990px";
	mp3IFrame.style.border='0px';
	mp3IFrame.style.display="BLOCK";
	mp3IFrame.style.width="1px";
	mp3IFrame.style.height="1px";
	mp3IFrame.style.overflow="HIDDEN";
	mp3IFrame.style.background="#fff";
	bodyRef.appendChild(mp3IFrame);
}

function MP3Embed(filepath,WindowNumber) {

	// Get Operating System 
	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 = 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"};
	// Add additional MIME types as desired
	
	var playerCode='';
	
	if (visitorOS != "Windows") { 
		// audio/mpeg, audio/x-wav, audio/x-ms-wma, etc.};
		var objTypeTag = mimeType; 
	}
	playerCode+="<html><head><title>MP3 Embedder v1.0</title></head>";
	playerCode+="<body bgcolor='#555' style='margin:20px;padding:20px;'>";
	playerCode+="<object width='280' height='69'>";
	playerCode+="<param name='src' value='" + filepath + "'>";
	playerCode+="<param name='type' value='" + objTypeTag + "'>";
	playerCode+="<param name='autostart' value='1'>";
	playerCode+="<param name='showcontrols' value='1'>";    
	playerCode+="<param name='showstatusbar' value='1'>";
	playerCode+="<embed src ='" + filepath + "' type='" + objTypeTag + "' autoplay='true' width='280' height='69' controller='1' showstatusbar='1' bgcolor='#9999ff' kioskmode='true'>";
	playerCode+="</embed></object>";
	playerCode+="</body></html>";
	return playerCode;
}