/* 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 folder = 'http://neimah.org/music/'; // if your songs are in a different folder specify it here
// example: var folder = 'music/'; 

var t; // for volume timer
var t2; // for song time display timer
var t3; // used for fast_reverse in media player

var current_song = 0; // start at song 0 in playlist
var fade_out = 0; // 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 songs = new Array(); // will hold the playlist of songs
var media; // holds the filename of the current song
var songtoplay = "";

var play = 0; //media is playing
var playedonesong = 0;





// 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"'
+ 'style="position:absolute;left:-1000;top:-1000"'
+ '></DIV>');

function load(media)
{  

media = folder + 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="1" height="1">'                
+ '<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="1" height="1">'               
+ '<\/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"'
+ 'style="position:absolute;left:-1000;top:-1000"'
+ '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"'
+ 'style="position:absolute;left:-1000;top:-1000"'
+ 'autostart="true"'
+ '> </embed>'
+ '</OBJECT>';
}

} // end function load(media)

function play_song(song)
{
	songtoplay = song
	stop_song()
	
	//if (playedonesong == 0)
	//{
		// Check to see if current song has stopped
	//	if (play == 0)
	//	{
			load(songtoplay);
	//	}
	//	else // otherwise wait until it has 
	//		stop_song();
	//		play = 0;
	//		setTimeout('play_song(songtoplay)', 500);
	//}
	//else // already played one song
	
	play = 1;
}  // 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 = 500;
	else // if Firefox
		mseconds = 200;
	
	//if (document.getElementById('player').innerHTML != '')
	//if (fade_out == 1) // if we are fading the volume
	//{
	//	done = fader();
	//	if (!done)
	//	{	
	//		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')
	{
	//	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')
	{
	//	document.getElementById('message').innerHTML = "WMP Stop.";
		document.sound.controls.Stop();
	}
	play = 0;
} // end function stop()

function pause_song()
{

	// if a song is not playing then just return
	//if (document.getElementById('player').innerHTML == '')
	//	return;

	
// if IE or Netscape then Media Player Pause Controls
	if (detect_browser() == "MSIE" || 
		detect_browser() == "Netscape")
{ 	
	if (!document.sound.controls)
	{
		//if (document.getElementById('message'))
		//	document.getElementById('message').innerHTML = 'This'
		//	+ ' browser does not support pause control.';
		//return;
	}

	// if pause
	if (play == 1)
	{
		document.sound.controls.pause();
		play = 0;
	//	document.getElementById('pause_btn').innerText = 'Unpause';
	}
	else // if unpause
	{
		document.sound.controls.play();
	//	document.getElementById('pause_btn').innerText = 'Pause';
	}
}
else // If Firefox or Safari then use Quicktime Stop (Pause)	
{

	// Check to see if Stop is a function
	// If not then return
	// So far Safari does not support Stop
	if (typeof document.embeds['sound'].Stop != 'function')
	{
		if (document.getElementById('message'))
			document.getElementById('message').innerHTML = 'This'
			+ ' browser does not support pause control.';
		return;
	}
	
	// if pause
	//if (document.getElementById('pause_btn').innerHTML == 'Pause')
	//{
		document.embeds['sound'].Stop();
	//	document.getElementById('pause_btn').innerHTML = 'Unpause';
	//}
	//else // if unpause
	//{
	//	document.embeds['sound'].Play();
	//	document.getElementById('pause_btn').innerHTML = 'Pause';
	//}
}

}  // end function pause_song()


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;
	

} // end function detect_browser()



