var folder = ''; // 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 songs = new Array(); // will hold the playlist of songs
var media; // holds the filename of the current song

function loadsongs() {
	
	var songs_list = document.getElementById('songlist').value;
	var songs_list_length = songs_list.length;

	
	// Remove all CRs (13) from IE's textarea
	songs_list = songs_list.replace(/\r/gi, "");
	// first remove first carriage return if there is one
	// IE = CRLF (1310)
	// Firefox + Netscape = LF (10)
	// Safari = LFLF (1010)
	// So the first if statement is for IE and Safari
	// to check for LF on the second character
	if (songs_list.charAt(1) == '\n')
		songs_list = songs_list.substr(1);
	if (songs_list.charAt(0) == '\n')
		songs_list = songs_list.substr(1);
	// now remove carriage return from the end of string if there is one
	// first if needed for Safari
	// second if needed for netscape and firefox
	if (songs_list.charAt(songs_list_length-3) == '\n')
		songs_list = songs_list.substr(0, songs_list_length-3);
	if (songs_list.charAt(songs_list_length-2) == '\n')
		songs_list = songs_list.substr(0, songs_list_length-2);
	if (songs_list.charAt(songs_list_length-1) == '\n')
		songs_list = songs_list.substr(0, songs_list_length-1);

		
	// Note: IE will only split a TEXTAREA by \n. It will not
	// work with a DIV
	songs = songs_list.split('\n');
	
	media = songs[0]; // media becomes first song in list
	
	display_song();

}  // function loadsongs()

// 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="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+'"' 
		+ 'voume="100"' 
		+ 'width="160" height="144">'               
		+ '<\/embed>'
		+ '<\/object>';
	}


}

function play_song() {
	// Check to see if current song has stopped
	if (document.getElementById('player').innerHTML == '') {
		load(media);
		setTimeout('display_time();', 1000);
		setTimeout('display_info();', 1000);
		display_song();	
	}
	else // otherwise wait until it has 
		setTimeout('play_song()', 500);
}  // 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;
	
	if (document.getElementById('player').innerHTML != '')

	// 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').value = "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').value = "WMP Stop.";
		document.sound.controls.Stop();
	}	
	// Stop time display timer
	clearTimeout(t2);
	// Stop fast_reverse timer
	clearTimeout(t3);	
	// Wipe out contents of player DIV
	player.innerHTML = '';	
	// set speed display to 1X
	document.getElementById('speed').innerHTML = '1X';
} // 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').value = 'This'
				+ ' browser does not support pause control.';
			return;
		}
		
		// if pause
		if (document.getElementById('pause_btn').value == 'Pause') {
			document.sound.controls.pause();
			document.getElementById('pause_btn').value = 'Unpause';
		}
		 // if unpause
		else {
			document.sound.controls.play();
			document.getElementById('pause_btn').value = '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()

function volume(dir) {
	var current_volume;
	document.sound.settings.volume = dir;
	/*
	
	// 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').value = 'This'
				+ ' browser does not support volume control.';
			return;
		}
			
		current_volume = document.sound.settings.volume;
		
		if (document.getElementById('vol_display'))
			document.getElementById('vol_display').innerHTML = current_volume;
		
		// left or down
		if (dir == 1 || dir == 5)  {	
			document.sound.settings.volume = current_volume-10;
			//document.embeds['sound'].SetVolume(current_volume-10);
			if (dir == 1)
				t = setTimeout('volume('+dir+')', 100);
		}
		 // right or up
		else if (dir == 2) {
			document.sound.settings.volume = current_volume+10;
			//document.embeds['sound'].SetVolume(current_volume+10);
			t = setTimeout('volume('+dir+')', 100);
		}
		// stop changing volume
		else if (dir == 3) 	{
			clearTimeout(t);
		}
	}	*/
	return current_volume;
} // end function volume(dir)

window.onload = loadsongs;

