/* Cool Javascript MP3 Playback in Web Page with Custom Controls
By Jeff Baker
Created October 12, 2007
Copyright 2007 by Jeff Baker
www.seabreezecomputers.com
*/

var browser_name;
var fadeSound = false;

var t; // for volume timer

var fade_out = 1; // 0 = no fade out when stopping or changing songs
// change to fade_out = 1 for fade volume between songs
// or you can use the fade out button

//var media = 'stillsAudio.mp3'; // holds the filename of the current song

// Make a DIV to hold the player and place it off the screen
// so that we don't see it
document.write('<DIV ID="player">-</DIV>');

function load(media)
{  

var player = document.getElementById('player'); 

if (detect_browser() == "MSIE" || 
		detect_browser() == "Netscape")
{ 
player.innerHTML = '<object id="sound"' 
+ '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="160" height="144">'                
+ '<param name="url" value="'+media+'">'      
+ '<param name="volume" value="100">'            
+ '<embed id="sound" 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="100"' 
+ 'width="160" height="144">'               
+ '<\/embed>'
+ '<\/object>';
}
else // if Safari or Firefox, then load Quicktime controls
{
player.innerHTML = '<OBJECT '
+ 'CLASSID="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" '
+ 'WIDTH="160"HEIGHT="144" ID="sound"'
+ 'CODEBASE="http://www.apple.com/qtactivex/qtplugin.cab">'
+ '<PARAM name="SRC" VALUE="'+media+'">'
+ '<PARAM name="AUTOPLAY" VALUE="true">'
+ '<PARAM name="CONTROLLER" VALUE="false">'
+ '<PARAM name="VOLUME" VALUE="100">'
+ '<PARAM name="ENABLEJAVASCRIPT" VALUE="true">'
+ '<PARAM name="TYPE" VALUE="audio/wav">'
+ '<embed classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B"' 
+ 'name="sound"'
+ 'id="sound"' 
+ 'src="'+media+'"' 
+ 'pluginspage="http://www.apple.com/quicktime/download/"'
+ 'volume="100"' 
+ 'enablejavascript="true" '
+ 'type="audio/wav" '
+ 'height="16" '
+ 'width="200"'
+ 'autostart="true"'
+ '> </embed>'
+ '</OBJECT>';
}
detect_browser();

} // end function load(media)

function play_song()
{
	load(media);
}  // end function play

function stop_song()
{
	var done;
	var mseconds; // milliseconds
	var player = document.getElementById('player'); 
	
	// if IE or Netscape then Media Player Pause Controls
	if (detect_browser() == "MSIE" || 
		detect_browser() == "Netscape")
		mseconds = 100;
	else // if Firefox
		mseconds = 50;
	
	if (document.getElementById('player').innerHTML != '-')
	if (fade_out == 1) // if we are fading the volume
	{
		done = fader();
		if (!done)
		{	
			fadeSound = true;
			setTimeout('stop_song();', mseconds);
			return;
		}	
	}	
	
	// Call stop function if available in quicktime player
	//document.getElementById('message').innerHTML = (typeof document.sound.Stop);
	if (document.sound)
	if (typeof document.sound.Stop == 'function')
	{
		if (document.getElementById('message'))
		document.getElementById('message').innerHTML = "QT Stop.";
		document.sound.Stop();
	}
	// Call stop function if available in Media player
	//document.getElementById('message').innerHTML = (typeof document.sound.controls);
	if (document.sound)
	if (typeof document.sound.controls == 'object')
	{
		if (document.getElementById('message'))
		document.getElementById('message').innerHTML = "WMP Stop.";
		document.sound.controls.Stop();
	}
	
	// Wipe out contents of player DIV
	if(browser_name == 'Firefox') {
		player.innerHTML = '-';
	}
	document.getElementById('controls').innerHTML = '';
	
} // end function stop()

function detect_browser()
{
	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;
} // end function detect_browser()

function volume(dir)
{
	var current_volume;
	
	// if a song is not playing then just return
	if (document.getElementById('player').innerHTML == '-')
		return;

	// if IE or Netscape then Media Player Volume Controls
	if (detect_browser() == "MSIE" || 
		detect_browser() == "Netscape")
{ 
	if (!document.sound.settings)
	{
		if (document.getElementById('message'))
			document.getElementById('message').innerHTML = 'This'
			+ ' browser does not support volume control.';
		return;
	}
		
	current_volume = document.sound.settings.volume;

	if (dir == 1 || dir == 5) // left or down
	{	
		document.sound.settings.volume = current_volume-10;
		//document.embeds['sound'].SetVolume(current_volume-10);
		if (dir == 1)
			t = setTimeout('volume('+dir+')', 100);
	}
	else if (dir == 2) // right or up
	{
		document.sound.settings.volume = current_volume+10;
		//document.embeds['sound'].SetVolume(current_volume+10);
		t = setTimeout('volume('+dir+')', 100);
	}
	else if (dir == 3) // stop changing volume
	{
		clearTimeout(t);
	}
}
else // if Firefox or Safari then Quicktime volume controls
{
	// Check to see if GetVolume is a function
	// If not then return
	// So far Safari does not support GetVolume
	if (typeof document.embeds['sound'].GetVolume != 'function')
	{
		if (document.getElementById('message'))
			document.getElementById('message').innerHTML = 'This'
			+ ' browser does not support volume control.';
		return;
	}
		
	current_volume = document.embeds['sound'].GetVolume();	

	if (dir == 1 || dir == 5) // left or down
	{	
		//document.sound.settings.volume = current_volume-10;
		document.embeds['sound'].SetVolume(current_volume-10);
		if (dir == 1)
			t = setTimeout('volume('+dir+')', 100);
	}
	else if (dir == 2) // right or up
	{
		//document.sound.settings.volume = current_volume+10;
		document.embeds['sound'].SetVolume(current_volume+10);
		t = setTimeout('volume('+dir+')', 100);
	}
	else if (dir == 3) // stop changing volume
	{
		clearTimeout(t);
	}
	
}	
	return current_volume;
} // end function volume(dir)

function fader()
{
	var current_volume;
	
	// if IE or Netscape then Media Player Volume Controls
	if (detect_browser() == "MSIE" || 
		detect_browser() == "Netscape")
{ 
	if (!document.sound.settings)
	{
		if (document.getElementById('message'))
			document.getElementById('message').innerHTML = 'This'
			+ ' browser does not support volume control.';
		return 1; // done because no fade support
	}
}
else // if Firefox or Safari then Quicktime volume controls
{
	// Check to see if GetVolume is a function
	// If not then return
	// So far Safari does not support GetVolume
	if (typeof document.embeds['sound'].GetVolume != 'function')
	{
		if (document.getElementById('message'))
			document.getElementById('message').innerHTML = 'This'
			+ ' browser does not support volume control.';
		return 1; // done because no fade support
	}
}	
	current_volume = volume(5);
	//document.getElementById('message').innerHTML = current_volume;
	if (current_volume <= 0)
	{
		volume(3); // end volume decrease
		return 1; // return done
	}
	else
		return 0;	

} // end function fader()

//window.onload = load(media);

