window.onload = boot;
var mp;
var mpOut;
var browser;

function boot() {
	mpOut = document.getElementById("mpOut");
	browser = detect_browser();
	
	media = "musicas/Ephedra_excertos.mp3";
	loadMedia(media, 0);
	
	//media = "";
}

function loadMedia(media, start) {
	if (start)
		stop();
	if (browser == "MSIE" || browser == "Netscape") {
		mpOut.innerHTML = '<object id="mp"' 
		+ 'classid="clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6"'
		+ 'codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701"'
		+ 'standby="Loading Microsoft® Windows® Media Player components..."'  
		+ 'type="application/x-oleobject" width="1" height="1">'                
		+ '<param name="url" value="'+media+'">'      
		+ '<param name="volume" value="50">' 
		+ '<PARAM name="AUTOPLAY" VALUE="false">'		
		+ '<embed id="mp" type="application/x-mplayer2" src="'+media+'"' 
		+ 'classid="clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6"'
		+ 'pluginspage="http://www.microsoft.com/Windows/MediaPlayer/"'
		+ 'type="application/x-mplayer2"'
		+ 'url="'+media+'"' 
		+ 'volume="50"' 
		+ 'width="1" height="1">' 
		+ 'autostart="false"'		
		+ '<\/embed>'
		+ '<\/object>';
		mp = document.mp;
		document.getElementById("volumeDisplay").innerHTML = mp.settings.volume + "%";
	}
	else { // if Safari or Firefox, then load Quicktime controls
		if (start)
			var autostart = "true";
		else
			var autostart = "false";
		mpOut.innerHTML = '<OBJECT '
		+ 'CLASSID="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" '
		+ 'WIDTH="1"HEIGHT="1" ID="mp"'
		+ 'style="position:absolute;left:-1000;top:-1000"'
		+ 'CODEBASE="http://www.apple.com/qtactivex/qtplugin.cab">'
		+ '<PARAM name="SRC" VALUE="'+media+'">'
		+ '<PARAM name="AUTOPLAY" VALUE="'+autostart+'">'
		+ '<PARAM name="CONTROLLER" VALUE="false">'
		+ '<PARAM name="VOLUME" VALUE="128">'
		+ '<PARAM name="ENABLEJAVASCRIPT" VALUE="true">'
		+ '<PARAM name="TYPE" VALUE="audio/wav">'
		+ '<embed classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B"' 
		+ 'name="mp"'
		+ 'id="mp"' 
		+ 'src="'+media+'"' 
		+ 'pluginspage="http://www.apple.com/quicktime/download/"'
		+ 'volume="128"' 
		+ 'enablejavascript="true" '
		+ 'type="audio/wav" '
		+ 'height="1" '
		+ 'width="1"'
		+ 'style="position:absolute;left:-1000;top:-1000"'
		+ 'autostart="'+autostart+'"'
		+ '> </embed>'
		+ '</OBJECT>';
		mp = document.mp;
		document.getElementById("volumeDisplay").innerHTML = mp.GetVolume() + "%";
	}
	if (!start)
		stop();
	else
		play();
}

function play() {
	var control = document.getElementById("mp_play_pause");
	control.src = "images/mp_pause.gif";
	control.onclick = pause;
	
	if (browser == "MSIE" || browser == "Netscape") {
		mp.controls.play();
	}
	else {
		mp.Play();
	}
}

function pause() {
	var control = document.getElementById("mp_play_pause");
	control.src = "images/mp_play.gif";
	control.onclick = play;
	
	if (browser == "MSIE" || browser == "Netscape") {
		mp.controls.pause();
	}
	else {
		mp.Stop();
	}
}

function stop() {
	if (browser == "MSIE" || browser == "Netscape") {
		mp.controls.stop();
	}
	else {
		mp.Rewind(0);
	}
	pause();
}

function volumeUp() {
	var curVol;
	if (browser == "MSIE" || browser == "Netscape") {
		curVol = mp.settings.volume;
		if ((curVol+10) >= 100)
			nextVol = 100;
		else
			nextVol = curVol + 10;
		mp.settings.volume = nextVol;
	}
	else {
		curVol = mp.GetVolume();
		if ((curVol+39) >= 256)
			nextVol = 256;
		else
			nextVol = curVol + 39;
		mp.SetVolume(nextVol);
	}
	document.getElementById("volumeDisplay").innerHTML = nextVol + "%";
}

function volumeDown() {
	var curVol;
	if (browser == "MSIE" || browser == "Netscape") {
		curVol = mp.settings.volume;
		if ((curVol-10) <= 0)
			nextVol = 0;
		else
			nextVol = curVol - 10;
		mp.settings.volume = nextVol;
	}
	else {
		curVol = mp.GetVolume();
		if ((curVol-39) <= 0)
			nextVol = 0;
		else
			nextVol = curVol - 39;
		mp.SetVolume(nextVol);
	}
	document.getElementById("volumeDisplay").innerHTML = nextVol + "%";
}

function detect_browser() {
	var browser_name = navigator.userAgent;
	// We have to check for Opera first because
	// at the beginning of the userAgent variable
	// Opera claims it is MSIE. 
	
	if (browser_name.indexOf("Opera")!= -1)
		browser_name = "Opera";
	else if (browser_name.indexOf("Firefox")!= -1)
		browser_name = "Firefox";
	else if (browser_name.indexOf("MSIE")!= -1)
		browser_name = "MSIE";
	else if (browser_name.indexOf("Netscape")!= -1)
		browser_name = "Netscape";
	else if (browser_name.indexOf("Safari")!= -1)
		browser_name = "Safari";
	
	return browser_name;
}